Split PDF using Apache PDFBox
PDF file can be split into many small files using the Apache PDFBox library. Lets see the steps and simple examples on how to Split PDF using Apache PDFBox.
The class mainly used for doing this is org.apache.pdfbox.multipdf.Splitter.
The method that we will be using is Splitter::split(). This method takes PDDocument as a paramter and return a list of PDDocuments by splitting it based on the number of pages by default. You can change the splitting algorithm using the below 3 methods of the Splitter class.
a) setSplitAtPage(int split)
This will tell the splitting algorithm where to split the pages. The default is 1, so every page will become a new document. If it was two then each document would contain 2 pages. If the source document had 5 pages it would split into 3 new documents, 2 documents containing 2 pages and 1 document containing one page.
b) setStartPage(int start)
This will set the start page.
c) setEndPage(int end)
This will set the end page.
Example
We had already merged a PDF in our last post, and we will use that same pdf to split it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package com.kscodes.examples.pdfbox; import java.io.FileInputStream; import java.io.IOException; import java.util.List; import org.apache.pdfbox.multipdf.Splitter; import org.apache.pdfbox.pdmodel.PDDocument; public class SplitPdfExample { public static void main(String args[]) { try { // load Document object with existing pdf. PDDocument pdDocument = PDDocument.load(new FileInputStream("K:\\Kscodes\\pdf\\merged.pdf")); // Intialize a Splitter object Splitter splitter = new Splitter(); // Get a list of documents using splitter List<PDDocument> splittedDocuments = splitter.split(pdDocument); // Now save each of these documents as seperate PDF files int counter = 1; for (PDDocument document : splittedDocuments) { String pdfPath = "K:\\Kscodes\\pdf\\splitted" + counter + ".pdf"; document.save(pdfPath); System.out.println("PDF file created at :: " + pdfPath); counter++; } } catch (IOException ioe) { System.out.println("Error while saving pdf" + ioe.getMessage()); } } } |
Output