ADVERTISEMENTS

How to generate dynamic pdf file in java application

Nitasha BatraSep 18, 2017
How to generate dynamic pdf file in java application

We can create the pdf file using iText library. The iText library is supports the generation of HTML, PDF, RTF, and XML documents. It contains classes to generate the PDF file in different font, add water marks and to generate table in the PDF file. For creating the simple pdf file we use the Eclipse IDE. Firstly download the eclipse and after that download the iText jar file.

ADVERTISEMENTS

Here are steps :

  • Firstly create a new Java project in Eclipse named PDFGeneration
  • Right click on the PDF Generation project in package explorer view and select properties.
  • Click on Java Build Path. On the library tab, click add external jars.
  • Browse and select the itextpdf-5.3.4.jar file
  • Click Ok
  • Know right click on src package of PDF Generation project then create a new java class name CraetePDfFile.
  • After that create the main method in this class. The declaration of main method must be:
    public static void main(static arg[]) {
     ... 
     }
  • Now firstly create the instantiate of the Document object. The com.itextpdf.text.Document class in IText represents a PDF document. If you need to generate a PDF document from scratch, you will use the Document class. Firstly create the instance of object.
  • Document document = new Document(PageSize.A4, 50, 50, 50, 50);
  • The first argument is the page size. The next arguments are left, right, top, and bottom margins, respectively.
  • Second, a PDFWriter is created, passing the Document instance and an OutputStream to its constructor. The Document instance is the document we are currently adding content to. The OutputStream is where the generated PDF document is written to. In this example the PDF document is written to a file, using a FileOutputStream.
    PdfWriter writer = PdfWriter.getInstance(document, new   FileOutputStream("D:\\Java Setup\\ITextTest.pdf"));
  • Now the document is opened by calling document.open(). Now you can add content to the Document instance.
  • The com.itextpdf.text.Paragraph class in IText represents a "paragraph" of text. In a paragraph we can set the paragraph alignment, indentation and spacing before and after the paragraph. Now create the paragraph object by calling
     Paragraph paragraph1 = new Paragraph();
  • Then add the paragraph instance in document.
    document.add(paragraph1);
  • Font Factory is used to set the font by calling this method
    FontFactory.GetFont(fontName,Size,style,color);
  • Then the Document instance is closed, by calling document.close(). It is important to close the document, to flush all content in the document to the PDFWriter.
ADVERTISEMENTS

Here is a full code example that generates a very simple PDF document using IText:

import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.CMYKColor;
import com.itextpdf.text.pdf.PdfWriter;

public class pdfFile {
   public static void main(String arg[]) throws FileNotFoundException, DocumentException
   {
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\Java Setup\\ITextTest.pdf"));
			document.open();
		      Paragraph paragraph1 = new Paragraph();
		      paragraph1.setSpacingBefore(100);
		      document.add(paragraph1);
		document.add(new Paragraph("Somejghj more text on the first page with different color and font type.", 
		FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new CMYKColor(0, 255, 0, 0))));
		 document.close(); 
   }
}

 

ADVERTISEMENTS