Recent

How to merge more than one PDF files using Java



Hello friends, today I am going to tell you How to merge more than one PDF files using Java. Using this java code you can merge more than one PDF files to a new PDF file. 
Follow the bellow java code:



Merge2PDF.java:



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techzoop.mergepdf;

/**
 *
 * @author ARPAN
 */
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 Merge2PDF {

    public static void main(String[] arg) {
        try {
            List<InputStream> pdfList = new ArrayList<InputStream>();
// Path of the 1st pdf that you want to merge
            pdfList.add(new FileInputStream("C:\\Users\\USER\\Desktop\\11\\1.pdf"));
            // Path of the 2nd pdf that you want to merge
pdfList.add(new FileInputStream("C:\\Users\\USER\\Desktop\\11\\2.pdf"));
// Output file path
            OutputStream output = new FileOutputStream("C:\\Users\\USER\\Desktop\\11\\New_Merged.pdf");
         
// This is the boolean flag that represents whether you need to include page number or not
            Merge2PDF.mergepdfList(pdfList, output, true);
        } catch (Exception ex) {
            System.err.println("Exception occurs : " + ex.getMessage());
        }
    }

    public static void mergepdfList(List<InputStream> streamOfPDFFiles,
            OutputStream outputStream, boolean paginate) {
// Creating new object of Document
        Document document = new Document();

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

            // Create Readers for the pdfList.
            while (iteratorpdfList.hasNext()) {
                InputStream pdf = iteratorpdfList.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 bsfont = 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(bsfont, 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());
            }
        }
    }
}



Library required:


For running this code properly the following library file is required. Click the bellow library file name to download the library.

itext-2.1.4.jar




Share on Google Plus

About Techzoop

    Google+ Comment
    Facebook Comment

0 comments:

Post a Comment