How to Remove Pages from PDF using Java

In this article, you will learn the process of how to remove pages from PDF using Java. All the details required for setting the environment, a list of operations to be performed while developing the application, and a ready-to-run sample code to remove pages from PDF document using Java. We will have a quick glimpse of different options to delete pages along with the introduction of other possible operations with the loaded PDF file.

Steps to Remove Some Pages from PDF using Java

  1. Set the environment to use Aspose.PDF for Java to remove selected pages
  2. Prepare a list of page numbers that are to be deleted and save them in an integer array
  3. Declare and instantiate an object of the PdfFileEditor class object to delete the selected pages
  4. Call the delete() method by providing a source PDF file, an array of integers, and the output PDF file name

The process to delete some pages from PDF using Java is summarized in these steps. The process is commenced by creating an array of integers where the target page numbers are stored for deletion. In the next step, the PdfFileEditor class object is declared which has multiple options to delete pages in a PDF. In the final step, the delete() method is called by providing the necessary arguments.

Code to Delete PDF Pages using Java

In this sample code, the process to remove pages from PDF file using Java is demonstrated using a few lines of code only. The PdfFileEditor class object not only allows deleting the pages from files on disk but also supports removing pages from PDF stored in a stream and saving the output PDF in a stream as well. This class has other features for instance you can insert pages, extract pages and create a new PDF, split a PDF in different ways, make a booklet, and resize the contents to name a few.

In this article, we have learned the process of how to delete pages from PDF using Java. If you want to learn other operations like merging multiple PDF files into a single PDF, refer to the article on how to merge PDF files in Java.

 English