Tutorials     About     RSS
Tech and Media Labs

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.add(new Paragraph("A Hello World PDF document."));

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

        } catch (DocumentException e) {


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

Featured Videos

Sponsored Ads

Maildroppa - Smart Email Marketing Solution
Close TOC

All Trails

Trail TOC

Page TOC