Tuesday, October 9, 2012

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

0 comments:

Post a Comment