Java: Add or Delete Pages in PDF Documents

2022-01-14 07:17:00 Written by  support iceblue
Rate this item
(0 votes)

When you edit a PDF document, it is sometimes necessary to delete redundant pages of the document or add new pages to the document. This article will show you how to add or delete pages in a PDF document using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>10.10.7</version>
    </dependency>
</dependencies>
    

Add Empty Pages to a PDF Document

The following steps show you how to add empty pages to a specific position of a PDF document and its end.

  • Create a PdfDocument instance.
  • Load a sample PDF document using PdfDocument.loadFromFile() method.
  • Create a new blank page and insert it into a specific position of the document using PdfDocument.getPages().insert(int index) method.
  • Create another new blank page with the specified size and margins and then append it to the end of the document using PdfDocument.getPages().add(java.awt.geom.Dimension2D size, PdfMargins margins) method.
  • Save the document to another file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfMargins;

public class InsertEmptyPage {
    public static void main(String[] args) {
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Load a sample PDF document
        pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.pdf");

        //Insert a blank page to the document as the second page
        pdf.getPages().insert(1);

        //Add an empty page to the end of the document
        pdf.getPages().add(PdfPageSize.A4, new PdfMargins(0, 0));

        //Save the document to another file
        pdf.saveToFile("output/insertEmptyPage.pdf");
        pdf.close();
    }
}

Java: Add or Delete Pages in PDF Documents

Delete an Existing Page in a PDF Document

The following steps are to delete a specific page of a PDF document.

  • Create a PdfDocument instance.
  • Load a sample PDF document using PdfDocument.loadFromFile() method.
  • Remove a specific page of the document using PdfDocument.getPages().removeAt(int index) method.
  • Save the document to another file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;

public class DeletePage {
    public static void main(String[] args) {

        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Load a sample PDF document
        pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.pdf");

        //Delete the second page of the document
        pdf.getPages().removeAt(1);

        //Save the document to another file
        pdf.saveToFile("output/deletePage.pdf");
        pdf.close();
    }
}

Java: Add or Delete Pages in PDF Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 27 July 2023 07:05