Tuesday, December 23, 2025
HomeLanguagesJavaRemoving Pages from a PDF Document using Java

Removing Pages from a PDF Document using Java

Program to remove pages from an existing document a PDF document. The external jar file is required to import in the program. Below is the implementation for the same.

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

Approach:

  1. Load an existing PDF document using the static method load() of the PDDocument class. This method accepts a file object as a parameter, since this is a static method you can invoke it using class name as shown below. 
    File file = new File(“path of the document”)  
    PDDocument.load(file);
  2. List the number of pages that exists in the PDF document using the getNumberOfPages() method as shown below.
    int noOfPages= document.getNumberOfPages();
    System.out.print(noOfPages);
  3. Remove a page from the PDF document using the removePage() method of the PDDocument class. To this method, 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.
    document.removePage(2);
  4. 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”);
  5. Finally, close the document using the close() method of the PDDocument class as shown below.
    document.close();

Note: External Jar required (Download by clicking here).

Below is the implementation of the above approach:

Java




// Removing Pages from a PDF document using Java
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
public class RemovingPages {
    public static void main(String args[])
        throws IOException
    {
        // Loading an existing document
        File file
            = new File("/home/mayur/gfgTemp.pdf");
        PDDocument document = PDDocument.load(file);
  
        // Listing the number of existing pages
        int noOfPages = document.getNumberOfPages();
        System.out.print(noOfPages);
  
        // Removing the pages
        document.removePage(1);
  
        System.out.println("page removed");
        // Saving the document
        document.save("/home/mayur/gfgTemp.pdf");
  
        // Closing the document
        document.close();
    }
}


Before Execution:

Total 4 pages

After Execution:

3 pages remaining

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32456 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6825 POSTS0 COMMENTS
Nicole Veronica
11959 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6912 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS