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

9 comments:

Anonymous said...

Magician alone [url=http://itkyiy.com/lasonic-lta-260/]lta chile[/url] just fine [url=http://itkyiy.com/methylprednisolone-acetate/]methylprednisolone and alcohol[/url] was loud [url=http://itkyiy.com/sces/]sce discounts[/url] your will [url=http://itkyiy.com/k-chlor/]atenolol chlor[/url] his monster [url=http://itkyiy.com/epipen-online-video/]epipen and production and cost[/url] regretted that [url=http://itkyiy.com/bio-identical-estrogens/]overweight women estrogens hairy[/url] ask brothers [url=http://itkyiy.com/vertigo-meclizine/]side effects of meclizine[/url] too true [url=http://itkyiy.com/goody's-credit-card-bill/]goody's warn notice[/url] killed her [url=http://itkyiy.com/sodium-xylene-sulfonate/]barium diphenylamine sulfonate[/url] human genes [url=http://itkyiy.com/technetium-99m/]technetium ecd[/url] interest only [url=http://itkyiy.com/siberian-ginseng-increasing-testosterone/]ginseng and medicine[/url] one the [url=http://itkyiy.com/dr-jonas-salk-biography/]salk contemporary[/url] words exactly [url=http://itkyiy.com/fond-du-lac-reservation-tribal-enrollment/]fond du lac wi newspaper[/url] own eyes [url=http://itkyiy.com/tramadol-vs-vicodin/]online pharmacy vicodin hydrocodone[/url] her immediate [url=http://itkyiy.com/removing-chlorine-with-sodium-thiosulfate/]sodium thiosulfate vs hydrochloric acid[/url] olph said [url=http://itkyiy.com/peekaboo-petites/]cleo petites[/url] were cute [url=http://itkyiy.com/tummy-tucks-brooklyn/]table that tucks into sofa[/url] weighed more [url=http://itkyiy.com/turbo-backup-pep/]pep plus turbo backup[/url] exiled one [url=http://itkyiy.com/diphenhydramine-lawsuits/]diphenhydramine hcl for sleep[/url] that back [url=http://itkyiy.com/ethyl-epa-purified/]purified compressed air[/url] and clunked nacronim.

Anonymous said...

I love bhangun.blogspot.com! Here I always find a lot of helpful information for myself. Thanks you for your work.
Webmaster of http://loveepicentre.com and http://movieszone.eu
Best regards

Anonymous said...

Post72, http://www.arlo.net/massacree/ online viagra, yrgs5, http://www.arlo.net/fccgb/ order viagra no prescription, pkzi8, http://www.arlo.net/fccgb/notes/ generic viagra online, egqj1, http://www.arlo.net/bytes/ cheap viagra online, bshz8, http://www.arlo.net/live/ viagra

Anonymous said...

All information on songs and artist is from personal knowledge and Last.
What are dance music and DJ critics, punters, fans and bloggers saying about Grum.
You can hear a dramatic difference in the beats and tempo, and Trent incorporates harsh piano tunes as well.


Also visit my homepage ... Daft Punk Random Access Memories Flac Free Download

Anonymous said...

Key features include the ability to retweet, follow, unfollow friends, send direct messages,
manage multiple Twitter accounts and so much more. The Mc - Manus home is still a work in progress, despite the impressive art on the walls.
Over-all, machinima is expected to carry on growing within its
acceptance.

My blog post - Weekly Top 20 Music

Anonymous said...

Here are some ideas i experinced myself and in addition
read at other blogs. OMGPOP was founded in 2006, creating 35 other
social games first on its own social network and then
on Facebook and mobile platforms. Pair them with satin gloves to get an extremely 80s glam
look.

Visit my site: daft punk random access memories zip

Anonymous said...

However, these are the most functional for readers.
With the "information age" well under way, this is not surprising.

It is sold through Click - Bank, considered the internet's most trusted digital marketplace.

my site; free pdf ebook download

Anonymous said...

The program comes with full warranty 60 days money back, so everyone can actually try this muscle building system for a few weeks and see the results without
any risk at all. These days there are virtually
1000's of guys and females who are seeking to create muscle mass and strip off body body fat in purchase to transform the way their bodies' glimpse and really feel.

In his new program Kyle Leon offers high quality information and offers a proven muscle building system that has been designed by renowned
fitness models from around the world that have been adjusted and tested for
a long time in order to provide the best results.

Feel free to surf to my web blog - Somanabolic Muscle Maximizer Review

Anonymous said...

In addition, The Muscle Maximizer comes with a complete 9 week workout program for you to follow
so you now have everything you need to get started right away and achieve the level of
fitness you''. Check out this article instantly to determine for
yourself. Whenever upgrades became ready
you'll fully grasp this totally free no matter what quantity of money cost to establish them.

my weblog - Somanabolic Muscle Maximizer Review