Wednesday, 21 December 2011

iText PDF Generation in java


iText is a freely available Java library from Lowagie.com (see Resources). The iText library is powerful and supports the generation of HTML, RTF, and XML documents, in addition to generating PDFs. You can choose from a variety of fonts to be used in the document. Also, the structure of iText allows you to generate any of the above-mentioned types of documents with the same code.

The iText library contains classes to generate PDF text in various fonts, generate tables in PDF document, add watermarks to pages, and so on. There are many more features available with iText. It would not be possible to demonstrate all of them in a single article. We will cover the basics required for PDF generation.

For this application you need following jars file
Text-2.1.6.jar
iText-rtf-2.1.6.jar
Text-rups-2.1.6.jar

PDFGeneration.java
  1. /** 
  2.  * File Name         :  PDFGeneration.java 
  3.  * Created By        :  Konda Reddy 
  4.  * Created Date      :  Dec 20, 2011 
  5.  * Purpose           :  Generation of PDF Through iText api 
  6.  */  
  7. package com.lkr.iTextPDF;  
  8.   
  9. /** 
  10.  * @author LKR 
  11.  * 
  12.  */  
  13. import java.io.FileOutputStream;  
  14. import java.io.IOException;  
  15. import com.lowagie.text.*;  
  16. import com.lowagie.text.pdf.PdfWriter;  
  17.   
  18. public class PDFGeneration {  
  19.  /** 
  20.   * Generates a PDF file with the itext API 
  21.   *  
  22.   * @param args 
  23.   *            no arguments needed here 
  24.   */  
  25.  public static void main(String[] args) {  
  26.     
  27.   System.out.println("---> resulting PDF: PDFGenerationExample");  
  28.   // step 1: creation of a document-object  
  29.   Document document = new Document();  
  30.   try {  
  31.    // step 2: we create a writer  
  32.    PdfWriter.getInstance(document,new FileOutputStream("pdfs/PDFGenerationExample.pdf"));  
  33.      
  34.    /** Setting margins to document*/  
  35.    document.setMarginMirroring(true);  
  36.    document.open();  
  37.    // step 4: we add a paragraph to the document  
  38.    document.add(new Paragraph(  
  39.        "The left margin of this odd page is 36pt (0.5 inch); the right margin 72pt (1 inch); the top margin 108pt (1.5 inch); the bottom margin 180pt (2.5 inch)."));  
  40.    Paragraph paragraph = new Paragraph();  
  41.    paragraph.setAlignment(Element.ALIGN_JUSTIFIED);  
  42.    for (int i = 0; i < 20; i++) {  
  43.     paragraph.add("Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea, Hello Land, Hello People. ");  
  44.    }  
  45.    document.add(paragraph);  
  46.    document.add(new Paragraph(  
  47.        "The right margin of this even page is 36pt (0.5 inch); the left margin 72pt (1 inch)."));  
  48.   
  49.   
  50.   } catch (DocumentException de) {  
  51.    System.err.println(de.getMessage());  
  52.   } catch (IOException ioe) {  
  53.    System.err.println(ioe.getMessage());  
  54.   }  
  55.    
  56.   // step 5: we close the document  
  57.   document.close();  
  58.  }  
  59. }  

No comments:

Post a Comment