Merging multiple PDFs into a single PDF using iText.

iText is a library that allows you to generate PDF files on the fly. Please find more details on iText on below link.

http://www.lowagie.com/iText/

Here is a code snippet which takes multiple PDFs stream as input and merging them to a new PDF file. The snippet uses iText java library.

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.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;

 public class ItextMerge {
    public static void main(String[] args) {
        List<inputstream> list = new ArrayList<inputstream>();
        try {
            // Source pdfs            
            list.add(new FileInputStream(new File("f:/1.pdf")));
            list.add(new FileInputStream(new File("f:/2.pdf")));

            // Resulting pdf            
            OutputStream out = new FileOutputStream(new File("f:/result.pdf"));

            doMerge(list, out);

         } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

     /**
     * Merge multiple pdf into one pdf
     *
     * @param list
     *            of pdf input stream
     * @param outputStream
     *            output file output stream
     * @throws DocumentException
     * @throws IOException
     */
    public static void doMerge(List<inputstream> list, OutputStream outputStream)
            throws DocumentException, IOException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, outputStream);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        
        for (InputStream in : list) {
            PdfReader reader = new PdfReader(in);
            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                document.newPage();
                //import the page from source pdf
                PdfImportedPage page = writer.getImportedPage(reader, i);
                //add the page to the destination pdf
                cb.addTemplate(page, 0, 0);
            }
        }
        
        outputStream.flush();
        document.close();
        outputStream.close();
    }
}

SHARE
    Blogger Comment
    Facebook Comment