PDF Generation in Java: Overview of iText, Apache PDFBox, OpenPDF Libraries

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

PDF Generation in Java: Overview of Popular Libraries



Creating PDF documents is one of the common tasks in corporate and educational Java development. Reports, invoices, certificates, educational materials — all of these are conveniently exported to PDF. Java has several mature libraries that allow both creating PDF from scratch and editing existing files. In this article, we will look at three of the most popular tools: iText, Apache PDFBox, and OpenPDF.



1. iText — Powerful but with Licensing Restrictions



iText is the most well-known Java library for working with PDF. It has been around since 2000 and supports almost all format capabilities: text, tables, images, watermarks, annotations, signatures, and much more.



However, iText has an important nuance: starting from version 5.5.x, the library is released under a dual license. Commercial use requires purchasing an AGPL license if you do not distribute your application with open source code. For educational projects hosted in open access, the iText 5.x version under AGPL is suitable.



Example of creating a simple PDF with text and a table using iText 5:



import com.itextpdf.text.*;import com.itextpdf.text.pdf.PdfPTable;import com.itextpdf.text.pdf.PdfWriter;import java.io.FileOutputStream;

public class ITextExample { public static void main(String[] args) throws Exception { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("example_itext.pdf")); document.open();

// Title document.add(new Paragraph("PDF Example with iText", FontFactory.getFont(FontFactory.HELVETICA_BOLD, 18)));

// Table PdfPTable table = new PdfPTable(3); table.addCell("Column 1"); table.addCell("Column 2"); table.addCell("Column 3"); table.addCell("Data 1"); table.addCell("Data 2"); table.addCell("Data 3"); document.add(table);

document.close(); System.out.println("PDF created: example_itext.pdf"); }}


2. Apache PDFBox — Free and Open Source



Apache PDFBox is a completely free library under the Apache 2.0 license. It has no restrictions on commercial use and provides a low-level API for working with PDF. PDFBox allows you to create documents, extract text, fill forms, work with images, and even create complex layouts.



The main advantage of PDFBox is its flexibility and absence of licensing fees. The disadvantage is that the API is less convenient for simple tasks than iText and requires more code for formatting.



Example of creating a PDF with text and a table using PDFBox 2.x:



import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.pdmodel.PDPage;import org.apache.pdfbox.pdmodel.PDPageContentStream;import org.apache.pdfbox.pdmodel.font.PDType1Font;import org.apache.pdfbox.pdmodel.common.PDRectangle;import java.io.IOException;

public class PDFBoxExample { public static void main(String[] args) throws IOException { try (PDDocument document = new PDDocument()) { PDPage page = new PDPage(PDRectangle.A4); document.addPage(page);

try (PDPageContentStream cs = new PDPageContentStream(document, page)) { // Title cs.setFont(PDType1Font.HELVETICA_BOLD, 18); cs.beginText(); cs.newLineAtOffset(50, 750); cs.showText("PDF Example with PDFBox"); cs.endText();

// Simple table (simulation) cs.setFont(PDType1Font.HELVETICA, 12); cs.setLineWidth(1f); // Draw table lines manually (simplified) for (int i = 0; i < 4;

Recommendations