Thursday, April 26, 2012

Create/Manipulate PDF files using JAVA

Hi Guys,

              Do you know that we can generate the PDF files on the fly in JAVA. To achieve this we need to use the iText.jar file. Just set the iText.jar file to your class path and use below program to generate a PDF file.
Knowledge Base : http://www.lowagie.com/iText/download.html.
Download Link  : http://sourceforge.net/projects/itext/files/

Sample Program
-----------------
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class GeneratePDF {
    public static void main(String[] args) {
        try {
            OutputStream file = new FileOutputStream(new File("C:\\Test.pdf"));
            Document document = new Document();
            PdfWriter.getInstance(document, file);
            document.open();
            document.add(new Paragraph("Hello Kiran"));
            document.add(new Paragraph(new Date().toString()));
            document.close();
            file.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 A Test.pdf file will be generated in " C:\\" drive.

No comments:

Post a Comment