PDFBox - Removing Pages



Let us now learn how to remove pages from a PDF document.

Removing Pages from an Existing Document

You can remove a page from an existing PDF document using the removePage() method of the PDDocument class.

Step 1: Loading an Existing PDF Document

Load an existing PDF document using the static method loadPDF() of the Loader class. This method accepts a RandomAccessReadBufferedFile object as a parameter, since this is a static method you can invoke it using class name as shown below.

// Loading an existing document 
PDDocument document = Loader.loadPDF(
   new RandomAccessReadBufferedFile("D:/Projects/PDFBox/PdfBox_Examples/sample.pdf"));

Step 2: Listing the Number of Pages

You can list the number of pages that exists in the PDF document using the getNumberOfPages() method as shown below.

int noOfPages= document.getNumberOfPages();
System.out.println(noOfPages);

Step 3: Removing the Page

You can remove a page from the PDF document using the removePage() method of the PDDocument class. To this method, you need to pass the index of the page that is to be deleted.

While specifying the index for the pages in a PDF document, keep in mind that indexing of these pages starts from zero, i.e., if you want to delete the 1st page then the index value needs to be 0.

document.removePage(2);

Step 4: Saving the Document

After removing the page, save the PDF document using the save() method of the PDDocument class as shown in the following code block.

document.save("Path");

Step 5: Closing the Document

Finally, close the document using the close() method of the PDDocument class as shown below.

document.close();

Example - Removing Pages from a PDF Document

Suppose, we have a PDF document with name sample.pdf and it contains three empty pages as shown below.

Removing page before

This example demonstrates how to remove pages from an existing PDF document. Here, we will load the above specified PDF document named sample.pdf, remove a page from it, and save it in the path D:/Projects/PDFBox/PdfBox_Examples/sample.pdf. Save this code in a file with name PDFBoxDemo.java.

package com.tutorialspoint.pdfbox;

import java.io.IOException;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

public class PDFBoxDemo {

   public static void main(String args[]) throws IOException {

      // Loading an existing document 
      PDDocument document = Loader.loadPDF(
         new RandomAccessReadBufferedFile("D:/Projects/PDFBox/PdfBox_Examples/sample.pdf")); 

      // Listing the number of existing pages
      int noOfPages= document.getNumberOfPages();
      System.out.println(noOfPages);

      // Removing the pages
      document.removePage(2);

      System.out.println("page removed");

      // Saving the document 
      document.save("D:/Projects/PDFBox/PdfBox_Examples/sample.pdf");

      // Closing the document  
      document.close();         
   }  
}

Output

Compile and run the code to verify the following output −

3
page removed

If you verify the specified path, you can find that the required page was deleted and only two pages remained in the document as shown below.

Removing page after
Advertisements