Archive for November 2011

Merging PDF files in Java

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.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
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();
  mergerUtility.addSources(sourcePDFs);
  // set the destination pdf name and merge input pdfs
  mergerUtility.setDestinationFileName("merged.pdf");
  mergerUtility.mergeDocuments();
 }
}

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 !!!

http://pdfbox.apache.org/index.html
 cheers, Munish Gogna
Saturday, November 5, 2011
Posted by Munish Gogna

Popular Post

Labels

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)