- PDFBox - Home
- PDFBox - Overview
- PDFBox - Environment
- PDFBox - Creating a PDF Document
- PDFBox - Adding Pages
- PDFBox - Loading a Document
- PDFBox - Removing Pages
- PDFBox - Document Properties
- PDFBox - Adding Text
- PDFBox - Adding Multiple Lines
- PDFBox - Reading Text
- PDFBox - Inserting Image
- Encrypting a PDF Document
- JavaScript in PDF Document
- PDFBox - Splitting a PDF Document
- Merging Multiple PDF Documents
- PDFBox - Converting PDF To Image
- PDFBox - Adding Rectangles
PDFBox - Useful Resources
PDFBox - Merging Multiple PDF Documents
In the previous chapter, we have seen how to split a given PDF document into multiple documents. Let us now learn how to merge multiple PDF documents as a single document.
Merging Multiple PDF Documents
You can merge multiple PDF documents into a single PDF document using the class named PDFMergerUtility class, this class provides methods to merge two or more PDF documents in to a single PDF document.
Following are the steps to merge multiple PDF documents.
Step 1: Instantiating the PDFMergerUtility class
Instantiate the merge utility class as shown below.
PDFMergerUtility PDFmerger = new PDFMergerUtility();
Step 2: Setting the destination file
Set the destination files using the setDestinationFileName() method as shown below.
PDFmerger.setDestinationFileName("D:/Projects/PDFBox/PdfBox_Examples/merged.pdf");
Step 3: Setting the source files
Set the source files using the addSource() method as shown below.
File file = new File("path of the document")
PDFmerger.addSource(file);
Step 4: Merging the documents
Merge the documents using the mergeDocuments() method of the PDFmerger class as shown below.
PDFmerger.mergeDocuments();
Example - Merging Multiple PDF Documents
Suppose, we have two PDF documents sample1.pdf and sample2.pdf, in the path D:/Projects/PDFBox/PdfBox_Examples/ as shown below.
This example demonstrates how to merge the above PDF documents. Here, we will merge the PDF documents named sample1.pdf and sample2.pdf in to a single PDF document merged.pdf. Save this code in a file with name PDFBoxDemo.java.
PDFBoxDemo.java
package com.tutorialspoint.pdfbox;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import java.io.File;
import java.io.IOException;
public class PDFBoxDemo {
public static void main(String[] args) throws IOException {
File file1 = new File("D:/Projects/PDFBox/PdfBox_Examples/sample1.pdf");
File file2 = new File("D:/Projects/PDFBox/PdfBox_Examples/sample2.pdf");
//Instantiating PDFMergerUtility class
PDFMergerUtility PDFmerger = new PDFMergerUtility();
//Setting the destination file
PDFmerger.setDestinationFileName("D:/Projects/PDFBox/PdfBox_Examples/merged.pdf");
//adding the source files
PDFmerger.addSource(file1);
PDFmerger.addSource(file2);
//Merging the two documents
PDFmerger.mergeDocuments();
System.out.println("Documents merged");
}
}
Output
Compile and run the code to verify the following output −
Documents merged
If you verify the given path, you can observe that a PDF document with name merged.pdf is created and this contains the pages of both the source documents as shown below.