Merge or Split PDF Documents in Java
Merging or splitting PDF documents offers flexibility and convenience when working with PDFs, allowing users to consolidate related information or extract specific sections as needed. Whether you want to merge several reports into one comprehensive file or isolate specific pages for separate distribution, merging or splitting PDF documents provides efficient solutions for organizing and manipulating your digital content. In this article, I am going to introduce how to merge or split PDF documents in Java using Free Spire.Doc for Java library.
- Merge Multiple PDFs into a Single PDF Document in Java
- Split a PDF Document by Page Ranges in Java
Installing Free Spire.Pdf.jar
Free Spire.PDF for Java is a free and professional library for creating, reading and manipulating PDF documents in a Java application. But kindly note that the free version is limited to 10 pages when loading a PDF document. To get rid of the limitation, you may need to use the commercial version.
If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue</groupId>
<artifactId>spire.pdf.free</artifactId>
<verson>5.1.0</version>
</dependency>
</dependencies>
Merge Multiple PDFs into a Single PDF Document in Java
The Free Spire.PDF for Java library offers a convenient method, PdfDocument.mergeFiles(String[] filePaths), to merge PDF files from an array of file paths. Simply populate a string array with the paths of the desired PDF documents and invoke this method. The following example illustrates how to retrieve PDF files from a folder and merge them into a single PDF document.
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfDocumentBase;
import java.io.File;
public class MergePDFs {
public static void main(String[] args) {
//Specify folder path
File folder = new File("C:/Users/Administrator/Desktop/Document/");
//Get files under the folder
File[] listOfFiles = folder.listFiles();
//Define a string array
String[] filePaths = new String[listOfFiles.length];
//Loop through the files
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
//Get absolute path of each file and write the string array
filePaths[i] = listOfFiles[i].getAbsolutePath();
}
}
//Merge these files
PdfDocumentBase doc = PdfDocument.mergeFiles(filePaths);
//Save to a PDF file
doc.save("output/Merge.pdf", FileFormat.PDF);
doc.dispose();
}
}
Split a PDF Document by Page Ranges in Java
To divide a PDF document into multiple smaller PDF documents, follow these steps:
- Obtain the source document.
- Create PdfDocument objects according to the desired number of splits.
- Insert the chosen pages or page regions from the source document into each newly created PdfDocument object.
- Save each object as a separate PDF document.
import com.spire.pdf.PdfDocument;
public class SplitPdfByPageRange {
public static void main(String[] args) {
//Load the source PDF file
PdfDocument sourceDoc = new PdfDocument("C:/Users/Administrator/Desktop/source.pdf");
//Create another two PdfDocument objects
PdfDocument newDoc_1 = new PdfDocument();
PdfDocument newDoc_2 = new PdfDocument();
//Insert the first page of source file to the first document
newDoc_1.insertPage(sourceDoc, 0);
//Insert the rest pages of source file to the second document
newDoc_2.insertPageRange(sourceDoc, 1, sourceDoc.getPages().getCount() - 1);
//Save to two different PDF files
newDoc_1.saveToFile("output/NewPDF_1.pdf");
newDoc_2.saveToFile("output/NewPDF_2.pdf");
}
}