Split a PDF document into Multiple PDFs

To split a PDF document into multiple PDF documents, you may use Splitter.split() method of PDFBox Java API.

In this tutorial, we shall learn to split a PDF document with an example Java program.

PDFBox Example 1 – Split PDF to Multiple PDFs

In this example, we will take PDF with multiple pages, and split this PDF document to multiple PDFs where each resulting PDF document contains only one page from the source document.

SplitPDFExample.java

</>
Copy
import org.apache.pdfbox.multipdf.Splitter; 
import org.apache.pdfbox.pdmodel.PDDocument;

import java.io.File; 
import java.io.IOException; 
import java.util.List; 
import java.util.Iterator;

public class SplitPDFExample {

	public static void main(String[] args) throws IOException {
	      File file = new File("/home/tk/sample_pdf.pdf");
	      
	      // load pdf file
	      PDDocument document = PDDocument.load(file); 

	      // instantiating Splitter
	      Splitter splitter = new Splitter();
	      
	      // split the pages of a PDF document
	      List<PDDocument> Pages = splitter.split(document);

	      // Creating an iterator 
	      Iterator<PDDocument> iterator = Pages.listIterator();

	      // saving splits as pdf
	      int i = 0;
	      while(iterator.hasNext()) {
	         PDDocument pd = iterator.next();
	         // provide destination path to the PDF split
	         pd.save("/home/tk/pdfs/sample_part_"+ ++i +".pdf");
	         System.out.println("Saved /home/tk/pdfs/sample_part_"+ i +".pdf");
	      }
	      System.out.println("Provided PDF has been split into multiple.");
	      document.close();
	}

}

Output

Saved /home/tk/pdfs/sample_part_1.pdf
Saved /home/tk/pdfs/sample_part_2.pdf
Saved /home/tk/pdfs/sample_part_3.pdf
Saved /home/tk/pdfs/sample_part_4.pdf
Saved /home/tk/pdfs/sample_part_5.pdf
Saved /home/tk/pdfs/sample_part_6.pdf
Provided PDF has been split into multiple.

PDFBox Example 2 – Split PDF at Specified Interval

Following is a step by step guide to split a PDF document into multiple PDF documents which have been split at a particular interval in source document.

SplitPDFAtPageExample.java

</>
Copy
import org.apache.pdfbox.multipdf.Splitter; 
import org.apache.pdfbox.pdmodel.PDDocument;

import java.io.File; 
import java.io.IOException; 
import java.util.List; 
import java.util.Iterator;

public class SplitPDFAtPageExample {

	public static void main(String[] args) throws IOException {
	      File file = new File("/home/tk/sample_pdf.pdf");
	      
	      // load pdf file
	      PDDocument document = PDDocument.load(file); 

	      // instantiating Splitter
	      Splitter splitter = new Splitter();
	      
	      splitter.setSplitAtPage(2);
	      
	      // split the pages of a PDF document
	      List<PDDocument> Pages = splitter.split(document);

	      // Creating an iterator 
	      Iterator<PDDocument> iterator = Pages.listIterator();

	      // saving splits as pdf
	      int i = 0;
	      while(iterator.hasNext()) {
	         PDDocument pd = iterator.next();
	         pd.save("/home/tk/pdfs/sample_part_"+ ++i +".pdf");
	         System.out.println("Saved /home/tk/pdfs/sample_part_"+ i +".pdf");
	      }
	      
	      // close the document
	      document.close();
	}

}

Output

Saved /home/tk/pdfs/sample_part_1.pdf
Saved /home/tk/pdfs/sample_part_2.pdf
Saved /home/tk/pdfs/sample_part_3.pdf

By default, splitAtPage is set to 1.

Conclusion

In this PDFBox Tutorial, we have learnt to split a PDF document into multiple PDFs.