Merge Multiple PDF Documents using iText and Java
Previously, we saw how to split a single PDF document into multiple PDF documents. You may also face a scenario, where you need to merge multiple PDF documents into a single PDF document. These PDF documents can contain reports, tables, even multiple pages per document. In this example, we demonstrate how to merge multiple PDF documents using iText and Java.
Merge Multiple PDF Documents
- We start by creating a list that contains all PDF documents that we are merging. These documents reside in the
src/main/resources
folder. Using the staticgetResource()
method of the class we are able to obtain the path. - We create and initialize a
PdfCopy
object. The first argument, we pass theDocument
. The second argument we provide aFileOutputStream
. This contains the path where the document will be created. We can optionally use thePdfSmartCopy
. This has the same functionality as thePdfCopy
, but when resources – such as fonts, images – are encountered, a reference to these resources is saved in a cache, so that they can be reused. This requires more memory, but reduces the file size of the resulting PDF document. - Now, we can start merging PDF documents.
- First, we iterate over the list.
- During the iteration, we create a new
PdfReader
for every file. - We can merge the entire document using the
PdfCopy#addDocument()
method. Later on, we show how to merge only a specific page of a PDF document. - You can optionally call the
PdfCopy#freeReader()
method. This writes the reader to the document and free the memory used by it. This is often used when concatenating multiple documents to keep the memory usage restricted to the current appending document. - We close the
PdfReader
.
- Finally, we close the document and the file’ll be created.
package com.memorynotfound.pdf.itext;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MergePdf {
static List<URL> files = new ArrayList<URL>(Arrays.asList(
MergePdf.class.getResource("/example-001.pdf"),
MergePdf.class.getResource("/example-002.pdf"),
MergePdf.class.getResource("/example-003.pdf")
));
public static void main(String... args) throws IOException, DocumentException {
Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileOutputStream("merge-pdf-result.pdf"));
document.open();
for (URL file : files){
PdfReader reader = new PdfReader(file);
copy.addDocument(reader);
copy.freeReader(reader);
reader.close();
}
document.close();
}
}
Merging an Individual Page
Before, we merged the entire document directly using the PdfCopy#addDocument(reader)
method. You may want to merge a specific page. In this case, you can iterate over each page individually. When you reach the page you want to merge, you can add it to the PdfCopy
object using the addPage()
method.
document.open();
for (URL file : files){
PdfReader reader = new PdfReader(file);
for (int i = 1; i <= reader.getNumberOfPages(); i++){
// optionally write an if statement to include the page
copy.addPage(copy.getImportedPage(reader, i));
}
copy.freeReader(reader);
reader.close();
}
document.close();