Generating PDF files in today’s enterprise applications is quite common. Doing this with Java is not an easy task as Java does not gives default api’s to handle PDF files. No worries, iText jar is for you.
iText is a free Java-PDF library that allows you to generate PDF files on the fly (dynamically). iText is an ideal library for developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation. iText is not an end-user tool. Typically you won’t use it on your Desktop as you would use Acrobat or any other PDF application. Rather, you’ll build iText into your own applications so that you can automate the PDF creation and manipulation process.
iText core: iText-2.1.5.jar
In above code snippet we have created Document object which represents our PDF document. Also, by supplying OutputStream object to getInstance() method sends the output to OutputStream. Thus in our case we have created a output file and sent output to it.
iText is a free Java-PDF library that allows you to generate PDF files on the fly (dynamically). iText is an ideal library for developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation. iText is not an end-user tool. Typically you won’t use it on your Desktop as you would use Acrobat or any other PDF application. Rather, you’ll build iText into your own applications so that you can automate the PDF creation and manipulation process.
iText (Java-PDF Library) can be used to:
- Serve PDF to a browser
- Generate dynamic documents from XML files or databases
- Use PDF’s many interactive features
- Add bookmarks, page numbers, watermarks, etc.
- Split, concatenate, and manipulate PDF pages
- Automate filling out of PDF forms
- Add digital signatures to a PDF file
Technical Requirements to use iText
You should have JDK 1.4 or later to integrate iText PDF generation in your application.Getting iText
Download iText jar from its home page http://www.lowagie.com/iText/download.htmliText core: iText-2.1.5.jar
Generate simple PDF in Java using free Java-PDF library
It is very easy to generate a simple PDF file in Java using iText. All you have to do is to put itext.jar in your class path and paste following code in GeneratePDF.java class and compile and execute it. After you execute this, a file Test.pdf will be created in C: drive (If you are using Linux, you may want to have /usr/test.pdf as path).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; //Now in some class write this function public static void generatePDF () { 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(); } }
In above code snippet we have created Document object which represents our PDF document. Also, by supplying OutputStream object to getInstance() method sends the output to OutputStream. Thus in our case we have created a output file and sent output to it.
nice
ReplyDelete