Friday, April 3, 2009

How to generate PDF report from Java

1. Download iText-2.1.5.jar (You can google and get it)
2. Demo.java

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

public class Demo {
public static void main(String[] args) {
new Demo().createPDF();
}
public void createPDF(){
Document d = new Document (PageSize.A4);
try {
PdfWriter.getInstance(d, new FileOutputStream("sample.pdf"));
d.open ();
Paragraph p = new Paragraph ("Binod Kumar Suman,\n Bangalore, India");
d.add (p);
d.close ();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
System.out.println("******** PDF Created ***************");
}
}

Now check, you will get one pdf file sample.pdf

3 comments:

  1. Could you please provide RSS facility?

    ReplyDelete
  2. Hello Binod,
    Can you help me regarding a problem??
    That Problem is " how to display an image stored as blob type in mysql through iText pdf in JSP/Servlet"

    ReplyDelete

You can put your comments here (Either feedback or your Question related to blog)