PRODUCT COMPARISONS

A Comparison between IronPDF for Java and Apache PDFBox

Published January 14, 2023
Share:

This article will cover the following two of the most popular libraries used in Java to work with PDF files:

  1. IronPDF
  2. Apache PDFBox

Now which library should we use? In this article, I'll compare both libraries' core functionality to allow you to make a decision about which one is best for your production environment.

IronPDF

The IronPDF library supports HTML to PDF convertor for Java 8+, Kotlin, and Scala. This creator provides cross platform support, i.e. Windows, Linux or Cloud platforms. It is especially designed for Java that prioritizes accuracy, ease of use, and speed.

IronPDF is developed to help Software Developers develop, edit, and extract content from PDF documents. It is written upon the success and popularity of IronPDF for .NET.

Standout Features of IronPDF include:

Use HTML Assets

  • HTML (5 and below), CSS (Screen & Print), images (JPG, PNG, GIF, tiff, svg, bmp), JavaScript (+ Render Delays)
  • Fonts (Web & Icon)

HTML to PDF

  • HTML file/string to PDF document creation and manipulation
  • URL to PDF

Convert Images

  • Image to new PDF documents
  • PDF to Image

Custom Paper Settings

  • Custom Paper Size, Orientation & Rotation
  • Margins (mm, inch, & zero)
  • Color & Grayscale, Resolution & JPEG Quality

Additional Features

  • Website & System Logins
  • Custom User Agents and Proxies
  • HTTP Headers

Apache PDFBox library

Apache PDFBox is an open source Java library for working with PDF files. It allows to generate, edit, and manipulate existing documents. It can also extract content from files. The library provides several utilities which are used to perform various operations on documents.

Here are the standout features of Apache PDFBox.

Extract Text

  • Extract Unicode text from files.

Split & Merge

  • Split a single PDF into many files
  • Merge multiple documents.

Fill Forms

  • Extract data from forms
  • Fill a PDF form.

Preflight

  • Validate files against the PDF/A-1b standard.

Print

  • Print a PDF using the standard printing API.

Save as Image

  • Save PDFs as PNG, JPEG or other image types.

Create PDFs

  • Develop a PDF from scratch with embedded fonts and images.

Signing

  • Digitally sign files.

Overview

The rest of the article goes as follows:

  1. IronPDF Installation
  2. Apache PDFBox Installation
  3. Create PDF Document
  4. Images to Document
  5. Encrypting Documents
  6. Licensing
  7. Conclusion

Now, we will download and install the libraries to compare them their powerful features.

1. IronPDF Installation

Installing IronPDF for Java is simple. There are different ways of doing it. This section will demonstrate two of the most popular ways.

1.1. Download JAR and add the Library

To download the IronPDF JAR file, visit the Maven website and download the latest version of IronPDF.

  • Click the Downloads option and download the JAR.
A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 1: Download IronPDF JAR

Download IronPDF JAR

Once the JAR is downloaded, it's now time to install the library into our Maven project. You can use any IDE, but we will be using Netbeans. In the Projects section:

  • Right-Click the Libraries folder and select the Add JAR/Folder option.
A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 2: Add IronPDF Library in Netbeans

Add IronPDF Library in Netbeans

  • Move to the folder where you downloaded the JAR.
  • Select the IronPDF JAR and click Open button.
A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 3: Open IronPDF JAROpen IronPDF JAR

Open IronPDF JAR

1.2. Install via Maven as a Dependency

Another way of downloading and installing IronPDF is using Maven. You can simply add the dependency in the pom.xml or use Netbeans's Dependency tool to include it your project.

Add the Library Dependency in pom.xml

Copy the following code and paste it in the pom.xml.

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>com.ironsoftware</artifactId>
   <version>2024.9.1</version>
</dependency>
XML

Add The Library using the Dependencies Feature

  • Right-Click on dependencies
  • Select Add Dependency and fill in the following details with updated version
A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 4: Add IronPDF Dependency

Add IronPDF Dependency

Now let's install Apache PDFBox.

2. Apache PDFBox Installation

We can download and install PDFBox using the same methods as IronPDF.

2.1. Download JAR and Add the Library Manually

To install PDFBox JAR, visit the official website and download the latest version.

After creating a project, in the project section:

  • Right-Click the Libraries folder and select Add JAR/Folder option.
A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 5: Add Library

Add Library

  • Move to the folder where you downloaded the JAR.
  • Select the PDFBox JAR and click Open button.
A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 6: Open PDFBox JAR

Open PDFBox JAR

2.2. Install via Maven as a Dependency

Add Dependency in the pom.xml

Copy the following code and paste it in the pom.xml.

<dependencies>
    <dependency>  
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox-app</artifactId>
        <version>3.0.0-alpha3</version>
    </dependency>
</dependencies>
XML

This will automatically download the PDFBox dependency and install in repository folder. It will now be ready to use.

Add Dependency using the Dependencies Feature

  • Right-Click on dependencies in the project section
  • Select Add Dependency and fill in the following details with updated version
A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 7: Add PDFBox Dependency

Add PDFBox Dependency

3. Create PDF Document

3.1. Using IronPDF

IronPDF provides different methods for creating files. Let's have a look at two most important methods.

Existing URL to PDF

IronPDF makes it very simple to generate documents from HTML. The following code sample convert a web page's URL to a PDF.

License.setLicenseKey("YOUR-LICENSE-KEY");
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
myPdf.saveAs(Paths.get("url.pdf"));
JAVA

The output is the below URL that is well formatted and saved as following:

A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 8: Add PDFBox Dependency

IronPDF URL Output

HTML Input String to PDF

The following sample code shows how an HTML string can be used to render a PDF in Java. You simply use HTML string or document to convert it to generate new documents.

License.setLicenseKey("YOUR-LICENSE-KEY");
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
myPdf.saveAs(Paths.get("html_saved.pdf"));
JAVA

The output is as follows:

A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 9: IronPDF HTML Output

IronPDF HTML Output

3.2. Using Apache PDFBox

PDFBox can also generate new PDF documents from different formats, but it cannot convert directly from URL or HTML string.

The following code sample creates a document with some text:

//Create document object
PDDocument document = new PDDocument();    

PDPage blankPage = new PDPage();
document.addPage(blankPage);

//Retrieving the pages of the document 
PDPage paper = document.getPage(0);
PDPageContentStream contentStream = new PDPageContentStream(document, paper);

//Begin the Content stream 
contentStream.beginText(); 

//Setting the font to the Content stream  
contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);

//Setting the position for the line 
contentStream.newLineAtOffset(25, 700);

String text = "This is the sample document and we are adding content to it.";

//Adding text in the form of string 
contentStream.showText(text);      

//Ending the content stream
contentStream.endText();

System.out.println("Content added");

//Closing the content stream
contentStream.close();

//Saving the document
document.save("C:/PdfBox_Examples/my_doc.pdf");

System.out.println("PDF created");  

//Closing the document  
document.close();
JAVA
A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 10: PDFBox Positioned Output

PDFBox Positioned Output

However, if we remove contentStream.newLineAtOffset(25, 700); from the above code example and then run the project, it produces a PDF with output at the bottom of the pape. This can be pretty annoying for some developers, as they have to adjust the text using (x,y) coordinates. y = 0 means that the text will appear at the bottom.

A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 11: PDFBox Positioned Output

PDFBox without Positioning Output

4. Images to Document

4.1. Using IronPDF

IronPDF can easily convert multiple images to a single PDF. The code for adding multiple images to a single document goes as follows:

import com.ironsoftware.ironpdf.*;  
import java.io.IOException;  
import java.nio.file.*;  
import java.util.ArrayList;  
import java.util.List;  

// Reference to the directory containing the images that we desire to convert
List<Path> images = new ArrayList<>();
images.add(Paths.get("imageA.png"));
images.add(Paths.get("imageB.png"));
images.add(Paths.get("imageC.png"));
images.add(Paths.get("imageD.png"));
images.add(Paths.get("imageE.png"));

// Render all targeted images as PDF content and save them together in one document.
PdfDocument merged = PdfDocument.fromImage(images);
merged.saveAs("output.pdf");
JAVA
A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 12: PDFBox Positioned Output

IronPDF Images to Output

4.2. Using Apache PDFBox

import java.io.File;

import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

// Reference to the directory containing the images that we desire to convert
Path imageDirectory = Paths.get("assets/images");

// Create an empty list to contain Paths to images from the directory.
List<Path> imageFiles = new ArrayList<>();

PDDocument doc = new PDDocument();

// Use a DirectoryStream to populate the list with paths for each image in the directory that we want to convert
try (DirectoryStream<Path> stream = Files.newDirectoryStream(imageDirectory, "*.{png,jpg}")) {
    for (Path entry : stream) {
        imageFiles.add(entry);
    }
    for (int i = 0; i < imageFiles.size(); i++){
        //Add a Page
PDPage blankPage = new PDPage();
        doc.addPage(blankPage);
        PDPage page = doc.getPage(i);

        //Creating PDImageXObject object
        PDImageXObject pdImage = PDImageXObject.createFromFile(imageFiles.get(i).toString(),doc);

        //creating the PDPageContentStream object
        PDPageContentStream contents = new PDPageContentStream(doc, page);

        //Drawing the image in the document
        contents.drawImage(pdImage, 0, 0);

        System.out.println("Image inserted");

        //Closing the PDPageContentStream object
        contents.close();
    }
    //Saving the document
    doc.save("C:/PdfBox_Examples/sample.pdf");

    //Closing the document
    doc.close();

} catch (IOException exception) {
    throw new RuntimeException(String.format("Error converting images to PDF from directory: %s: %s",
            imageDirectory,
            exception.getMessage()),
            exception);
}
JAVA
A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 13: PDFBox Images to Output

IronPDF Images to Output

5. Encrypting Documents

5.1. Using IronPDF

The code for encrypting PDFs with a password in IronPDF is given below:

// Open a document(or create a new one from HTML)
PdfDocument pdf = PdfDocument.fromFile(Paths.get("assets/composite.pdf"));

// Edit security settings
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setOwnerPassword("top-secret");
securityOptions.setUserPassword("sharable");

// Change or set the document encryption password
SecurityManager securityManager = pdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);
pdf.saveAs(Paths.get("assets/secured.pdf"));
JAVA

5.2. Using Apache PDFBox

Apache PDFBox also provides document encryption to make the files more secure. You can also add additional information like meta data. The code goes as follows:

//Loading an existing document
File file = new File("C:/PdfBox_Examples/sample.pdf");
PDDocument document = PDDocument.load(file);

//Creating access permission object
AccessPermission ap = new AccessPermission();

//Creating StandardProtectionPolicy object
StandardProtectionPolicy spp = new StandardProtectionPolicy("1234", "1234", ap);

//Setting the length of the encryption key
spp.setEncryptionKeyLength(128);

//Setting the access permissions
spp.setPermissions(ap);

//Protecting the document
document.protect(spp);

System.out.println("Document encrypted");

//Saving the document
document.save("C:/PdfBox_Examples/encrypted.pdf");
//Closing the document
document.close();
JAVA

6. Pricing and Licensing

IronPDF Pricing and Licensing

IronPDF is free to use for developing simple PDF applications and can be licensed for commercial use at any time. IronPDF offers single project licenses, single developer licenses, licenses for agencies and multinational organizations, and SaaS and OEM redistribution licenses and support. All licenses are available with a free trial, a 30 day money back guarantee, and one year of software support and upgrades.

The Lite package is available for $749. There are absolutely no recurring fees with IronPDF products. More detailed information about software licensing is available on the product licensing page.

A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 14: IronPDF Licensing

IronPDF Licensing

Apache PDFBox Pricing and Licensing

Apache PDFBox is freely available without any charges. It is free regardless of how it is used, whether for personal, for internal or for commercial purposes.

You can include the Apache License 2.0 (current version) from here. To include the copy of license, simply include it in your work and you can also attach the following notice as a comment at the top of your source code.

Copyright [yyyy] [name of copyright owner]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Conclusion

In comparison, IronPDF has an upper hand over Apache PDFBox in both functionality and product support. It also provides SaaS and OEM support, which is a requirement in modern software development. However, the library is not free for commercial use like Apache PDFBox is.

Companies with large software applications amy require continual bug fixes and support from third-party vendors to resolve problems as they arise during software development. This is something that is lacking in many open source solutions like Apache PDFBox, which relies on voluntary support from its community of developers to keep it maintained. In short, IronPDF is best used for business and market use, while Apache PDFBox is better suited for personal and noncommercial applications.

There is also a free trial to test the functionality of IronPDF. Give it a try or buy it now.

You can now get all Iron Software products in Iron Suite at a greatly reduced price. Visit this web page for more information about this amazing deal.

< PREVIOUS
A Comparison Between IronPDF for Java & Spire.PDF Java
NEXT >
A Comparison between IronPDF For Java and iTextPDF itext7 for Java

Ready to get started? Version: 2024.9 just released

Free Maven Download View Licenses >