Duplicate a Page in PDF in Java
This article demonstrates how to duplicate a page within a PDF document using Spire.PDF for Java.
import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.graphics.PdfMargins; import com.spire.pdf.graphics.PdfTemplate; import java.awt.geom.Dimension2D; import java.awt.geom.Point2D; public class DuplicatePage { public static void main(String[] args) { //Load a sample PDF document PdfDocument pdf = new PdfDocument("C:\\Users\\Administrator\\Desktop\\original.pdf"); //Get the first page PdfPageBase page = pdf.getPages().get(0); //Get the page size Dimension2D size = page.getActualSize(); //Create a template based on the page PdfTemplate template = page.createTemplate(); for (int i = 0; i < 10; i++) { //Add a new page to the document page = pdf.getPages().add(size, new PdfMargins(0)); //Draw template on the new page page.getCanvas().drawTemplate(template, new Point2D.Float(0, 0)); } //Save the file pdf.saveToFile("output/DuplicatePage.pdf"); } }
Replace Text with Image in Word in Java
This article demonstrates how to replace selected text in a Word document with an image using Spire.Doc for Java.
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.documents.TextSelection; import com.spire.doc.fields.DocPicture; import com.spire.doc.fields.TextRange; public class ReplaceTextWithImage { public static void main(String[] args) { //Load a sample Word file Document document = new Document(); document.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx"); //Find the string 'E-iceblue' in the document TextSelection[] selections = document.findAllString("E-iceblue", true, true); //Replace the string with an image int index = 0; TextRange range = null; for (Object obj : selections) { TextSelection textSelection = (TextSelection)obj; DocPicture pic = new DocPicture(document); pic.loadImage("C:\\Users\\Administrator\\Desktop\\e-iceblue-logo.png"); range = textSelection.getAsOneRange(); index = range.getOwnerParagraph().getChildObjects().indexOf(range); range.getOwnerParagraph().getChildObjects().insert(index,pic); range.getOwnerParagraph().getChildObjects().remove(range); } //Save the document document.saveToFile("output/ReplaceTextWithImage.docx", FileFormat.Docx_2013); } }
Add Trendline to Chart in PowerPoint in Java
A trendline is a line superimposed on a chart revealing the overall direction of the data. Spire.Presentation for Java supports adding six different types of trendlines to chart, i.e. linear, logarithmic, polynomial, power, exponential and moving average.
The below example demonstrates how to use Spire.Presentation for Java to add a linear trendline to a chart.
import com.spire.presentation.FileFormat; import com.spire.presentation.ISlide; import com.spire.presentation.Presentation; import com.spire.presentation.charts.IChart; import com.spire.presentation.charts.ITrendlines; import com.spire.presentation.charts.TrendlineSimpleType; public class AddTrendlineToChart { public static void main(String[] args) throws Exception { //create a Presentation instance Presentation ppt = new Presentation(); //load the PowerPoint document ppt.loadFromFile("Chart.pptx"); //get the first slide ISlide slide = ppt.getSlides().get(0); //get the chart on the slide IChart chart = (IChart)slide.getShapes().get(0); //add a linear trendline to the first series of the chart ITrendlines trendLine = chart.getSeries().get(0).addTrendLine(TrendlineSimpleType.LINEAR); //display equation trendLine.setdisplayEquation(true); //save the resultant document ppt.saveToFile("AddTrendline.pptx", FileFormat.PPTX_2013); } }
Output:
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: