Java protect presentation slides by setting the property with mark as final
Mark as Final means that the presentation slide is final edition and the author doesn’t want any changes on the document. With Spire.Presentation for Java, we can protect the presentation slides by setting the password. This article demonstrates how to mark a presentation as final by setting the document property MarkAsFinal as true.
import com.spire.presentation.FileFormat; import com.spire.presentation.Presentation; public class MarkAsFinal { public static void main(String[] args) throws Exception { //Create a PPT document and load file Presentation presentation = new Presentation(); presentation.loadFromFile("Sample.pptx"); //Set the document property MarkAsFinal as true presentation.getDocumentProperty().set("_MarkAsFinal", true); //Save the document to file presentation.saveToFile("output/MarkasFinal.pptx", FileFormat.PPTX_2010); } }
Effective screenshot after mark as final for presentation:
Add or Delete Rows and Columns from PowerPoint Table in Java
This article demonstrates how to add or delete rows and columns in a PowerPoint table using Spire.Presentation for Java.
Here is a screenshot of the sample PowerPoint file:
Add a row and a column
import com.spire.presentation.*; public class AddRowAndColumn { public static void main(String[] args) throws Exception { //load the sample PowerPoint file Presentation presentation = new Presentation(); presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx"); //get the table in the document ITable table = null; for (Object shape : presentation.getSlides().get(0).getShapes()) { if (shape instanceof ITable) { table = (ITable) shape; //add the last row to the end of the table as a new row int rowCount = table.getTableRows().getCount(); TableRow row = table.getTableRows().get(rowCount - 1); table.getTableRows().append(row); //get the new row and set the text for each cell rowCount = table.getTableRows().getCount(); row = table.getTableRows().get(rowCount - 1); row.get(0).getTextFrame().setText("America"); row.get(1).getTextFrame().setText("Washington"); row.get(2).getTextFrame().setText("North America"); row.get(3).getTextFrame().setText("9372610"); //add the last column to the end of the table as a new column int colCount = table.getColumnsList().getCount(); TableColumn column =table.getColumnsList().get(colCount-1); table.getColumnsList().add(column); //get the new column and set the text for each cell colCount = table.getColumnsList().getCount(); column = table.getColumnsList().get(colCount-1); column.get(0).getTextFrame().setText("Population"); column.get(1).getTextFrame().setText("32370000"); column.get(2).getTextFrame().setText("7350000"); column.get(3).getTextFrame().setText("15040000"); column.get(4).getTextFrame().setText("26500000"); column.get(5).getTextFrame().setText("329740000"); } } //save the document presentation.saveToFile("output/AddRowAndColumn.pptx", FileFormat.PPTX_2013); } }
Delete a row and a column
import com.spire.presentation.FileFormat; import com.spire.presentation.ITable; import com.spire.presentation.Presentation; public class DeleteRowAndColumn { public static void main(String[] args) throws Exception { //load the sample PowerPoint file Presentation presentation = new Presentation(); presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx"); //get the table in the document ITable table = null; for (Object shape : presentation.getSlides().get(0).getShapes()) { if (shape instanceof ITable) { table = (ITable) shape; //delete the second column table.getColumnsList().removeAt(1, false); //delete the second row table.getTableRows().removeAt(1, false); } } //save the document presentation.saveToFile("output/DeleteRowAndColumn.pptx", FileFormat.PPTX_2013); } }
Modify Hyperlinks in Word in Java
This article demonstrates how to modify hyperlinks in Word including modifying hyperlink address and display text using Spire.Doc for Java.
Below is the sample Word document we used for demonstration:
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.Field; import java.util.ArrayList; import java.util.List; public class ModifyHyperlink { public static void main(String[] args) { //Load Word document Document doc = new Document(); doc.loadFromFile("Hyperlink.docx"); List hyperlinks = new ArrayList(); //Loop through the section in the document for (Section section : (Iterable<Section>) doc.getSections() ) { //Loop through the section in the document for (Paragraph para : (Iterable<Paragraph>) section.getParagraphs() ) { for (DocumentObject obj:(Iterable<DocumentObject>) para.getChildObjects() ) { if (obj.getDocumentObjectType().equals(DocumentObjectType.Field)) { Field field = (Field) obj; if (field.getType().equals(FieldType.Field_Hyperlink)) { hyperlinks.add(field); } } } } } hyperlinks.get(0).setCode("HYPERLINK \"http://www.google.com\""); hyperlinks.get(0).setFieldText("www.google.com"); doc.saveToFile("EditHyperlink.docx", FileFormat.Docx_2013); } }
Output:
Remove footnote from Word document in Java
We have already demonstrated how to use Spire.Doc to insert footnote to word document in Java applications. This article will show how to remove footnote from word document in Java.
Firstly, view the sample document with footnotes:
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.*; public class RemoveFootnote { public static void main(String[] args) throws Exception { //Load the Sample Word document. Document document = new Document(); document.loadFromFile("Sample.docx"); Section section = document.getSections().get(0); //Traverse paragraphs in the section and find the footnote for (int j = 0; j < section.getParagraphs().getCount(); j++) { Paragraph para = section.getParagraphs().get(j); int index = -1; for (int i = 0, cnt = para.getChildObjects().getCount(); i < cnt; i++) { ParagraphBase pBase = (ParagraphBase) para.getChildObjects().get(i); if (pBase instanceof Footnote) { index = i; break; } } if (index > -1) //remove the footnote para.getChildObjects().removeAt(index); } document.saveToFile("Removefootnote.docx", FileFormat.Docx); } }
Effective screenshot after remove the footnote from word document:
Apply Background Image to Slides in Java
This article demonstrates how to apply background image to all slides in a PowerPoint presentation using Spire.Presentation for Java.
import com.spire.presentation.FileFormat; import com.spire.presentation.Presentation; import com.spire.presentation.SlideBackground; import com.spire.presentation.drawing.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.FileInputStream; public class AppplyBgToAllSlides { public static void main(String[] args) throws Exception { //load a PowerPoint file Presentation presentation = new Presentation(); presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx"); //get the image data BufferedImage bufferedImage = ImageIO.read(new FileInputStream("C:\\Users\\Administrator\\Desktop\\bg.jpg")); IImageData imageData = presentation.getImages().append(bufferedImage); //loop through the slides for (int i = 0; i < presentation.getSlides().getCount() ; i++) { //apply the image to the specific slide as background SlideBackground background = presentation.getSlides().get(i).getSlideBackground(); background.setType(BackgroundType.CUSTOM); background.getFill().setFillType(FillFormatType.PICTURE); background.getFill().getPictureFill().setFillType(PictureFillType.STRETCH); background.getFill().getPictureFill().getPicture().setEmbedImage(imageData); } //save the file presentation.saveToFile("output/BackgroundImage.pptx", FileFormat.PPTX_2013); presentation.dispose(); } }
Detect and remove Word Macros in Java
Spire.Doc load the word document with macros, it also supports to detect if a Word document contains VBA macros and remove all the VBA macros from a word document. This article demonstrates how to detect and remove VBA macros from Word document in Java applications.
Firstly, please view the sample document with macros:
import com.spire.doc.Document; import com.spire.doc.FileFormat; public class RemoveMacro { public static void main(String[] args) throws Exception { //Load the Sample Word document. Document doc = new Document(); doc.loadFromFile("VBAMacros.docm"); //If the document contains Macros, remove them from the document. if (doc.isContainMacro() ) { doc.clearMacros(); } //save to file doc.saveToFile("output/RemoveMacro.docm", FileFormat.Docm); } }
Effective screenshot after clear the VBA macros from word document:
Replace Image with New Image in Word in Java
This article demonstrates how to replace an existing image in a Word document with a new image using Spire.Doc for Java.
Below is the screenshot of the original Word document before replacing image:
import com.spire.doc.Document; import com.spire.doc.DocumentObject; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.DocPicture; public class ReplaceImages { public static void main(String[] args){ //Load the Word document Document doc = new Document(); doc.loadFromFile("Images.docx"); //Get the first section Section section = doc.getSections().get(0); //Loop through the paragraphs in the section for (Paragraph para:(Iterable) section.getParagraphs() ) { //Loop through the child object in the paragraph for (DocumentObject obj:(Iterable) para.getChildObjects() ) { //Replace image with new image if(obj instanceof DocPicture){ DocPicture pic = (DocPicture)obj; pic.loadImage("Hydrangeas.jpg"); } } } //Save the resultant document doc.saveToFile("ReplaceWithImage.docx", FileFormat.Docx_2013); } }
Output:
How to Mannually Add Spire.PDF as Dependency in a .NET Standard Library Project
Step 1: Download the latest version of Spire.PDF Pack from the link below, unzip it, and you'll get the DLL files for .NET Standarad from the "netstandard2.0" folder. If you already have this folder in your disk, go straight to step two.
Step 2: Create a .NET Standard library project in your Visual Studio.
Step 3: Add all DLL files under the "netstandard2.0" folder as dependencies in your project.
Right-click "Dependencies" – select "Add Reference" – click "Browse" – selcet all DLLs under "netstandard2.0" folder – click "Add".
Step 4: Install the other five packages in your project via the NuGet Package Manager. They are SkiaSharp, System.Buffers, System.Memory, System.Text.Encoding.CodePages and System.Runtime.CompilerServices.Unsafe.
Right-click "Dependencies" – select "Manage NuGet Packages" – click "Browse" –type the package name – select the package from the search results – click "Install".
Step 5: Now that you've added all the dependences successfully, you can start to write your own .NET Standard library that is capable of creating and processing PDF documents.
using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace SpirePdfStandard { public class Class1 { public void CreatePdf() { //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Add a page PdfPageBase page = doc.Pages.Add(); //Draw text on the page at the specified position page.Canvas.DrawString("Hello World", new PdfFont(PdfFontFamily.Helvetica, 13f), new PdfSolidBrush(Color.Black), new PointF(50, 50)); //Save the document doc.SaveToFile("Output.pdf"); } } }
Rotate shapes on Word document in Java
This article demonstrates how to rotate shapes on a Word document using Spire.Doc for Java.
import com.spire.doc.Document; import com.spire.doc.DocumentObject; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.*; import com.spire.doc.fields.ShapeObject; public class RotateShape { public static void main(String[] args) throws Exception { //Load the Sample Word document. Document doc = new Document(); doc.loadFromFile("InsertShapes.docx"); //Get the first section Section sec = doc.getSections().get(0); //Traverse every paragraphs to get the shapes and rotate them for ( Paragraph para: (Iterable) sec.getParagraphs()) { for (DocumentObject obj : (Iterable) para.getChildObjects()) { if (obj instanceof ShapeObject) { ((ShapeObject) obj).setRotation(20); } } } //Save to file doc.saveToFile("output/RotateShape.docx", FileFormat.Docx); } }
Effective screenshot after rotating the shapes on word:
Align Table in PowerPoint in C#
Spire.Presentation supports setting alignment for table in a PowerPoint document. This article demonstrates how to align a table to the bottom of a PowerPoint slide using Spire.Presentation.
Below screenshot shows the original table before setting alignment:
using Spire.Presentation; namespace AlignTable { class Program { static void Main(string[] args) { //Load PowerPoint document Presentation ppt = new Presentation(); ppt.LoadFromFile("Table.pptx"); ITable table = null; //Loop through the shapes in the first slide foreach (IShape shape in ppt.Slides[0].Shapes) { //Find the table and align it to the bottom of the slide if (shape is ITable) { table = (ITable)shape; table.SetShapeAlignment(Spire.Presentation.ShapeAlignment.AlignBottom); } } //Save the resultant document ppt.SaveToFile("Result.pptx", FileFormat.Pptx2013); } } }
Output: