Add Pages to PDF via Java

Insert pages to PDF document programmatically using Aspose.PDF for Java Library

How to Add pages to PDF Using Java

In order to insert page, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful and easy to use conversion API for Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

Insert Page to PDF via Java


You need Aspose.PDF for Java to try the code in your environment.

  1. Create a Document object with the input PDF file.

  2. Call the PageCollection collection’s Insert method with specified index.

  3. Save the output PDF using the Save method.

Insert New Page to PDF


    Document document = new Document();

    // Add page
    document.getPages().add();

    // Insert a empty page in a PDF
    document.getPages().insert(2);

    // Save updated PDF
    document.save(_dataDir + "InsertEmptyPage_out.pdf");