Skip to content

Instantly share code, notes, and snippets.

@abelsromero
Created February 16, 2016 07:15
Show Gist options
  • Save abelsromero/57c5fb1257a4cddf20bb to your computer and use it in GitHub Desktop.
Save abelsromero/57c5fb1257a4cddf20bb to your computer and use it in GitHub Desktop.
PDF splitter with Apache PDFBox ('org.apache.pdfbox:pdfbox:1.8.11')
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.Splitter;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class PdfSplitter {
public static final String PATH = "PUT_YOUR_PATH_TO_PDF_HERE";
public static final String SRC_FILE = "FILE_TO_SPLIT";
public static void main(String[] args) throws IOException, COSVisitorException {
PDDocument document = PDDocument.load(new File(PATH, SRC_FILE));
Splitter splitter = new Splitter();
List<PDDocument> pages = splitter.split(document);
for (int i = 0; i < pages.size(); i++) {
PDDocument doc = pages.get(i);
doc.save(new File(PATH, "doc_" + i + ".pdf"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment