iText Merge PDF Example
In the previous example we saw how we can create and add Barcode to the PDF document. In this example we will demonstrate how we can merge multiple PDF Documents into one.
We often face a situation where we need to merge some of the PDF Documents in our applications. Itext provides us with a way to merge different PDF documents into a single PDF document. Let’s see how we can achieve this :
1. Project Setup
We shall use Maven to setup our project. Open eclipse and create a simple Maven project and check the skip archetype selection checkbox on the dialogue box that appears. Replace the content of the existing pom.xml
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ItextPDFMergeExample</groupId> <artifactId>ItextPDFMergeExample</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.6</version> </dependency> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.52</version> </dependency> </dependencies> </project>
That’s all from setting-up project point of view, let’s start with the actual code implementation for this demonstration now:
2. Implementation
MergePDFExample.java
package com.examples.jcg; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfImportedPage; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfWriter; public class MergePDFExample { public static void main(String[] args) { try { List<InputStream> list = new ArrayList<InputStream>(); InputStream inputStreamOne = new FileInputStream(new File("HelloWorld.pdf")); list.add(inputStreamOne); InputStream inputStreamTwo = new FileInputStream(new File("HelloWorld1.pdf")); list.add(inputStreamTwo); OutputStream outputStream = new FileOutputStream(new File("Merger.pdf")); mergePdf(list, outputStream); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static void mergePdf(List<InputStream> list, OutputStream outputStream) throws DocumentException, IOException { Document document = new Document(); PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream); document.open(); PdfContentByte pdfContentByte = pdfWriter.getDirectContent(); for (InputStream inputStream : list) { PdfReader pdfReader = new PdfReader(inputStream); for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) { document.newPage(); PdfImportedPage page = pdfWriter.getImportedPage(pdfReader, i); pdfContentByte.addTemplate(page, 0, 0); } } outputStream.flush(); document.close(); outputStream.close(); } }
We start off by creating FileInputStream
objects for each of the PDF Files we have to merge and add these InputStream
Objects to the List<InputStream>
instance. Next, we create an OutputStream
instance, which will create the file we wish to be the final output document.
The mergePdf
method creates an instance of PdfWriter using the OutputStream
object we created earlier. Next we iterate over the List
of InputStream
objects we created and create a com.itextpdf.text.pdf.PdfReader
object from each FileInputStream
instances we extract from the list.
Next, we iterate over all the pages in each of the document using the PdfReader.getNumberOfPages()
method. We import the page from the document using the PdfWriter.getImportedPage()
and then add the page to the new Document
by using the PdfContentByte.addTemplate
method.
We repeat this procedure for each page in each of the PDF Document we have to merge to get all the PDF Files in a single Document.
3. Download the source code
Here we studied how we can merge existing PDF Documents into a single PDF document using Itext.
You can download the source code of this example here: ItextPDFMergeExample.zip