iText 7 for Java: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

iText 7 for Java: Complete Guide to Creating and Editing PDF

\n\n

iText 7 is a powerful open-source (AGPL) library for Java, designed for programmatic creation, manipulation, and rendering of PDF documents. It is the successor to the popular iText 5 library and offers a completely redesigned, modular architecture focused on performance, flexibility, and ease of use. iText 7 allows developers to solve a wide range of tasks: from generating simple invoices and reports to creating complex, legally significant documents with digital signatures, forms, and metadata.

\n\n

Why is iText 7 needed? In the modern world, PDF is a universal format for document exchange, guaranteeing consistent display on any device. Manual creation of PDF files is time-consuming and inefficient, especially when working with large volumes of data. iText 7 automates this process, giving Java developers full control over every aspect of the document: from adding text and images to creating interactive forms and managing access rights. It is indispensable in document management systems, e-commerce, banking, and any other applications requiring PDF generation or processing.

\n\n

The library supports all the latest PDF specifications, including PDF 2.0. It also provides additional modules (add-ons) for working with HTML conversion (pdfHTML), optical character recognition (pdfOCR), SVG creation (pdfSVG), and image processing (pdfImage). It is important to note that iText 7 is distributed under a dual license: AGPL (requires open-sourcing your application) and commercial (for proprietary use).

\n\n

Installation

\n

To install iText 7 in your Java project, use the Maven or Gradle build system. Below is the exact command for adding dependencies to the pom.xml file (Maven).

\n\n
<!-- pom.xml -->\n<dependencies>\n    <!-- Main iText 7 module -->\n    <dependency>\n        <groupId>com.itextpdf</groupId>\n        <artifactId>itext7-core</artifactId>\n        <version>8.0.4</version> <!-- Check the latest version on Maven Central -->\n        <type>pom</type>\n    </dependency>\n</dependencies>
\n\n

For Gradle (build.gradle):

\n
implementation 'com.itextpdf:itext7-core:8.0.4'
\n\n

If you only need specific modules (e.g., kernel, layout, io), you can add them individually:

\n
<dependency>\n    <groupId>com.itextpdf</groupId>\n    <artifactId>kernel</artifactId>\n    <version>8.0.4</version>\n</dependency>\n<dependency>\n    <groupId>com.itextpdf</groupId>\n    <artifactId>layout</artifactId>\n    <version>8.0.4</version>\n</dependency>
\n\n

Quick Start: Minimal Working Example

\n

The following code creates a simple PDF document with the text "Hello, World!" and saves it to the file HelloWorld.pdf.

\n\n
import com.itextpdf.kernel.pdf.PdfDocument;\nimport com.itextpdf.kernel.pdf.PdfWriter;\nimport com.itextpdf.layout.Document;\nimport com.itextpdf.layout.element.Paragraph;\n\nimport java.io.FileNotFoundException;\n\npublic class HelloWorld {\n    public static void main(String[] args) throws FileNotFoundException {\n        // 1. Create a PdfWriter that will write data to the file\n        PdfWriter writer = new PdfWriter("HelloWorld.pdf");\n        \n        // 2. Create a PdfDocument — an object representing the PDF document itself\n        PdfDocument pdf = new PdfDocument(writer);\n        \n        // 3. Create a Document — a high-level object for adding                    

Recommendations