Tuesday, November 28, 2006

PDF generation with iText

iText is a Java API for reading, writing, and editing PDF documents and PDF forms. The latest release (v1.4.7) adds support for AES encryption of documents, as well as better integration with Acrobat forms. The iText project's PDF library is perhaps the best-known open-source API to create, read, and edit PDF documents from Java. iText supports a rich set of PDF capabilities, such as the ability to select fonts, specify hyphenation, generate PDF in various page sizes, include tables and graphics in PDF documents, print page numbers, headers, and footers, use watermarks, and even supports the creation and reading of barcodes and document encryption. iText API also works with other report-oriented Java projects, such as JFree, allowing easy insertion of JFree reports or JFree charts in PDF documents.
The following example demonstrates how to create a simple PDF form using iText.
  1. Download the latest version of iText (v 1.4.7).
  2. The following piece of code can be used to generate a simple pdf with some text on it
    import java.io.FileOutputStream;
    import java.io.IOException;
    import com.lowagie.text.Document;
    import com.lowagie.text.DocumentException;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.pdf.PdfWriter;
    public class TestiText {
    public static void main(String[] args) {
    Document document = new Document();
    try {
    PdfWriter.getInstance(document, new FileOutputStream("c:\\test.pdf"));
    document.open();
    document.add(new Paragraph("iText Works :)"));
    } catch (DocumentException de) {
    de.printStackTrace();
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }
    document.close();
    }
    }
    TestiText.java
  3. Paragraph is one of the page elements in iText a listing of the page elements can be found here.

9 comments:

  1. hello,

    merging two pdfs throws java.io.IOException: PDF header signature not found exception.
    what is the reason?

    regards,
    Rakesh Parmar

    ReplyDelete
  2. same here:
    got this exception:
    .IOException: PDF header signature not found
    any tipp? thanks

    ReplyDelete
  3. Hi,

    Will u tell me how to open the generated Pdf as i didnt save anywhere ...

    and I just added a String into the document . Is this enough to generate the PDF????

    regards,

    Jai

    ReplyDelete
  4. This site is good and I found very interesting stuff here. Great job, thanks

    ReplyDelete
  5. Keep us posted with new updates. its was really valuable. thanks a lot.

    ReplyDelete
  6. You make so many great article here and I read your article a couple of times.

    ReplyDelete
  7. I’m impressed with this article, I must say this is one of the best blog!!

    ReplyDelete
  8. Thank you for this great read!! I definitely enjoy every little bit of it,

    ReplyDelete
  9. Hello there! Big thumbs up for the excellent info have got here on this post.

    ReplyDelete

Popular Posts