combine two pdf files

In this section, you will see how to combine two pdf files in java.

combine two pdf files


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* 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 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:

import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;

public class Comboine2page {
  public static void main(String[] args)throws Exception {
  System.out.println("Combines two page");  
    PdfReader reader = new PdfReader("HelloWorldPdf.pdf");  
  int n = reader.getNumberOfPages(); 
  Rectangle psize = reader.getPageSize(1);
  float width = psize.height();
  float height = psize.width();  
 Document document = new Document(new Rectangle(width, height)); 
  PdfWriter Pdfwriter = PdfWriter.getInstance(document, 
 PdfContentByte cb = Pdfwriter.getDirectContent();
 int i = 0;
 int p = 0;
 while (i < n) {
 PdfImportedPage page1 = Pdfwriter.getImportedPage(reader, i);
 cb.addTemplate(page1, .5f00.5f60120);
 if (i < n) {
 PdfImportedPage page2 = Pdfwriter.getImportedPage(reader, i);
 cb.addTemplate(page2, .5f00.5f, width / 60120);
 BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, 
 cb.setFontAndSize(bf, 19);
 cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p 
" of " ((n / 2(n % 00)), width / 2400);


The output of the program is given below:

Download this example.


Share on Google+Share on Google+

combine two pdf files

Posted on: April 17, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: combine two pdf files  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
pradip garala
November 12, 2011
pdf merge in java

thanks for giving such a helpful code....