Published January 16, 2023
A Comparison between IronPDF For Java and iTextPDF itext7 for Java
Today, developers can benefit from better solutions thanks to ever-improving technology.
The future of software development processes lies in automation. For a long time, PDF files have presented difficulties for developers. When working with PDFs (i.e., producing content and converting content from other formats to PDF), there are numerous considerations to be made. With the development of numerous libraries intended to aid in reading, writing, creating and even converting PDFs from many formats, these needs are now met.
This article will compare two of the most well-received PDF libraries for Java developers for editing, printing, and createing PDF files:
- IronPDF library: A Java PDF library based on that focuses on generating PDFs from HTML.
- ITextPDF library: A Java-first, open source library that focuses on generating PDFs using a programmable API.
We will examine the features of the two libraries before moving on to the performance expenses for converting and handling the PDFs in order to determine which library is best for your application. Additionally, each library's duration will be recorded for later research.
Installing IronPDF Java
To install IronPDF for java, you just declare it as a dependency. To define IronPDF as a dependency, please add the following to your pom.xml
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>com.ironsoftware</artifactId>
<version>2023.9.2</version>
</dependency>
Note: you can download the .jar manually:
- Go to the Maven Repo.
- Extract the contents of the zip file.
- Create a folder and copy the contents of zip folder.
- Open Eclipse IDE.
- Create a new Java project.
- Add the IronPDF .jar files in class path.
- Finish the project creation wizard. That's it!
IronPDF Features
Developers can quickly produce, read, and manipulate PDFs with the help of the robust IronPDF PDF library. IronPDF uses the chrome engine at its core and offers a wealth of practical and powerful features, including the ability to convert HTML5, JavaScript, CSS, and image files to PDF. IronPDF can also add unique headers and footers, and create PDF files precisely as they appear in a web browser. Various web formats, including HTML, ASPX, Razor View, and MVC, are supported by IronPDF. IronPDF's key attributes are as follows:
- Easily create, read, and edit PDFs with java programs,
- Create PDFs from any website URL link that has settings for User-Agents, Proxies, Cookies, HTTP Headers, and Form Variables to support login using HTML login forms,
- Remove photos from already-existing PDF publications,
- Add text, photos, bookmarks, watermarks, and other elements to PDFs,
- Merge and divide the pages of several PDF documents,
- Convert media-type files, including CSS files, into documents.
Installing ITextPDF Java
ITextPDF is available for free at https://itextpdf.com/. This library is open-source under the AGPL software licensing model.
To add the iText library into your application, include the following maven repository into our pom.xml
file.
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.2</version>
</dependency>
Download the iTextPDF .jar files directly and download the slf4j.jar file directly. To use the libraries add the iTextPDF .jar files to the classpath of the system.
IText Library Features
- Create PDFs from HTML, XML, and Images (png, jpg, etc)
- Add bookmarks, page numbers, and markers to PDF documents
- Split a PDF file into multiple PDFs or combine multiple PDFs into a single PDF
- Edit PDF Forms programmatically
- Add text, images, and geometrical figures to PDFs
Convert HTML Strings to PDFs using iTextPDF Java
In iText for Java, HTMLConverter
is the primary class used to convert HTML to PDF.
There are three main methods in HTMLConverter
:
convertToDocument
, which returns aDocument
objectconvertToElements
, which return a list ofIElement
objectsconvertToPdf
handles converting HTML content to PDF. This method accepts HTML input as aString
, aFile
, or anInputStream
, and returns aFile
, anOutputStream
, or aDocument
object.
package com.hmkcode;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.html2pdf.HtmlConverter;
public class App
{
public static final String HTML = "<h1>Hello</h1>"
+ "<p>This was created using iText</p>"
+ "<a href='hmkcode.com'>hmkcode.com</a>";
public static void main( String[] args ) throws FileNotFoundException, IOException
{
HtmlConverter.convertToPdf(HTML, new FileOutputStream("string-to-pdf.pdf"));
System.out.println( "PDF Created!" );
}
}
Convert HTML Strings to PDFs using IronPDF Java
The PdfDocument
class from IronPDF offers multiple static methods that let Java developers produce HTML text from various sources. One such method is the PdfDocument.renderHtmlAsPdf
method, which converts a string of HTML markup into a PDF document.
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"));
Convert HTML Files to PDF using ITextPDF Java
The convertToPdf
method can be used to convert any HTML file to a PDF.
Images and CSS files may be included in the HTML file. They must, however, be in the same place in the HTML file. We can use the ConverterProperties
class to set the base path for referenced CSS and images. This is useful when the HTML file assets are located in different directories.
Consider an index.html containing the following markup.
<html>
<head>
<title>HTML to PDF</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>HTML to PDF</h1>
<p>
<span class="itext">itext</span> 7.1.9
<span class="description"> converting HTML to PDF</span>
</p>
<table>
<tr>
<th class="label">Title</th>
<td>iText - Java HTML to PDF</td>
</tr>
<tr>
<th>URL</th>
<td>http://hmkcode.com/itext-html-to-pdf-using-java</td>
</tr>
</table>
</body>
</html>
<html>
<head>
<title>HTML to PDF</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>HTML to PDF</h1>
<p>
<span class="itext">itext</span> 7.1.9
<span class="description"> converting HTML to PDF</span>
</p>
<table>
<tr>
<th class="label">Title</th>
<td>iText - Java HTML to PDF</td>
</tr>
<tr>
<th>URL</th>
<td>http://hmkcode.com/itext-html-to-pdf-using-java</td>
</tr>
</table>
</body>
</html>
The source code below converts the index.html file into a PDF:
package com.hmkcode;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.html2pdf.HtmlConverter;
public class App
{
public static void main( String[] args ) throws FileNotFoundException, IOException
{
HtmlConverter.convertToPdf(new FileInputStream("index.html"),
new FileOutputStream("index-to-pdf.pdf"));
System.out.println( "PDF Created!" );
}
}
Convert HTML Files to PDF using IronPDF for Java
IronPDF's PdfDocument.renderHtmlFileAsPdf
method converts HTML files located on a computer or on a network file path.
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
Add Images to PDF files using IronPDF Java
You can use IronPDF's PdfDocument.fromImage
method to render a group of images into a single PDF file. The next example uses this method on a short list of images located on different filesystem paths.
// Create an ArrayList containing the list of images that you want to combine
// into ONE PDF document
Path imageA = Paths.get("directoryA/1.png");
Path imageB = Paths.get("directoryB/2.png");
Path imageC = Paths.get("3.png");
List<Path> imageFiles = new ArrayList<>();
imageFiles.add(imageA);
imageFiles.add(imageB);
imageFiles.add(imageC);
// Convert the three images into a PDF and save them.
PdfDocument.fromImage(imageFiles).saveAs(Paths.get("assets/composite.pdf"));
Add Images to PDFs using ITextPDF Java
The complete code example below uses iText to convert two images located in the current working directory into one PDF file.
import java.io.*;
// importing itext library packages
import com.itextpdf.io.image.*;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
public class InsertImagePDF {
public static void main(String[] args) throws IOException {
String currDir = System.getProperty("user.dir");
// Getting path of current working directory
// to create the pdf file in the same directory of
// the running java program
String pdfPath = currDir + "/InsertImage.pdf";
// Creating path for the new pdf file
PdfWriter writer = new PdfWriter(pdfPath);
// Creating PdfWriter object to write the PDF file to
// the path
PdfDocument pdfDoc = new PdfDocument(writer);
// Creating a Document object
Document doc = new Document(pdfDoc);
// Creating imagedata from image on disk(from given
// path) using ImageData object
ImageData imageDataA = ImageDataFactory.create(
currDir + "/imageA.jpg");
Image imgA = new Image(imageDataA);
ImageData imageDataB = ImageDataFactory.create(
currDir + "/imageB.jpg");
Image imgB = new Image(imageDataB);
// Creating Image object from the imagedata
doc.add(imgA);
doc.add(imgB);
// Adding Image to the empty document
doc.close();
// Close the document
System.out.println("Image added successfully and PDF file created!");
}
}
Licensing
iTextSharp is open source and is licensed under the AGLP.
This ensures that anyone who utilizes an application that incorporates iTextSharp is entitled to a complete copy of the application's source code, even if they do so over a corporate network or the internet.
Contact iText directly to discuss the pricing of the license if you intend to use it for business applications.
IronPDF is free for development and can always be licensed for commercial deployment. Licenses are available for single-project use, individual developers, agencies, and global corporations, as well as for SaaS and OEM redistribution. All licenses include a 30-day money-back guarantee, one year of product support and updates, validity for dev/staging/production, and also a permanent license (one-time purchase).
Pricing for the Lite package starts from $749.
- Developers can enjoy unlimited use of the library for development. In terms of general licensing, the rates are very cost-effective
- Free one-year unlimited support
- 30-day trials are also available for licensing purposes
- All licenses include a 30-day money-back guarantee
- Licenses are valid for all environments (development, staging, production, etc)
- Licneses include one year of unconditional support
- IronPDF licenses require a one-time only purchase
IronPDF vs iText
There are several significant differences between iText and IronPDF.
iText's API is structured around a programmatic model. Manipulation of PDF properties and content under this model are more low-level and granular. While this gives the programmer more control and flexibility, it also requires writing more code to implement use-cases.
IronPDF's API is structured around optimizing the developer's productivity. IronPDF simplifies PDF editing, manipulation, creation, and other complex tasks by allowing developers to complete them with just a few lines of code.
Conclusion
All customers of Iron Software have the option of purchasing the entire suite of packages with just two clicks. You can currently purchase all five libraries from the Iron Software suite, along with ongoing support for each, for the cost of just two libraries from the suite.