Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This article will cover the following two of the most popular libraries used in Java to work with PDF files:
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.
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:
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.
The rest of the article goes as follows:
Now, we will download and install the libraries to compare them their powerful features.
Installing IronPDF for Java is simple. There are different ways of doing it. This section will demonstrate two of the most popular ways.
To download the IronPDF JAR file, visit the Maven website and download the latest version of IronPDF.
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:
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.
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>
Now let's install Apache PDFBox.
We can download and install PDFBox using the same methods as IronPDF.
To install PDFBox JAR, visit the official website and download the latest version.
After creating a project, in the project section:
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>
This will automatically download the PDFBox dependency and install in repository folder. It will now be ready to use.
IronPDF provides different methods for creating files. Let's have a look at two most important methods.
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"));
The output is the below URL that is well formatted and saved as following:
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"));
The output is as follows:
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();
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.
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");
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);
}
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"));
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();
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.
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.
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.
9 .NET API products for your office documents