Monday, January 25, 2010

iText tutorial: Merge & Split PDF files using iText JAR

In previous article about Generating PDF files using iText JAR, Kiran Hegde had described a nice and basic way of generating PDF files in Java using iTest JAR. It is a great starter tutorial for those who wants to start working with iText.
In one of the requirement, I had to merge two or more PDF files and generate a single PDF file out of it. I thought of implementing the functionality from scratch in iText, but then thought to google it and see if already someone have written code for what I was looking for.

As expected, I got a nice implementation of java code that merges 2 or more PDF files using iText jar. I thought of dissecting the code in this post and give credit to original author of the post.
Merge PDF files in Java using iText JAR

So here we go. First let us see the code.
view source
print?

001 package net.viralpatel.itext.pdf;
002
003 import java.io.FileInputStream;
004 import java.io.FileOutputStream;
005 import java.io.IOException;
006 import java.io.InputStream;
007 import java.io.OutputStream;
008 import java.util.ArrayList;
009 import java.util.Iterator;
010 import java.util.List;
011
012 import com.lowagie.text.Document;
013 import com.lowagie.text.pdf.BaseFont;
014 import com.lowagie.text.pdf.PdfContentByte;
015 import com.lowagie.text.pdf.PdfImportedPage;
016 import com.lowagie.text.pdf.PdfReader;
017 import com.lowagie.text.pdf.PdfWriter;
018
019 public class MergePDF {
020
021 public static void main(String[] args) {
022 try {
023 List pdfs = new ArrayList();
024 pdfs.add(new FileInputStream("c:\\1.pdf"));
025 pdfs.add(new FileInputStream("c:\\2.pdf"));
026 OutputStream output = new FileOutputStream("c:\\merge.pdf");
027 MergePDF.concatPDFs(pdfs, output, true);
028 } catch (Exception e) {
029 e.printStackTrace();
030 }
031 }
032
033 public static void concatPDFs(List streamOfPDFFiles,
034 OutputStream outputStream, boolean paginate) {
035
036 Document document = new Document();
037 try {
038 List pdfs = streamOfPDFFiles;
039 List readers = new ArrayList();
040 int totalPages = 0;
041 Iterator iteratorPDFs = pdfs.iterator();
042
043 // Create Readers for the pdfs.
044 while (iteratorPDFs.hasNext()) {
045 InputStream pdf = iteratorPDFs.next();
046 PdfReader pdfReader = new PdfReader(pdf);
047 readers.add(pdfReader);
048 totalPages += pdfReader.getNumberOfPages();
049 }
050 // Create a writer for the outputstream
051 PdfWriter writer = PdfWriter.getInstance(document, outputStream);
052
053 document.open();
054 BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
055 BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
056 PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
057 // data
058
059 PdfImportedPage page;
060 int currentPageNumber = 0;
061 int pageOfCurrentReaderPDF = 0;
062 Iterator iteratorPDFReader = readers.iterator();
063
064 // Loop through the PDF files and add to the output.
065 while (iteratorPDFReader.hasNext()) {
066 PdfReader pdfReader = iteratorPDFReader.next();
067
068 // Create a new page in the target for each source page.
069 while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { 070 document.newPage(); 071 pageOfCurrentReaderPDF++; 072 currentPageNumber++; 073 page = writer.getImportedPage(pdfReader, 074 pageOfCurrentReaderPDF); 075 cb.addTemplate(page, 0, 0); 076 077 // Code for pagination. 078 if (paginate) { 079 cb.beginText(); 080 cb.setFontAndSize(bf, 9); 081 cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" 082 + currentPageNumber + " of " + totalPages, 520, 083 5, 0); 084 cb.endText(); 085 } 086 } 087 pageOfCurrentReaderPDF = 0; 088 } 089 outputStream.flush(); 090 document.close(); 091 outputStream.close(); 092 } catch (Exception e) { 093 e.printStackTrace(); 094 } finally { 095 if (document.isOpen()) 096 document.close(); 097 try { 098 if (outputStream != null) 099 outputStream.close(); 100 } catch (IOException ioe) { 101 ioe.printStackTrace(); 102 } 103 } 104 } 105 } If you see what the code does is pretty simple. 1. In main() method, we create a List of InputStream objects that points to all the input PDF files we need to merge 2. We call MergePDF.concatPDFs() static method passing list of input PDFs, OutputStream object for merged output PDF and a boolean flag that represents whether you need to include page numbers at the end of each page as command line arguments 3. In concatPDFs() method, first we convert List of InputStream objects to List of PdfReader objects in first while loop. And also we keep count of the total pages in all the input PDF files. 4. Next we create BaseFont object using BaseFont.createFont() method. This will be the font for writing page numbers 5. Next we create output objects to write our merged PDF file using Document class object and PdfWriter.getInstance() method 6. Finally we write all the input PDFs into merged output PDF iterating each PDF and then writing each page of it in two while loops 7. And then, close all the streams and clear all the buffers. Good boys do this ;-) So now we know how to merge PDF files into one, let us see the way to split a PDF file or extract a part of PDF into another PDF. Split PDF files in Java using iText JAR Let us see the code. view source print? 01 /** 02 * @author viralpatel.net 03 * 04 * @param inputStream Input PDF file 05 * @param outputStream Output PDF file 06 * @param fromPage start page from input PDF file 07 * @param toPage end page from input PDF file 08 */ 09 public static void splitPDF(InputStream inputStream, 10 OutputStream outputStream, int fromPage, int toPage) { 11 Document document = new Document(); 12 try { 13 PdfReader inputPDF = new PdfReader(inputStream); 14 15 int totalPages = inputPDF.getNumberOfPages(); 16 17 //make fromPage equals to toPage if it is greater 18 if(fromPage > toPage ) {
19 fromPage = toPage;
20 }
21 if(toPage > totalPages) {
22 toPage = totalPages;
23 }
24
25 // Create a writer for the outputstream
26 PdfWriter writer = PdfWriter.getInstance(document, outputStream);
27
28 document.open();
29 PdfContentByte cb = writer.getDirectContent(); // Holds the PDF data
30 PdfImportedPage page;
31
32 while(fromPage <= toPage) { 33 document.newPage(); 34 page = writer.getImportedPage(inputPDF, fromPage); 35 cb.addTemplate(page, 0, 0); 36 fromPage++; 37 } 38 outputStream.flush(); 39 document.close(); 40 outputStream.close(); 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } finally { 44 if (document.isOpen()) 45 document.close(); 46 try { 47 if (outputStream != null) 48 outputStream.close(); 49 } catch (IOException ioe) { 50 ioe.printStackTrace(); 51 } 52 } 53 } In above code, we have created a method splitPDF () that can be used to extracts pages out of a PDF and write it into another PDF. The code is pretty much self explanatory and is similar to the one to merge PDF files. Thus, if you need to split an input.pdf (having 20 pages) into output1.pdf (1-12 pages of input.pdf) and output2.pdf (13-20 of input.pdf), you can call the above method as follow: view source print? 01 public static void main(String[] args) { 02 try { 03 MergePDF.splitPDF(new FileInputStream("C:\\input.pdf"), 04 new FileOutputStream("C:\\output1.pdf"), 1, 12); 05 MergePDF.splitPDF(new FileInputStream("C:\\input.pdf"), 06 new FileOutputStream("C:\\output2.pdf"), 13, 20); 07 08 } catch (Exception e) { 09 e.printStackTrace(); 10 } 11 } Feel free to bookmark the code and share it if you feel it will be useful to you sumber: http://viralpatel.net/blogs/2009/06/itext-tutorial-merge-split-pdf-files-using-itext-jar.html