Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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 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:

import java.io.FileOutputStream;
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, 
new 
FileOutputStream("combine2Page1.pdf"));
   document.open();
            
   PdfContentByte cb = Pdfwriter.getDirectContent();
   int i = 0;
   int p = 0;
   while (i < n) {
   document.newPage();
   p++;
   i++;
   PdfImportedPage page1 = Pdfwriter.getImportedPage(reader, i);
   cb.addTemplate(page1, .5f00.5f60120);
   if (i < n) {
   i++;
   PdfImportedPage page2 = Pdfwriter.getImportedPage(reader, i);
   cb.addTemplate(page2, .5f00.5f, width / 60120);
   }
   BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, 
BaseFont.CP1252,BaseFont.NOT_EMBEDDED
);
   cb.beginText();
   cb.setFontAndSize(bf, 19);
   cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p 
" of " ((n / 2(n % 00)), width / 2400);
   cb.endText();
   }
   document.close();    
    }
}

 

The output of the program is given below:

Download this example.

 

                         

» View all related tutorials
Related Tags: c web pdf file stl text application import jar diff io make packages port this package itext app if tex

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.