PDFBox Removing Pages
We can remove pages from existing PDF document. removePage() method of the PDDocument class is used to remove the page from the document.
Follow the steps below to remove page from the existing PDF document-
Load PDF Document
We can load the existing PDF document by using the static load() method. This method accepts a file object as a parameter. We can also invoke it using the class name PDDocument of the PDFBox.
List the Number of Pages
We need to list the number of pages that exist in the PDF document by using the getNumberOfPages() method as shown below.
Remove Page
The removePage() method of the PDDocument class is used to remove a page from the document. In this method, we need to pass the index of the page as a parameter which we want to delete from the PDF document. The indexing of the page starts from zero which means, if we want to delete the 4th page from the PDF document then its index is 3.
Save Document
After removing the required document, we have to save it to our desired location. save() method is used to save the document. The save() method accepts a string value and passes a path of the document as a parameter.
Close Document
After completing the task, we need to close the PDDocument class object by using the close() method.
Example-
Output:
After successful execution of above program, we can see the following message.