The PDF file format is not natively supported by Java. Therefore, to split a PDF file into multiple PDFs, you will need an external library. This tutorial explains how to split PDF files using JPedal.
Getting Started
- Add JPedal to your class or module path (download the trial jar)
- Create a File handle pointing to the PDF file
- Call one of the methods below from
PdfPageSplit
, and supply a number to determine where to split the PDF
How to split a PDF file into pages
To split a PDF file into individual pages, simply call the following method.
PdfPageSplit.splitAllPages(new File("/path/to/input.pdf"), new File("/path/to/output-folder"));
How to split a PDF file into two files
To split a PDF file into two files, call the following method and provide the page number where you want to split. The first file will contain pages up to and including pageToSplitAt
, and the second file will contain the rest.
PdfPageSplit.splitIn2(new File("/path/to/input.pdf"), new File("/path/to/output-folder"), pageToSplitAt);
How to split a PDF file into multiple files
To split a PDF file into multiple files, call the following method and provide the number of pages you want per file. Each file will contain pagePerNewFile
number of pages, and the final file will contain the remainder.
PdfPageSplit.splitToNPagePDFs(new File("/path/to/input.pdf"), new File("/path/to/output-folder"), pagePerNewFile);
How to split a PDF file that is password protected
JPedal also supports PDF file splitting for password protected files. To do this, provide the password as the last parameter in any of the above methods.
PdfPageSplit.splitAllPages(new File("/path/to/input.pdf"), new File("/path/to/output-folder"), "password");
Before and after
Notice how the original file remains untouched
In this article I showed you how to break large PDF files into multiple documents still in the PDF format. Breaking a PDF into multiple files will have many advantages such as being able to focus on specific content and helping with file size management.
Why do developers choose JPedal over alternatives?
- Actively developed commercial library with full support and no third party dependencies.
- Simple licensing options and source code access for OEM users.
- Process PDF files up to 3x faster than alternative Java PDF libraries.
The JPedal PDF library allows you to solve these problems in Java
Viewer viewer = new Viewer();
viewer.setupViewer();
viewer.executeCommand(ViewerCommands.OPENFILE, "pdfFile.pdf");
//Convenience static method (see class for additional options)
ExtractClippedImages.writeAllClippedImagesToDir("inputFileOrDirectory", "outputDir", "outputImageFormat", new String[] {"imageHeightAsFloat", "subDirectoryForHeight"});
//Convenience static method (see class for additional options)
ExtractTextAsWordList.writeAllWordlistsToDir("inputFileOrDirectory", "outputDir", -1);
//Convenience static method (see class for additional options)
ArrayList resultsForPages = FindTextInRectangle.findTextOnAllPages("/path/to/file.pdf", "textToFind");
PrintPdfPages print = new PrintPdfPages("C:/pdfs/mypdf.pdf");
if (print.openPDFFile()) {
print.printAllPages("Printer Name");
}
//Convenience static method (see class for additional options)
ExtractClippedImages.writeAllClippedImagesToDir("inputFileOrDirectory", "outputDir", "outputImageFormat", new String[] {"imageHeightAsFloat", "subDirectoryForHeight"});