Merge & Split PDF files using i. Text JARIn previous article about Generating PDF files using i. Text JAR, Kiran Hegde had described a nice and basic way of generating PDF files in Java using i. Test JAR. It is a great starter tutorial for those who wants to start working with i. Text. In one of the requirement, I had to merge two or more PDF files and generate a single PDF file out of it.
You can merge multiple PDF files into one document if, for instance, your scanner produced individual pages of a document while scanning, or you have multiple documents that would just work better as a single file. I found a much faster drag and drop option that allows you to split up a pdf document, by dragging a page or multiple pages into any folder you want. I'm using Adobe Acrobat Pro 9:-Go to View--> Navigation. Simpo PDF Merge and Split combines a PDF merger and a PDF spliter. Sep-Oct, 1998 Blatant Opportunist 52.3 Copyright c 1998 by Don Lancaster and Synergetics (520) 428-4073 www.tinaja.com All commercial rights and all electronic media rights fully reserved. Reposting expressly forbidden.
I thought of implementing the functionality from scratch in i. Text, 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 i. Text 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 i. Text JARSo here we go.
First let us see the code. File. Input. Stream. File. Output. Stream. IOException. import java. Input. Stream. import java.
Output. Stream. import java. Array. List. import java. Iterator. import java.
List. import com. Document. import com. Base. Font. import com.
Pdf. Content. Byte. Pdf. Imported. Page. Pdf. Reader. import com. Pdf. Writer. public class Merge. PDF . In main() method, we create a List of Input.
Stream objects that points to all the input PDF files we need to merge. We call Merge. PDF. PDFs() static method passing list of input PDFs, Output. Stream 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.
In concat. PDFs() method, first we convert List of Input. Stream objects to List of Pdf. Reader objects in first while loop. And also we keep count of the total pages in all the input PDF files. Next we create Base.
Font object using Base. Font. create. Font() method. This will be the font for writing page numbers. Next we create output objects to write our merged PDF file using Document class object and Pdf. Writer. get. Instance() method. 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.
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 i. Text JARLet us see the code./**. Stream Input PDF file. Stream Output PDF file. Page start page from input PDF file.
Page end page from input PDF file. PDF(Input. Stream input.
Stream. Output. Stream output. Stream, int from. Page, int to. Page) . 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.