Posted by : Munish Gogna Saturday, November 5, 2011

I was just looking for some way to merge PDFs generated from different sources to one final deck. iText library was one obvious choice but when I looked into PDFBox library I was stunned with the simplicity and ease with which PDFs can be merged. I tried merging three different PDF files and to my surprise it took less than 15 lines of code as shown below:

package com.gognamunish.test;

import java.util.ArrayList;
import java.util.List;
import org.apache.pdfbox.util.PDFMergerUtility;

 * Merges pdf files into one final pdf.
 * @author Munish Gogna
public class PDFMerge {

 public static void main(String[] args) throws Exception {
  // Get the byte streams from any source (maintain order)
  List<InputStream> sourcePDFs = new ArrayList<InputStream>();
  sourcePDFs.add(new FileInputStream(new File("pdf1.pdf")));
  sourcePDFs.add(new FileInputStream(new File("pdf2.pdf")));
  sourcePDFs.add(new FileInputStream(new File("pdf3.pdf")));

  // initialize the Merger utility and add pdfs to be merged
  PDFMergerUtility mergerUtility = new PDFMergerUtility();
  // set the destination pdf name and merge input pdfs

The library is rich and provides many options out of the box that can be useful when it comes to playing with PDFs, worth a try !!!
 cheers, Munish Gogna

3 Responses so far.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post


enums (1) java (2) JAX-RS (1) JPA (1) mysql (1) request 2 (1) RESTful (1) sphinx (1) tomcat (1) web service (2) ws (2)