DEV Community

Cover image for Split and Merge PDF Files in Java
E-iceblue Product Family
E-iceblue Product Family

Posted on • Updated on

Split and Merge PDF Files in Java

As PDF file format is getting increasingly popular in business these days, office workers are often exposed to PDF documents and may also have the requirements to process the PDFs as well. For example, split a PDF containing multiple invoices into separate files, or combine several reports into a single PDF.

This article is going to introduce how we can split a large PDF and merge multiple PDFs by using Spire.PDF for Java. There are three pieces of code in the sections below illustrating the split and merge from the following three aspects.

  • Split PDF to multiple single-page PDFs
  • Split PDF to several files by page ranges
  • Merge multiple PDFs into one document

Before you can compile the code snippets below, download Spire.PDF for Java and import the Spire.Pdf.jar and Spire.Common.jar into your project.

Split PDF to Single-Page PDFs

Spire offers a quite straightforward mothed – split, to divide a multi-page PDF document into multiple single page based PDF documents. Simply load the source PDF file to a PdfDocument object, and then call split method to do the division.

//specify input file path and output directory
String inputFile= "C:/Users/Administrator/Desktop/test.pdf";
String outputDirectory = "C:/Users/Administrator/Desktop/Output/";

//create a PdfDocument instance
PdfDocument doc = new PdfDocument();

//load a PDF file
doc.loadFromFile(inputFile);

//split PDF to single-page PDFs
doc.split(outputDirectory + "output-{0}.pdf",1);

Split PDF by Page Ranges

If you’d like the PDF be split by the selected page numbers, for example, the first page becomes one document, the remaining pages become another, then you’ll need to create two new PDF documents and insert the selected page(s) from the source document to the new ones. The following code snippet demonstrates the same.

//specify input file path and output directory
String inputFile= "C:/Users/Administrator/Desktop/test.pdf";
String outputDirectory = "C:/Users/Administrator/Desktop/Output/";

//create a PdfDocument instance
PdfDocument sourceDoc = new PdfDocument();

//load a source PDF file
sourceDoc.loadFromFile(inputFile);

//create two extra PdfDocument objects
PdfDocument newDoc_1 = new PdfDocument();
PdfDocument newDoc_2 = new PdfDocument();

//insert the first page from source file to the new PDF document
newDoc_1.insertPage(sourceDoc, 0);

//insert the rest pages from source file to the second new PDF document
newDoc_2.insertPageRange(sourceDoc,1,sourceDoc.getPages().getCount()-1);

//save the new PDF documents
newDoc_1.saveToFile(outputDirectory + "output-1.pdf");
newDoc_2.saveToFile(outputDirectory + "output-2.pdf");

Merge PDFs

To accomplish this task, Spire offers another easy-to-use method named mergeFiles. Just get and store the input file paths in a String array, then pass it to the mergeFiles method as one parameter, and then save the returned object as a new PDF file.

//get the paths of the files that you want to merge
String[] filePaths = new String[]{
        "C:/Users/Administrator/Desktop/Source/input-1.pdf",
        "C:/Users/Administrator/Desktop/Source/input-2.pdf",
        "C:/Users/Administrator/Desktop/Source/input-3.pdf"
};

//specify output directory
String outputDirectory = "C:/Users/Administrator/Desktop/Output/";

//merge PDF files using mergeFiles method
PdfDocumentBase newDoc = PdfDocument.mergeFiles(filePaths);

//save the new PDF document
newDoc.save(outputDirectory + "Merged.pdf");

Top comments (1)

Collapse
 
smithharber profile image
smithharber • Edited

CubexSoft PDF Split tool that is easy to split multiple pages to different PDF files at a single time process? The software helps to split PDF pages in PDF file format within a few minutes. The tool supports complete Windows & Mac OS editions. The utility saves all-important information and splits PDF files in exact form. Download the PDF Split & PDF Merge Tool and know how to split and merge PDFs into multiple small PDFs for free of cost.

Image description

Image description