Add Pages to PDF using 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 using 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 pdfDocument = new Document();

// Add page
pdfDocument.getPages().add();
// Insert an empty page in a PDF
pdfDocument.getPages().insert(2);

// Save updated PDF
pdfDocument.save(DATA_DIR.resolve( "InsertEmptyPage_out.pdf").toString());
pdfDocument.close();