In this program we are going to tell you how you can read a pdf file and combine more than one pdf into one.
To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages and classes we are talking about are java.io.* for input output, com.lowagie.text.pdf.Document, com.lowagie.text.Rectangle, com.lowagie.text.pdf.BaseFonti, com.lowagie.text.pdf.PdfContentByte, com.lowagie.text.pdf.PdfImportedPage, com.lowagie.text.pdf.PdfWriter and com.lowagie.text.pdf.PdfReader.
Now create a file named Comboine2page. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.
First of all create a PdfReader object for reading the pdf file. By the object of PdfReader we can get the number of pages in the pdf file. Now make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content.
Now create a document writer that writes the equivalent syntax for a document's content to a specific OutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream.
Now open the document by document.open(). By the object of the PdfWriter we get the reference of PdfContentType. It will be obtained by adding the method getDirectContent() to the previously generated PdfWriter object. At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance.
The code of the program is given below:
The output of the program is given below:
Liked it! Share this Tutorial