Pages Navigation Menu

Coding is much easier than you think

Merge PDF files using java iText

merge_pdf_files in java
 

Library Required

 
itext-2.1.4.jar
 
File: MergePDF.java
 

package com.simplecode.util;

//Please include the itext-2.1.4.jar in the classpath
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;

public class MergePDF {

	public static void main(String[] arg) {
	try {
		List<InputStream> pdfs = new ArrayList<InputStream>();
		pdfs.add(new FileInputStream("C:\File1.pdf"));
		pdfs.add(new FileInputStream("C:\File2.pdf"));
		OutputStream output = new FileOutputStream("C:\Merge.pdf");
                // boolean flag that represents whether you need to include page number or not
		MergePDF.concatPDFs(pdfs, output, true);	
	}

	catch (Exception e) 
	{
		System.err.println("Exception occurs : " + e.getMessage());
	}
	}


	public static void concatPDFs(List<InputStream> streamOfPDFFiles,
			OutputStream outputStream, boolean paginate) {

	Document document = new Document();

	try {
		List<InputStream> pdfs = streamOfPDFFiles;
		List<PdfReader> readers = new ArrayList<PdfReader>();
		int totalPages = 0;
		Iterator<InputStream> iteratorPDFs = pdfs.iterator();

		// Create Readers for the pdfs.
		while (iteratorPDFs.hasNext()) {
			InputStream pdf = iteratorPDFs.next();
			PdfReader pdfReader = new PdfReader(pdf);
			readers.add(pdfReader);
			totalPages += pdfReader.getNumberOfPages();
		}

		// Create a writer for the outputstream
		PdfWriter writer = PdfWriter.getInstance(document, outputStream);
		document.open();
		BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
				BaseFont.CP1252, BaseFont.NOT_EMBEDDED);


		// Holds the PDF data
		PdfContentByte cb = writer.getDirectContent();

		PdfImportedPage page;
		int currentPageNumber = 0;
		int pageOfCurrentReaderPDF = 0;
		Iterator<PdfReader> iteratorPDFReader = readers.iterator();

		// Loop through the PDF files and add to the output.
		while (iteratorPDFReader.hasNext()) {
			PdfReader pdfReader = iteratorPDFReader.next();

		// Create a new page in the target for each source page.
			while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
			      document.newPage();
			      pageOfCurrentReaderPDF++;
			      currentPageNumber++;
			      page = writer.getImportedPage(pdfReader,pageOfCurrentReaderPDF);
			      cb.addTemplate(page, 0, 0);

				// Code for pagination.
				if (paginate) {
				     cb.beginText();
				     cb.setFontAndSize(bf, 9);
				     cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
				     + currentPageNumber + " of " + totalPages, 520, 5, 0);
				     cb.endText();
				}
			}
			pageOfCurrentReaderPDF = 0;
		}
		outputStream.flush();
		document.close();
		outputStream.close();
	}
	
	catch (Exception e) {
		System.err.println("Exception : " + e.getMessage());
	}
	
	finally {
		if (document.isOpen())
			document.close();
		
		try {
			if (outputStream != null)
				outputStream.close();
		    }
		
		catch (IOException ioe) {
			System.err.println("Exception : " + ioe.getMessage());
		}
	}
	}
}

 
Related Article
 
Splitting PDF File Using Java iText API
 

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

  • Jagannath

    How can convert gray pdf file to colored jpg images and then jpg to pdf.