DEV Community

IDRSolutions
IDRSolutions

Posted on

How to Duplicate Pages in a PDF in Java (Tutorial)

Sometimes you might want to duplicate pages in a PDF. For example, when filling in multiple copies of a form. Our Java PDF toolkit, JPedal, makes this an easy task.

In this post, I will show you the three simple steps of duplicating pages using JPedal.

1. Download JPedal

You can download a JPedal trial jar here.

2. Add to your project

For this example, I am going to use Maven. If you want to use another build system, check out the setup page on our support site.

Install the downloaded jar to your local Maven repository:

mvn install:install-file -Dfile= -DgroupId=com.idrsolutions -Dpackaging=jar -DartifactId=jpedal -Dversion=1.0

Enter fullscreen mode Exit fullscreen mode

Add the following to your pom.xml file:

<dependencies>
    <dependency>
        <groupId>com.idrsolutions</groupId>
        <artifactId>jpedal</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

Enter fullscreen mode Exit fullscreen mode

3. Add to your code

Create an instance of PDF Manipulator and call the copy page method.

final PdfManipulator pdf = new PdfManipulator();
pdf.loadDocument(new File("inputFile.pdf"));
pdf.copyPage(1, 2);
pdf.apply();
pdf.writeDocument(new File("outputFile.pdf"));
pdf.closeDocument();
pdf.reset();

Enter fullscreen mode Exit fullscreen mode

Learn more about the PDF Manipulator API.

We can help you better understand the PDF format as developers who have been working with the format for more than 2 decades!

Top comments (0)