When an old document is updated; the outdated, obsolete, or even highly confidential pages are required to be removed from the latest version of the document. In this article, we will learn how to programmatically remove such pages from the PDF documents in Java. Further, we will discuss different ways to remove list of pages, pages range, even and odd pages of PDF document.

The following topics are discussed below:

Java API to Remove Pages from PDF

GroupDocs.Merger privides the Java API that allows programmatically removing of pages from the PDF document. Furthermore, it allow changing pages orientation, moving pages location, splitting of documents, extraction, and rotation of document pages. I will use this GroupDocs.Merger for Java to delete various pages of PDF files in Java. For the details and other features of the API, you can visit its documentation.

Download and Configure

Get the library from the downloads section. For your Maven-based Java application, just add the following pom.xml configuration. After this, you can try the examples of this article as well the many more example available on GitHub. For the details, you may visit the API Reference.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>

<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-merger</artifactId>
        <version>22.2</version> 
</dependency>

Remove Selected Pages from PDF in Java

To remove any set of pages, you only need to provide the list of pages numbers from the loaded PDF document. The below steps allow removing the provided list of selective pages from a PDF document in Java.

  • Initialize RemoveOptions class with the page numbers to remove.
  • Instantiate Merger object with source document path or stream.
  • Call removePages() method to delete the listed pages.
  • Call the appropriate save() method to save the resultant document.

The following Java code example removes the selected 2nd and 4th page from the PDF document.

Remove Pages Range from PDF in Java

Similarly, you can remove any range of pages within your PDF document. The following steps allow removing any range of pages from the PDF files in Java.

  • Initialize RemoveOptions.
  • Provide the page range by setting starting and ending page number.
  • Instantiate Merger object with source document path or stream.
  • Call removePages() method with the range.
  • Call the appropriate save() method to save the resultant document.

The following Java sample code removes all the pages from the PDF document within the provided range i.e. 3 to 5.

Remove Even or Odd Pages from PDF in Java

You can also remove any even/odd pages of the document. The following steps show how to remove even or odd pages of the PDF file within the given range in Java.

  • Initialize RemoveOptions class with the page range.
  • Set the mode to even or odd.
  • Instantiate Merger object with source document path or stream.
  • Call removePages() method with the removal options.
  • Call the appropriate save() method to save the resultant document.

The following Java code snippet removes all the odd pages from the whole PDF document.

The following Java code sample removes all the even pages from the PDF document within the provided range i.e. 1-5.

Get a Free API License

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion

To conclude, we learned to delete different set of pages from PDF documents within Java applications. Specifically, we have seen how to delete pages by providing page numbers and page ranges. Additionally, we saw how to remove odd or even pages from any PDF document in Java. You can try building your own application to eliminate any set of pages from your PDF files.

For more details about the API, visit the documentation. For queries, contact us via the forum.

See Also