DEV Community

Tayyab Ali
Tayyab Ali

Posted on • Edited on

6 Java PDF Libraries: In-Depth Review for Developers

As a Java or software developer, you'll know that PDFs are an important format to work with. But did you know that there are libraries specifically designed to help you work with PDFs? This article will introduce three popular PDF libraries and show you how to use them.

If you're creating new PDF documents, these libraries will help make your job easier. Stay tuned for more Java programming tips and tricks!

1. IronPDF

Image description

IronPDF is a .NET and Java PDF library that makes it easy to generate, modify, and convert PDF files. With IronPDF, you can create PDFs from scratch or convert existing documents to PDF format.

IronPDF provides a wide range of features, including the ability to add text, images, and annotations to PDFs, as well as support for password protection and encryption.

Additionally, IronPDF can be used to integrate PDF functionalities in Java applications like filling out PDF forms and creating bookmarks, as well as tables of contents. Whether you're looking to create a new PDF document or edit an existing one, IronPDF is a great option.

Code Example

Let's look at code examples showcasing many different functionalities of IronPDF. As shown in the code below, we can create PDF files from any URL. We have to import the IronPDF java package in every file where we want to use IronPDF.

import com.ironsoftware.ironpdf.*;
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://getbootstrap.com/");
myPdf.saveAs(Paths.get("url.pdf"));

Enter fullscreen mode Exit fullscreen mode

The output of the code above will display what it has created in the following image. IronPDF preserves the PDF quality and renders all images and buttons with great quality.
Image description

We can create fill-in PDF forms using the code example below.

import com.ironsoftware.ironpdf.PdfDocument;  
import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;  
import java.io.IOException;  
import java.nio.file.*;

// #1 Use Case: Create a PDF Form from HTML Form Markup  
Path outputLocation = Paths.get("assets/BasicForm.pdf");  
String formHTML = "<html>"  
  + "<body>"  
  + "<h2>Editable PDF  Form</h2>"  
  + "<form>"  
  + "First name: <br> <input type='text' name='firstname' value=''> <br>"  
  + "Last name: <br> <input type='text' name='lastname' value=''>"  
  + "</form>"  
  + "</body>"  
  + "</html>";  

ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();  
renderOptions.setCreatePdfFormsFromHtml(true);  
PdfDocument.renderHtmlAsPdf(formHTML, renderOptions).saveAs(outputLocation);  

// #2 Use Case: Writing Values to the PDF Form  
PdfDocument form = PdfDocument.fromFile(outputLocation);  

// Set the value of the firstname input field.  
form.getForm().setFieldValue("firstname", "Minnie");  

// Set the value of the lastname input field.  
form.getForm().setFieldValue("lastname", "Mouse");  

// Save the changes to the PDF Form.  
form.saveAs(Paths.get("assets/BasicForm_Filled.pdf"));

Enter fullscreen mode Exit fullscreen mode

There are many things that we can do in this PDF document using the IronPDF Java PDF library. You find more information and tutorials about the IronPDF library using the following link.

Pricing

The IronPDF java PDF library price starts from $749 for the lite plan and there are many to choose from depending on your needs.

You can get a free trial for 30 days to test the IronPDF production environment without a watermark. Here is more information about licensing using the following link.

Image description

2. iText7 Library

Image description

iText7 is a PDF library designed specifically to create high-quality PDF documents from scratch. It allows you to create both simple and complex documents in a variety of file formats, including HTML5.

iText7 includes features like digital signatures and encryption so that users can securely share their documents with others over the internet.

With its wide array of features and easy deployment options, iText has become one of the go-to libraries for Java developers when working with PDF formats.

Create PDF files - Code Example

Using the following code, you can create a simple table in PDFs using the iText library.

package com.itextpdf.samples.sandbox.tables;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;

import java.io.File;

public class SimpleTable {
    public static final String DEST = "./target/sandbox/tables/simple_table.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new SimpleTable().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Table table = new Table(UnitValue.createPercentArray(8)).useAllAvailableWidth();

        for (int i = 0; i < 16; i++) {
            table.addCell("hi");
        }

        doc.add(table);

        doc.close();
    }
}

Enter fullscreen mode Exit fullscreen mode

The above code contains a class that will create a table comprised of 16 cells.

This is the resulting output in the following image.
Image description

We can add a digital signature using the following code.

package com.itextpdf.samples.signatures.chapter02;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.StampingProperties;
import com.itextpdf.signatures.PdfSigner;
import com.itextpdf.signatures.PdfSignatureAppearance;
import com.itextpdf.signatures.IExternalSignature;
import com.itextpdf.signatures.IExternalDigest;
import com.itextpdf.signatures.PrivateKeySignature;
import com.itextpdf.signatures.BouncyCastleDigest;
import com.itextpdf.signatures.DigestAlgorithms;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.Certificate;

public class C2_02_SignHelloWorldWithTempFile {
    public static final String DEST = "./target/signatures/chapter02/";

    public static final String KEYSTORE = "./src/test/resources/encryption/ks";
    public static final String SRC = "./src/test/resources/pdfs/hello.pdf";

    public static final char[] PASSWORD = "password".toCharArray();

    public static final String[] RESULT_FILES = new String[] {
            "hello_signed_with_temp.pdf"
    };

    public void sign(String src, String temp, String dest, Certificate[] chain, PrivateKey pk,
            String digestAlgorithm, String provider, PdfSigner.CryptoStandard subfilter,
            String reason, String location)
            throws GeneralSecurityException, IOException {
        PdfReader reader = new PdfReader(src);

        // Pass the temporary file's path to the PdfSigner constructor
        PdfSigner signer = new PdfSigner(reader, new FileOutputStream(dest), temp, new StampingProperties());

        // Create the signature appearance
        Rectangle rect = new Rectangle(36, 648, 200, 100);
        PdfSignatureAppearance appearance = signer.getSignatureAppearance();
        appearance
                .setReason(reason)
                .setLocation(location)

                // Specify if the appearance before field is signed will be used
                // as a background for the signed field. The "false" value is the default value.
                .setReuseAppearance(false)
                .setPageRect(rect)
                .setPageNumber(1);
        signer.setFieldName("sig");

        IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, provider);
        IExternalDigest digest = new BouncyCastleDigest();

        // Sign the document using the detached mode, CMS or CAdES equivalent.
        signer.signDetached(digest, pks, chain, null, null, null, 0, subfilter);
    }

    public static void main(String[] args) throws GeneralSecurityException, IOException {
        File file = new File(DEST);
        file.mkdirs();

        BouncyCastleProvider provider = new BouncyCastleProvider();
        Security.addProvider(provider);
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(new FileInputStream(KEYSTORE), PASSWORD);
        String alias = ks.aliases().nextElement();
        PrivateKey pk = (PrivateKey) ks.getKey(alias, PASSWORD);
        Certificate[] chain = ks.getCertificateChain(alias);

        new C2_02_SignHelloWorldWithTempFile().sign(SRC, DEST, DEST + RESULT_FILES[0], chain, pk,
                DigestAlgorithms.SHA256, provider.getName(), PdfSigner.CryptoStandard.CMS,
                "Temp test", "Ghent");
    }
}

Enter fullscreen mode Exit fullscreen mode

The above code will add a digital signature, using the latest version of iText7.

Pricing

There is no pricing plan mentioned on the website. You must get a quote from customer support for purchasing the license. However, you can get some resources for free like iText 7 Core and pdfHTML. Find more information about iText7 licensing using the following link.

Image description

3. jPDFProcess

jPDFProcess is a Java library designed specifically to create high-quality PDFs, documents or images from scratch. With this tool you can quickly generate complex documents such as invoices or catalogs with ease thanks to its intuitive API and rich feature set including support for text manipulation, form creation/filling/submission, and more.

Additionally, jPDFProcess also provides an API for digitally signing documents using digital certificates. This makes it ideal for certain document exploitation scenarios like eSignatures or document archiving processes involving digital signatures.

Code Example

You can use the following code to create PDF files using the jPDFProcess open-source java tool.

/*
 * This sample java program uses jPDFProcess
 * to create a new PDF file, add a page to it
 * and draw an image and text on the page.
 * 
 */
package jPDFProcessSamples;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import com.qoppa.pdfProcess.PDFDocument;
import com.qoppa.pdfProcess.PDFGraphics;
import com.qoppa.pdfProcess.PDFPage;

public class CreatePDFWithTextAndImage
{
    public static void main (String [] args)
    {
        try
        {
            // create document
            PDFDocument pdfDoc = new PDFDocument ();

            // create and add a page
            PDFPage page = pdfDoc.appendNewPage(8.5 * 72, 11 * 72);

            // get graphics from the page
            // this object is a Graphics2D Object and you can draw anything 
            // you would draw on a Graphics2D
            PDFGraphics g2d = (PDFGraphics) page.createGraphics();

            // read an image from png,. jpeg, etc... 
            BufferedImage image = ImageIO.read(new File("C:\\myimage.png"));

            // draw the image on the page
            g2d.drawImage(image, 0, 0, null);

            // set the font and color
            g2d.setFont (PDFGraphics.HELVETICA.deriveFont(24f));
            g2d.setColor(Color.BLUE);

            // draw text on the graphics object of the page
            g2d.drawString("NEW TEXT", 200, 100);

            // Save the document
            pdfDoc.saveDocument ("C:\\test.pdf");
        }
        catch(Throwable t)
        {
            t.printStackTrace();
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

The code above will create a PDF document, add a page on it and draw an image and text on that page.

We can add a watermark to the PDF file using the following code.

package jPDFProcessSamples;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;

import com.qoppa.pdfProcess.PDFDocument;
import com.qoppa.pdfProcess.PDFGraphics;
import com.qoppa.pdfProcess.PDFPage;

public class AddWatermark
{
    public static void main (String [] args)
    {
        try
        {
            // Load document
            PDFDocument pdfDoc = new PDFDocument ("input.pdf", null);

            // Loop through all pages
            Font wmFont = PDFGraphics.HELVETICA.deriveFont(64f);
            for (int pageIx = 0; pageIx < pdfDoc.getPageCount(); ++pageIx)
            {
                // Get the page
                PDFPage page = pdfDoc.getPage(pageIx);

                // Get a graphics object to draw onto the page
                Graphics2D g2d = page.createGraphics();

                // Draw watermark
                g2d.setColor (new Color (160, 160, 160, 160));
                g2d.setFont(wmFont);
                g2d.translate(72, 72 + g2d.getFontMetrics().getAscent());
                g2d.rotate(Math.toRadians(45));
                g2d.drawString ("Confidential", 0, 0);
            }

            // Save the document
            pdfDoc.saveDocument ("output.pdf");
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The code above contains a class with a method that will loop through all the pages and a draw watermark on each one.

Pricing

jPDFProcess is an open-source java library. We can use its PDF features for free.

4- Apache PDFBox

Apache PDFBox is an open-source Java library that enables developers to work with PDF documents efficiently. With PDFBox, you can create new PDFs, manipulate existing ones, and extract content such as text and metadata.

Features of Apache PDFBox

  • Extract Text: Retrieve Unicode text from PDF files.
  • Split & Merge: Divide a single PDF into multiple files or combine several PDFs into one.
  • Fill Forms: Extract data from PDF forms or populate them programmatically.
  • Preflight: Validate PDFs against the PDF/A-1b standard to ensure compliance.
  • Print: Utilize the standard Java printing API to print PDF documents.

Code Example

Here's a simple example demonstrating how to create a PDF with text using Apache PDFBox:

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 java.io.IOException;

public class CreatePDF {
    public static void main(String[] args) {
        try (PDDocument document = new PDDocument()) {
            PDPage page = new PDPage();
            document.addPage(page);

            try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
                contentStream.beginText();
                contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
                contentStream.newLineAtOffset(100, 700);
                contentStream.showText("Hello, PDFBox!");
                contentStream.endText();
            }

            document.save("HelloPDFBox.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This code creates a PDF named HelloPDFBox.pdf with the text "Hello, PDFBox!" positioned at coordinates (100, 700) on the page.

Pricing

Apache PDFBox is released under the Apache License 2.0, making it free for both personal and commercial use. For more detailed information and tutorials, refer to the official documentation of Apache PDFBox.

5- Aspose.PDF for Java

Aspose.PDF for Java is a robust library that empowers developers to create, manipulate, and transform PDF documents within Java applications without relying on Adobe Acrobat. It offers a comprehensive suite of features, including the ability to add text, images, tables, and graphs to PDFs, as well as functionalities for merging, splitting, and encrypting documents. This flexibility makes it a valuable tool for applications requiring dynamic PDF generation and management.

Code Example

To illustrate its capabilities, here's a simple example demonstrating how to extract text from a PDF using Aspose.PDF for Java:

import com.aspose.pdf.Document;
import com.aspose.pdf.TextAbsorber;

public class ExtractText {
    public static void main(String[] args) {
        // Open the PDF document
        Document pdfDocument = new Document("input.pdf");

        // Create a TextAbsorber object to extract text
        TextAbsorber textAbsorber = new TextAbsorber();

        // Accept the absorber for all the pages
        pdfDocument.getPages().accept(textAbsorber);

        // Get the extracted text
        String extractedText = textAbsorber.getText();

        // Display the text
        System.out.println(extractedText);
    }
}

Enter fullscreen mode Exit fullscreen mode

In this snippet, we open an existing PDF file, utilize the TextAbsorber class to extract its text content, and then print the extracted text to the console. This example showcases the straightforward API design of Aspose.PDF for Java, enabling developers to perform complex PDF operations with minimal code.

Pricing

Aspose.PDF for Java offers several licensing options to accommodate different organizational needs:

  • Developer Small Business License: Priced at $1,199, this license permits one developer and one deployment location, including free support.
  • Developer OEM License: At $3,597, this license allows one developer and unlimited deployment locations, also with free support.
  • Site Small Business License: For $5,995, this license covers up to 10 developers and 10 deployment locations, with free support included.

Additional licensing options are available, such as Metered and Site OEM licenses, each tailored to specific business requirements. For a detailed breakdown of these options and to explore paid support and consulting services, you can visit the official pricing page. ​

6- PDF Clown

PDF Clown is an open-source library designed for Java and .NET platforms, enabling developers to create, manipulate, and extract data from PDF documents. Adhering strictly to the PDF 1.7 specification (ISO 32000-1), it offers a versatile toolset for handling various PDF operations.

Code Example

To illustrate its capabilities, here's a simple example demonstrating how to extract text from a PDF using PDF Clown in Java:

import org.pdfclown.files.File;
import org.pdfclown.documents.Document;
import org.pdfclown.documents.Page;
import org.pdfclown.tools.TextExtractor;

public class ExtractText {
    public static void main(String[] args) {
        // Open the PDF document
        File file = new File("input.pdf");
        Document document = file.getDocument();

        // Instantiate a TextExtractor
        TextExtractor textExtractor = new TextExtractor();

        // Iterate through the pages
        for (Page page : document.getPages()) {
            // Extract text from the page
            String text = textExtractor.extract(page);
            // Display the text
            System.out.println(text);
        }

        // Close the file
        file.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

In this snippet, we open an existing PDF file, use the TextExtractor class to extract text from each page, and then print the extracted text to the console. This example showcases the straightforward API design of PDF Clown, enabling developers to perform complex PDF operations with minimal code.

Pricing

PDF Clown is released under the GNU Lesser General Public License (LGPL), making it free for both personal and commercial use. This open-source model allows developers to integrate PDF Clown into their projects without licensing fees, providing a cost-effective solution for PDF manipulation needs.

Summary

Feature / Library IronPDF ⭐️ iText7 jPDFProcess Apache PDFBox Aspose.PDF PDF Clown
Create PDFs ✅ (from HTML, URL, Images) ✅ (Basic creation) ✅ (Image/Text-based) ✅ (Basic creation) ✅ (Rich features) ✅ (Basic creation)
Modify PDFs ✅ (Rich editing capabilities)
Extract Text
Fill Forms ✅ (Advanced form creation/editing) ✅ (Basic features)
HTML to PDF ✅ (Advanced support) ✅ (Requires add-on)
Digital Signatures ✅ (Advanced) ✅ (Advanced)
Annotations
Split/Merge PDFs
Encryption ✅ (Advanced security features) ✅ (Basic features)
Pricing Starts at $749 Quote-based Free (Open-source) Free (Open-source) Starts at $1199 Free (Open-source)
Free Trial ✅ (30 days, no watermark) ✅ (limited) ✅ (open-source) ✅ (open-source) ✅ (limited) ✅ (open-source)
Quality & Support ⭐️⭐️⭐️⭐️⭐️ (Excellent support) ⭐️⭐️⭐️ ⭐️⭐️⭐️ ⭐️⭐️⭐️ ⭐️⭐️⭐️⭐️ ⭐️⭐️⭐️

IronPDF is our preferred library of choice, as it offers slightly more features than IText7 and other libraries at a lower cost. We also found IronPDF to be beginner friendly, so a solid choice if you're a junior developer looking for a library to start with.

iText7 does deliver good render outputs and processing speeds. It also has a freemium which you can use in your projects with limited capabilities. However, iText7 does not offer a free trial. It has good documentation but lacks tutorials for beginners. There is no fixed price mentioned on the website for the license. You have to contact support to get a quote.

Aspose.PDF for Java provides comprehensive capabilities suitable for dynamic PDF generation but may pose a steeper learning curve. Pricing starts at $1,199. Apache PDFBox is a reliable, open-source option suitable for basic PDF tasks but lacks advanced commercial features.

PDF Clown for Java is free and adheres strictly to PDF standards, ideal for straightforward PDF manipulations. jPDFProcess is also a good library but lacks many features. Regardless, it is a free library that has useful functionality.

IronPDF has good documentation and tutorials to help you understand the process with almost no code. The license of IronPDF is optimized for all levels of users and starts at $749. IronPDF offers a free trial to let you test it in production.

In conclusion, all six libraries are high-quality and will help developers get the job done.

Top comments (1)

Collapse
 
ironsoftware profile image
IronSoftware

Hi

We would like to express our gratitude for featuring IronPDF in your recent article. As a token of our appreciation, we are pleased to offer a 5% discount on base licenses to the first 25 individuals who respond.

Please use the coupon code: IRON_2025 to avail of this offer. We encourage you to act swiftly, as this opportunity is limited.

Wishing you all the best in your coding endeavors.

Sincerely,

The Iron Software Team