How to merge two pdf files using itext in java?

To merge two or more pdf file using iText jar first download the iText jar files and include in the application classpath.

Steps:

1. Prepare input pdf file list as list of input stream.
2. Prepare output stream for merged pdf file.
3. call method to merge pdf files.
4. Create document and pdfReader objects.
5. Create pdf Iterator object using inputPdfList.
6. Create reader list for the input pdf files.
7. Create writer for the outputStream.
8. Open document.
9. Get PdfContentByte instance from writer object.
10. Iterate and process the reader list.
11. Create page and add content.
12. Close document and outputStream.

Example:

PDFMergeExample.java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
 
/**
 * This class is used to merge two or more 
 * existing pdf file using iText jar.
 * @author w3schools
 */
public class PDFMergeExample {
 
    static void mergePdfFiles(List<InputStream> inputPdfList,
            OutputStream outputStream) throws Exception{
 
        //Create document and pdfReader objects.
	Document document = new Document();
        List<PdfReader> readers = 
        		new ArrayList<PdfReader>();
        int totalPages = 0;
 
        //Create pdf Iterator object using inputPdfList.
        Iterator<InputStream> pdfIterator = 
        		inputPdfList.iterator();
 
        // Create reader list for the input pdf files.
        while (pdfIterator.hasNext()) {
                InputStream pdf = pdfIterator.next();
                PdfReader pdfReader = new PdfReader(pdf);
                readers.add(pdfReader);
                totalPages = totalPages + pdfReader.getNumberOfPages();
        }
 
        // Create writer for the outputStream
        PdfWriter writer = PdfWriter.getInstance(document, outputStream);
 
        //Open document.
        document.open();
 
        //Contain the pdf data.
        PdfContentByte pageContentByte = writer.getDirectContent();
 
        PdfImportedPage pdfImportedPage;
        int currentPdfReaderPage = 1;
        Iterator<PdfReader> iteratorPDFReader = readers.iterator();
 
        // Iterate and process the reader list.
        while (iteratorPDFReader.hasNext()) {
          PdfReader pdfReader = iteratorPDFReader.next();
          //Create page and add content.
          while (currentPdfReaderPage <= pdfReader.getNumberOfPages()) {
                      document.newPage();
                      pdfImportedPage = writer.getImportedPage(
                    		  pdfReader,currentPdfReaderPage);
                      pageContentByte.addTemplate(pdfImportedPage, 0, 0);
                      currentPdfReaderPage++;
          }
          currentPdfReaderPage = 1;
        }
 
        //Close document and outputStream.
        outputStream.flush();
        document.close();
        outputStream.close();
 
        System.out.println("Pdf files merged successfully.");
       }
 
	public static void main(String args[]){
	  try {
	    //Prepare input pdf file list as list of input stream.
	    List<InputStream> inputPdfList = new ArrayList<InputStream>();
	    inputPdfList.add(new FileInputStream("D:\\TestFile1.pdf"));
	    inputPdfList.add(new FileInputStream("D:\\TestFile2.pdf"));
 
	    //Prepare output stream for merged pdf file.
            OutputStream outputStream = 
            		new FileOutputStream("D:\\MergeFile.pdf");
 
            //call method to merge pdf files.
            mergePdfFiles(inputPdfList, outputStream);     
	   } catch (Exception e) {
		e.printStackTrace();
	  }
	}
}

Output:

Pdf files merged successfully.

Download this example.
 
Next Topic: How to add password protection to PDF using iText in Java?
Previous Topic: How to split an existing pdf file using iText jar in Java?