Wednesday, 9 September 2020

How to duplicate a page in PDF using Java

This article will introduce two methods to duplicate a page in PDF using Java programmatically.

  •         Duplicate a page in a PDF file to another PDF file
  •       Duplicate a page to other pages in the same PDF file

Test environment I used:

·    Intellij Idea2019.1

·    JDK 1.8.0

·    Spire.Pdf.jar

Add Spire.Pdf.jar to your project as a dependency

Method 1: Download the Free Spire.PDF for Java zip from the link, unzip it and find Spire.Pdf.jar in the “lib” folder. Finally add Spire.Pdf.jar to IDEA as a dependency.

Method 2: Create a Maven project, and specify e-iceblue Maven Repository configuration and define Spire.PDF for Java API dependency in your pom.xml as follows.

<repositories>

        <repository>

            <id>com.e-iceblue</id>

            <name>e-iceblue</name>

            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>

        </repository>

</repositories>

<dependencies>

    <dependency>

        <groupId>e-iceblue</groupId>

        <artifactId>spire.pdf.free</artifactId>

        <version>2.0.0</version>

    </dependency>

</dependencies>

Finally, you only need to click “Import Changes” to import the Spire.PDF jars.

Using the code

Duplicate a page in a PDF file to another PDF file

The following code snippets are used to achieve the purpose that duplicating the first page of Sample 1 to the front of Sample 2. Specific steps are listed below.

Step 1: Load two sample PDF files using the PdfDocument instance.

Step 2: Get the first page of Sample 1 and its size using getPages() and getSize() methods.

Step 3: Create a template based on the page and insert it to the front of Sample 2.

Step 4: Using the saveToFile method to save the Sample 2.

import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.graphics.PdfTemplate;
import java.awt.*;
import java.awt.geom.Dimension2D;

public class DuplicatePage1 {
   
public static void main(String[] args) {
       
//Load two sample PDF documents
       
PdfDocument pdf1 = new PdfDocument("C:\\Users\\Test1\\Desktop\\Sample1.pdf");
        PdfDocument pdf2 =
new PdfDocument("C:\\Users\\Test1\\Desktop\\Sample2.pdf");

       
//Get the first page of Sample1 document
       
PdfPageBase page = pdf1.getPages().get(0);
       
//Get the page size
       
Dimension2D size = page.getSize();
       
//Create an template based on the page
       
PdfTemplate template = page.createTemplate();

       
//Insert the template to the first page of Sample2
       
pdf2.getPages().insert(0,size,new PdfMargins(0,0));
        pdf2.getPages().get(
0).getCanvas().drawTemplate(template,new Point(0,0));

       
//Save to Sample2 document
       
pdf2.saveToFile("output/result1.pdf",FileFormat.PDF);
        pdf2.dispose();
    }
}

Output


Duplicate a page to other pages in the same PDF file

Free Spire.PDF for Java also supports duplicating a page to other pages in the same PDF file. The tutorial below is to duplicate the first page in a PDF file to the front and end of the file.

import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.graphics.PdfTemplate;
import java.awt.*;
import java.awt.geom.Dimension2D;
public class DuplicatePage2 {
   
public static void main(String[] args) {
       
//Load a Sample PDF document
       
PdfDocument pdf = new PdfDocument("C:\\Users\\Test1\\Desktop\\Sample2.pdf");
       
//Get the first page
       
PdfPageBase page = pdf.getPages().get(0);
       
//Get the page size
       
Dimension2D size = page.getSize();
       
//Create a template based on the page
       
PdfTemplate template = page.createTemplate();
       
//Insert two templates cyclically to the end of the sample document
       
for(int i=0; i<2; i++){
            page = pdf.getPages().add(size,
new PdfMargins(0));//Add a new page to the document
           
page.getCanvas().drawTemplate(template, new Point(0, 0));//Draw template on the new page
       
}
       
//Insert one template to the front of the sample document
       
pdf.getPages().insert(0,size,new PdfMargins(0,0));//Add a new page to the document
       
pdf.getPages().get(0).getCanvas().drawTemplate(template,new Point(0,0));//Draw template on the new page
        //Save the PDF file
       
pdf.saveToFile("output/result2.pdf",FileFormat.PDF);
    }

} 

Output




No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...