java search
Loading
Help improve Java Search

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.

3 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