itext

Getting started with itext

Remarks#

If you look at PDF creation, you’ll find two different approaches:

  • Graphical designers use desktop applications such as Adobe Acrobat or Adobe InDesign to create a document in a manual or semimanual process.
  • In another context, PDF documents are created programmatically, using an API to produce PDFs directly from software applications, without —or with minimal— human intervention. Sometimes the document is created in an intermediary format first (e.g. XML, HTML,…), and then converted to PDF.

These different approaches demand different software products.

The same goes for PDF manipulation.

  • You can update a PDF manually in tools such as Adobe Acrobat,
  • There are also tools that allow forms to be filled out automatically based on information from a database.

iText is a tool that focuses on the automation side of things.

What is iText?

iText is an SDK that was developed to allow developers to do the following (and much more):

  • Generate documents and reports based on data from an XML file or a database
  • Create maps and books, exploiting numerous interactive features available in PDF
  • Add bookmarks, page numbers, watermarks, and other features to existing PDF documents
  • Split or concatenate pages from existing PDF files
  • Fill out interactive forms
  • Digitally sign PDF documents
  • Serve dynamically generated or manipulated PDF documents to a web browser

iText is not an end-user tool. You have to build iText into your own applications so that you can automate the PDF creation and manipulation process.

When to use iText?

Typically, iText is used in projects that have one of the following requirements:

  • The content isn’t available in advance: it’s calculated based on user input or real-time database information.
  • The PDF files can’t be produced manually due to the massive volume of content: a large number of pages or documents.
  • Documents need to be created in unattended mode, in a batch process.
  • The content needs to be customized or personalized; for instance, the name of the end user has to be stamped on a number of pages.

Often you’ll encounter these requirements in web applications, where content needs to be served dynamically to a browser. Normally, you’d serve this information in the form of HTML, but for some documents, PDF is preferred over HTML for better printing quality, for identical presentation on a variety of platforms, for security reasons, to comply with specific industry standards (such as PAdES, PDF/A, or PDF/UA), or to reduce the file size.

Versions#

VersionFirst releaseLatest releaseEnd-of-Life
0.30 - 0.992000-02-142003-05-012005-12-31
1.00 - 1.4.82003-06-252006-12-192009-12-31
2.00 - 2.1.72003-02-152009-07-072012-12-31
5.0.0 - 5.5.112009-12-072017-03-202018-12-31
7.0.0 - ...2016-05-03...2025-12-31

Installation or Setup

iText for Java

Importing the iText jars from the Central Maven Repository is the best way to install iText 7. These simple videos explain how to do this using different IDEs:

In these tutorials, we only define the kernel and the layout projects as dependencies. Maven also automatically imports the io jar because the kernel packages depend on the io packages.

This is the basic list of dependencies for standard use of iText 7:

<dependencies>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>kernel</artifactId>
        <version>7.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>io</artifactId>
        <version>7.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>forms</artifactId>
        <version>7.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdfa</artifactId>
        <version>7.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdftest</artifactId>
        <version>7.0.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.18</version>
    </dependency>
</dependencies>

Every dependency corresponds with a jar in Java and with a DLL in C#.

  • kernel and io: contain low-level functionality.
  • layout: contains high-level functionality.
  • forms: needed for all the AcroForm examples.
  • pdfa: needed for PDF/A-specific functionality.
  • pdftest: needed for the examples that are also a test.

For more specific use of iText 7, you may need additional jars:

  • barcodes: use this if you want to create bar codes.
  • hyph: use this if you want text to be hyphenated.
  • font-asian: use this is you need CJK functionality (Chinese / Japanese / Korean)
  • sign: use this if you need support for digital signatures.

All the jars listed above are available under the AGPL license. You can also download these jars in a ZIP-file hosted on Github: https://github.com/itext/itext7/releases

If you want to use these jars, you have to add them to your CLASSPATH, just like you would add any other jar.

Additional iText 7 functionality is available through add-ons, which are delivered as jars under a commercial license. If you want to use any of these add-ons, or if you want to use iText 7 with your proprietary code, you need to obtain a commercial license key for iText 7 (see the legal section of the iText web site).

You can import such a license key using the license-key module. You can get the license-key jar like this:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-licensekey</artifactId>
    <version>2.0.0</version>
    <scope>compile</scope>
</dependency>

Some functionality in iText is closed source. For instance, if you want to use PdfCalligraph, you need the typography module. This module won’t work without an official license key.

iText for C#

You can download a ZIP-file containing all the DLLs that are available under the AGPL. For more info about these DLLs, please read the Java documentation.

Hello World

This is a very simple program to create a PDF using iText 7 / Java:

//Initialize writer
PdfWriter writer = new PdfWriter(dest);

//Initialize document
PdfDocument pdfDoc = new PdfDocument(writer);
Document doc = new Document(pdfDoc);

//Add paragraph to the document
doc.add(new Paragraph("Hello World!"));

//Close document
doc.close();

(Listing_01_01_HelloWorld.java)

You can navigate to many other examples from that page.


And this is a very simple program to create a PDF using the precursor iText 5.5.x / Java:

// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World!"));
// step 5
document.close();

(HelloWorld.java)

There are many more examples to navigate from this page, too.


These two examples look pretty similar. The advantages of the re-designed iText 7 API will become apparent, though, as soon as one starts to look closer at less trivial examples. Thus, simply navigate through the example source code from the links above and compare.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow