A Comparison between IronPDF for Java and OpenPDF
Discover the Best PDF Library for Your Java Application
In this article, we'll explore how a Java PDF library can enhance your project and compare the top two libraries based on their features, licensing costs, and tutorials. In the past, working with PDF files in Java was challenging, but with technological advancements, there are now numerous Java APIs available for managing them.
The two libraries that we'll be examining are:
Both libraries offer the ability to create, edit, and print PDF files in Java. In this comparison, we'll concisely evaluate their combined capabilities, codebase, and ease of installation.
First, let's take a look at IronPDF.
IronPDF for Java
IronPDF for Java creates and manages PDF files. It allows developers to read, create, and edit PDF files without the need for Adobe software. Developers can add custom headers and footers, add signatures, and other security restrictions. Full multithreading and asynchronous support help IronPDF achieve high performance when developers need it.
IronPDF Features
IronPDF for Java offers several PDF production and manipulation tools, allowing developers to quickly bring their Java applications up to speed.
HTML-to-PDF Conversions: This feature enables the generation of PDF documents from HTML files, MVC views, web forms, and URLs.
PDF Imaging: Allows developers to create PDFs from images and to extract images from PDFs.
PDF Editing: Can add watermarks, headers, footers, backgrounds, and foregrounds, add and remove pages, and more.
PDF Content Extraction: Can extract text and images from PDFs (extracting text from embedded PDF images could require the use of the IronOCR library).
- Compatibility: Compatible with Java 8+ and all modern operating systems and IDEs.
OpenPDF
OpenPDF is an open-source, free Java library with a dual LGPL and MPL license, specifically designed for working with PDFs. It allows for the generation and manipulation of PDF documents, as well as editing existing documents and extracting content. OpenPDF is a convenient tool when working with PDF documents, whether creating new ones or editing existing ones.
OpenPDF Features
Create PDFs and Print: Lets you create new PDF documents from scratch and use the Java printing API to print a PDF file.
Split and Merge: Can split a single PDF into many files or merge multiple PDF files into a single PDF file.
Extract Text: Lets you extract text from PDF files and PDF forms.
Signing: Allows developers to digitally sign a PDF.
Save as Image: Can save PDFs as image files, such as JPEG or PNG.
Office to PDF: Can convert MS Word, MS PowerPoint, and MS Excel to PDF documents.
- Parsing HTML: It also offers parsing of HTML files into PDF files.
IronPDF for Java Installation
Installing IronPDF for Java is a straightforward process, even for new Java developers.
To use IronPDF for Java, you'll need an IDE. In this article, we'll use JetBrains IntelliJ IDEA for installation and examples.
To start, open JetBrains IntelliJ IDEA and create a new Maven project:
- A new window will appear where you enter the name of the project and click on the 'Finish' button.
- After clicking 'Finish', a new project will open and the default
pom.xml
file will be opened. This is great because we need to add the Maven dependencies of IronPDF for Java to this file.
Add the following dependencies to the pom.xml
file:
<!-- Add IronPDF dependencies here -->
<dependencies>
<!-- IronPDF Maven Dependency -->
</dependencies>
<!-- Add IronPDF dependencies here -->
<dependencies>
<!-- IronPDF Maven Dependency -->
</dependencies>
Once you add the dependencies to the pom.xml
file, a small icon will appear in the upper right corner of the pom.xml
file. Click on this icon to install the Maven dependencies of IronPDF for Java. This process can take a few minutes, depending on your internet connection.
OpenPDF Installation
Installing OpenPDF is similar to the installation of IronPDF for Java. Follow these steps:
- Open JetBrains IntelliJ IDEA and create a new Maven project.
- Enter the name of the project and click on the 'Finish' button.
- A new project will open and the default
pom.xml
file will be displayed. This is good, as you need to add the Maven dependencies of OpenPDF.
Add the OpenPDF dependency to the dependencies
section of the pom.xml
file:
<dependencies>
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.30</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.30</version>
</dependency>
</dependencies>
When you add the repository and dependency code, a small icon will appear in the top right corner of the pom.xml
file. Click on the icon to install the OpenPDF for Java dependencies. After a few minutes, it will be installed and ready for use.
Create a New PDF file
In addition to user appeal, PDF also offers several technical benefits. PDF files are platform independent and can be read on any operating system or device. They also preserve formatting and layout, ensuring that the document appears the same no matter who opens it. PDF files are also compact and can be easily shared and stored. By using PDF in your Java application, you can provide your users with an efficient and well-supported document format that offers a wide range of functionality and compatibility.
Here, we will discuss how to create a new PDF file using both IronPDF and OpenPDF libraries.
Create a New PDF file using IronPDF
Creating and editing PDF files using IronPDF for Java is easy and only requires a few lines of code. The following is an example:
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render a simple HTML to PDF
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
// Save the rendered PDF
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render a simple HTML to PDF
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
// Save the rendered PDF
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}
Create a New PDF file using OpenPDF
Creating a new PDF file can be done using OpenPDF. Here's a code example:
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class Main {
/**
* Generates a PDF file with the text 'Hello World'
*/
public static void main(String[] args) {
System.out.println("Generating Hello World PDF");
Document document = new Document();
try (FileOutputStream fileOutputStream = new FileOutputStream("HelloWorld.pdf")) {
PdfWriter.getInstance(document, fileOutputStream);
// Open document for writing content
document.open();
// Add content to the PDF
document.add(new Paragraph("Hello World"));
} catch (DocumentException | IOException e) {
System.err.println("Error creating PDF: " + e.getMessage());
} finally {
// Ensure document is closed
document.close();
}
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class Main {
/**
* Generates a PDF file with the text 'Hello World'
*/
public static void main(String[] args) {
System.out.println("Generating Hello World PDF");
Document document = new Document();
try (FileOutputStream fileOutputStream = new FileOutputStream("HelloWorld.pdf")) {
PdfWriter.getInstance(document, fileOutputStream);
// Open document for writing content
document.open();
// Add content to the PDF
document.add(new Paragraph("Hello World"));
} catch (DocumentException | IOException e) {
System.err.println("Error creating PDF: " + e.getMessage());
} finally {
// Ensure document is closed
document.close();
}
}
}
Convert HTML to PDF
Web pages are frequently accessed by people. There are several sites that you may want to check regularly. Visiting the website every time may not be feasible. If you need to access the information frequently, it's more convenient to store it as a document that you can access anytime from your phone or laptop. PDF format is a better option as it provides the benefits of password protection, making the document secure.
Converting HTML to PDF is one of the most commonly used features of PDF libraries and is used by almost every developer due to its benefits. In this section, we will discuss coded examples for both IronPDF for Java and OpenPDF.
Convert HTML to PDF using IronPDF
IronPDF's state-of-the-art renderer provides seamless conversion of HTML to PDF in three different methods.
HTML File to PDF
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render HTML file to PDF
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("index.html");
// Save the rendered PDF
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render HTML file to PDF
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("index.html");
// Save the rendered PDF
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
}
}
HTML String To PDF
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render HTML string to PDF
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> Example of HTML to PDF using IronPDF for Java </h1> IronPDF for Java is a robust Java API for creating, converting, and manipulating PDF files");
// Save the rendered PDF
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render HTML string to PDF
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> Example of HTML to PDF using IronPDF for Java </h1> IronPDF for Java is a robust Java API for creating, converting, and manipulating PDF files");
// Save the rendered PDF
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}
URL to PDF
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render URL to PDF
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20");
try {
// Save the rendered PDF
myPdf.saveAs(Paths.get("url.pdf"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render URL to PDF
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20");
try {
// Save the rendered PDF
myPdf.saveAs(Paths.get("url.pdf"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Convert HTML to PDF using OpenPDF
OpenPDF provides an option for parsing HTML files into PDF files but lacks the feature of converting URLs to PDFs. Here is an example:
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.html.HtmlParser;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
/**
* Parses HTML and generates a PDF file
*/
public static void main(String[] args) {
System.out.println("Parsing HTML to create PDF");
try (Document document = new Document()) {
FileOutputStream outputStream = new FileOutputStream("contact.pdf");
PdfWriter.getInstance(document, outputStream);
// Open the document
document.open();
// Parse the HTML document and write to PDF
HtmlParser.parse(document, Main.class.getClassLoader().getResourceAsStream("contact.html"));
} catch (DocumentException | IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.html.HtmlParser;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
/**
* Parses HTML and generates a PDF file
*/
public static void main(String[] args) {
System.out.println("Parsing HTML to create PDF");
try (Document document = new Document()) {
FileOutputStream outputStream = new FileOutputStream("contact.pdf");
PdfWriter.getInstance(document, outputStream);
// Open the document
document.open();
// Parse the HTML document and write to PDF
HtmlParser.parse(document, Main.class.getClassLoader().getResourceAsStream("contact.html"));
} catch (DocumentException | IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}
Image to PDF Converter
Converting images to PDF is advantageous. It allows photographs to be made into a more legible and transferable format and significantly reduces file size while maintaining image quality.
Image to PDF Converter using IronPDF
Using IronPDF, you can easily convert any image format into a PDF file.
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Path to the directory containing images
Path imageDirectory = Paths.get("assets/images");
List<Path> imageFiles = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(imageDirectory, "*.{png,jpg}")) {
for (Path entry : stream) {
imageFiles.add(entry);
}
// Convert images to a single PDF document
PdfDocument.fromImage(imageFiles).saveAs(Paths.get("assets/composite.pdf"));
} catch (IOException exception) {
throw new RuntimeException("Error converting images to PDF from directory: " + imageDirectory + ": " + exception.getMessage(), exception);
}
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Path to the directory containing images
Path imageDirectory = Paths.get("assets/images");
List<Path> imageFiles = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(imageDirectory, "*.{png,jpg}")) {
for (Path entry : stream) {
imageFiles.add(entry);
}
// Convert images to a single PDF document
PdfDocument.fromImage(imageFiles).saveAs(Paths.get("assets/composite.pdf"));
} catch (IOException exception) {
throw new RuntimeException("Error converting images to PDF from directory: " + imageDirectory + ": " + exception.getMessage(), exception);
}
}
}
Image to PDF Converter using OpenPDF
OpenPDF offers image-to-PDF conversion but only supports formats like PNG, JPG, and TIFF.
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfWriter;
public class Main {
public static void main(String[] args) {
System.out.println("Converting Images to PDF");
// Step 1: Create a document object
Document document = new Document();
try {
// Step 2: Create a PdfWriter that listens to the document
// and directs a PDF-stream to a file
FileOutputStream fileOutputStream = new FileOutputStream("Images.pdf");
PdfWriter.getInstance(document, fileOutputStream);
// Step 3: Open the document
document.open();
// Step 4: Add images to the PDF
Image jpg = Image.getInstance("11.png");
document.add(jpg);
} catch (DocumentException | IOException e) {
System.err.println("Error creating PDF: " + e.getMessage());
} finally {
// Step 5: Close the document
document.close();
}
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfWriter;
public class Main {
public static void main(String[] args) {
System.out.println("Converting Images to PDF");
// Step 1: Create a document object
Document document = new Document();
try {
// Step 2: Create a PdfWriter that listens to the document
// and directs a PDF-stream to a file
FileOutputStream fileOutputStream = new FileOutputStream("Images.pdf");
PdfWriter.getInstance(document, fileOutputStream);
// Step 3: Open the document
document.open();
// Step 4: Add images to the PDF
Image jpg = Image.getInstance("11.png");
document.add(jpg);
} catch (DocumentException | IOException e) {
System.err.println("Error creating PDF: " + e.getMessage());
} finally {
// Step 5: Close the document
document.close();
}
}
}
Pricing and Licensing
IronPDF for Java is a powerful PDF library used for both personal and commercial purposes. It offers various licensing options, including single project licenses, SaaS and OEM redistribution, and licenses for multinational corporations. The cost of the Lite package starts at USD
, which includes a perpetual license, 30-day money-back guarantee, and a year of software support and upgrades. One of the advantages of IronPDF is that it has no recurring costs, meaning that once purchased, the license can be used for lifetime use.
OpenPDF is open-source software, licensed under the terms of the LGPL and MPL open-source licenses. This means that anyone using an application that utilizes OpenPDF (even over a business network or the internet) may be entitled to a complete copy of the program's source code if licensed under LGPL and MPL, making it ideal for academic purposes. However, for commercial projects, it's always recommended to contact OpenPDF for an estimate of any associated costs.
Conclusion
Java developers and IT professionals can easily integrate PDF capabilities into their Java applications using the IronPDF library. It offers a wide range of features, including formatting PDF files, generating charts and graphs, converting HTML and images to PDF, splitting and merging PDF files, and modifying PDF documents. IronPDF supports all Java versions starting from Java 8 and also JVM languages such as Java, Kotlin, and Scala. The library is also equipped with enhanced security features for PDF documents.
OpenPDF is an open-source, free Java library with an LGPL and MPL license. It allows for the creation and modification of PDF documents and extraction of content from them. Although OpenPDF is useful for producing and editing PDF documents, its features for manipulating PDF files are limited compared to IronPDF.
It's not possible to compare IronPDF and OpenPDF solely based on their licensing, as one is a commercial library and the other is open-source. However, in terms of features, OpenPDF has limited options for manipulating PDF files. On the other hand, IronPDF for Java provides a free trial license for developers to test the library and its advanced features.
IronPDF offers many more features than OpenPDF. Additionally, IronPDF provides extensive documentation, which simplifies coding with the library. OpenPDF tends to produce lengthy and complex code, and there is very little documentation available for it.
OpenPDF's HTML to PDF conversion capability is not suitable for large HTML files, and it does not support URL to PDF conversion.
Please note
Frequently Asked Questions
What are the two main Java PDF libraries discussed in this article?
The article discusses IronPDF for Java and OpenPDF as the two main Java PDF libraries.
What are the key features of the Java PDF library that allows HTML-to-PDF conversions?
IronPDF for Java allows HTML-to-PDF conversions, PDF imaging, PDF editing, PDF content extraction, and is compatible with Java 8+ and modern operating systems.
What licensing options are available for the Java library that offers HTML-to-PDF conversions?
IronPDF for Java offers various licensing options, including single project licenses, SaaS and OEM redistribution, and licenses for multinational corporations.
Is OpenPDF free to use?
Yes, OpenPDF is an open-source Java library with a dual LGPL and MPL license, making it free to use.
Can the Java library that provides HTML-to-PDF conversion also convert URLs to PDFs?
Yes, IronPDF can convert URLs to PDFs, in addition to HTML files and strings.
What is a limitation of OpenPDF compared to the Java library capable of URL to PDF conversions?
OpenPDF has limited options for manipulating PDF files and lacks the ability to convert URLs to PDFs.
What is the installation process for the Java library that allows HTML-to-PDF conversion?
To install IronPDF for Java, create a new Maven project in your IDE, add the necessary Maven dependencies to the pom.xml file, and install them.
How does the Java library with HTML-to-PDF conversion capabilities handle image to PDF conversion?
IronPDF can convert various image formats into a single PDF document.
Does OpenPDF support HTML to PDF conversion?
Yes, OpenPDF allows parsing HTML files into PDF files but does not support URL to PDF conversion.
What makes the Java library with HTML-to-PDF capabilities suitable for commercial use?
IronPDF offers a wide range of advanced features, extensive documentation, and various licensing options, making it suitable for commercial use.