PDFBox Splitting a PDF Document - PDFBox

What is PDFBox Splitting a PDF Document?

In the previous section, we went through on how to add JavaScript to a PDF document. In this section, we will learn how to split a PDF document into multiple documents.

Splitting the Pages in a PDF Document

A PDF document can be divided into multiple PDF documents using a class called Splitter.

Below steps should be followed to split a PDF document.

Step 1: Loading an Existing PDF Document

To load an existing document, use the load() method of the PDDocument class. Instantiate this class to load the PDF document as depicted below. It accepts a file object as parameter.

Step 2: Instantiate the Splitter Class

Splitter class should be instantiated in order to use the methods in it for Splitting the document.

Step 3: Splitting the PDF Document

Splitter class has Split() method that is used to split the document which accepts an object of the PDDocument class as the parameter.

The function of the split() method is to split the each page of the document into individual documents and returns all these documents in form of a list.

Step 4: Creating an Iterator Object

After creating individual documents using split() method, we need to create an iterator object to traverse through the list of documents. We can get the iterator using the listIterator() method as depicted below.

Step 5: Closing the Document

Lastly, to close the document use close() method of the PDDocument class as depicted below.


Let us consider a PDF document by name sample.pdf in the path C:/PdfBox_Examples/ containing two pages – one having image and other having text as depicted below.


Example below explains on how to split above mentioned PDF document. We will load a PDF document named sample.pdf and split it into two documents sample1.pdf and sample2.pdf. Save the above code in a file by name SplitPages.java.

Once the file is saved, compile and execute it from command prompt using the below commands-

Above program will split the given PDF document. Below message is rendered upon execution-

Verify the path given while saving the document and you will see there are multiple PDFs were created with names sample1 and sample2 as depicted below.



All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PDFBox Topics