Tech and Media Labs
This site uses cookies to improve the user experience.




Java IText: Document

Jakob Jenkov
Last update: 2014-05-24

The com.itextpdf.text.Document class in IText represents a PDF document. It is one of the core classes in IText. If you need to generate a PDF document from scratch, you will use the Document class.

First you must create a Document instance. Then you must open it. After that you add content to the document. Finally you close the Document instance.

Here is a simple code example:

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;

public class DocumentExample {

    public static void main(String[] args) {

        Document document = new Document();

        try {
            document.open();

            document.add(new Paragraph("A Hello World PDF document."));

            document.close(); // no need to close PDFwriter?

        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }
}

Notice how no PDFWriter is created in this example. If you want to flush the generated document to an OutputStream (file, network etc.), you must use a PDFWriter. There is an example of how to use a PDFWriter, in the Getting Started text.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC