IronPDF for Java 和 iTextPDF itext7 for Java 之间的比较
如今,开发人员可以因不断改进的技术而受益于更好的解决方案。
软件开发过程的未来在于自动化。 长期以来,PDF 文件给开发人员带来了困难。 在处理 PDF(即,生成内容以及将其他格式的内容转换成 PDF)时,有很多注意事项。 随着许多库的发展,这些库旨在帮助阅读、写作、创建,甚至将许多格式转换为 PDF,这些需求现在得到了满足。
本文将比较 Java 开发人员中最受欢迎的两个 PDF 库,用于编辑、打印和创建 PDF 文件:
- IronPDF for Java 库:一个专注于从 HTML 生成 PDF 的 Java PDF 库。
- ITextPDF 库:一个以 Java 为先的开源库,专注于使用可编程 API 生成 PDF。
我们将研究这两个图书馆的功能,然后研究转换和处理 PDF 的性能成本,以确定哪个图书馆最适合您的应用程序。 此外,将记录每个库的持续时间以供后续研究。
安装 IronPDF for Java
要安装 IronPDF for Java,您只需将其声明为依赖项。 要将 IronPDF 定义为依赖关系,请在 pom.xml 中添加以下内容:
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>YOUR_VERSION_HERE</version>
</dependency><dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>YOUR_VERSION_HERE</version>
</dependency>注意:您可以手动下载 .jar:
- 访问 IronPDF for Java Maven Repository 访问 Maven Repo 页面。
- 提取 zip 文件的内容。
- 创建一个文件夹并复制 zip 文件夹的内容。
- 打开 Eclipse IDE。
- 创建一个新的 Java 项目。
- 在类路径中添加 IronPDF .jar 文件。
- 完成项目创建向导。 就是这样!
IronPDF特性
开发人员可以使用强大的 IronPDF PDF 库快速生成、读取和操作 PDF。 IronPDF 在其核心使用 Chrome 引擎,并提供丰富的实用功能,包括将 HTML5、JavaScript、CSS 和图像文件转换为 PDF 的功能。 IronPDF 还可以添加独特的页眉和页脚,并准确地创建在网络浏览器中显示的 PDF 文件。 IronPDF 支持各种 Web 格式,包括 HTML、ASPX、Razor View 和 MVC。 IronPDF 的关键属性如下:
- 使用 Java 程序轻松创建、读取和编辑 PDF。
- 从任何网站 URL 链接创建 PDF,该链接具有用户代理、代理、Cookie、HTTP 头和表单变量的设置,以支持使用 HTML 登录表单的登录。
- 从现有的 PDF 出版物中删除照片。
- 向 PDF 添加文本、照片、书签、水印和其他元素。
- 合并和拆分多个 PDF 文档的页面。
- 将多媒体类型的文件(包括 CSS 文件)转换为文档。
安装 ITextPDF Java
ITextPDF 可在 iTextPDF 网站 免费使用。 该库是根据 AGPL 软件许可模型开源的。
要将 iText 库添加到您的应用程序中,请在 pom.xml 文件中包括以下 Maven 仓库。
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.2</version>
</dependency><dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.2</version>
</dependency>直接下载 iTextPDF .jar 文件并直接下载 slf4j.jar 文件。 要使用这些库,请将 iTextPDF .jar 文件添加到系统的类路径中。
IText 库功能
- 从 HTML、XML 和图像(png、jpg 等)创建 PDF。
- 向 PDF 文档添加书签、页码和标记。
- 将一个 PDF 文件拆分为多个 PDF,或将多个 PDF 合并为一个 PDF。
- 通过程序编辑 PDF 表单。
- 向 PDF 添加文本、图片和几何图形。
使用 iTextPDF Java 将 HTML 字符串转换为 PDF
在 Java 的 iText 中,HTMLConverter 是用于将 HTML 转换为 PDF 的主要类。
HTMLConverter 中有三个主要方法:
convertToDocument,返回一个Document对象。convertToElements,返回一个IElement对象列表。convertToPdf负责将 HTML 内容转换为 PDF。 此方法接受 HTML 输入为String、File或InputStream,并返回File、OutputStream或Document对象。
package com.hmkcode;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.html2pdf.HtmlConverter;
public class App {
// HTML content to be converted to PDF
public static final String HTML = "<h1>Hello</h1>"
+ "<p>This was created using iText</p>"
+ "<a href='http://hmkcode.com'>hmkcode.com</a>";
public static void main(String[] args) throws FileNotFoundException, IOException {
// Convert the HTML content to PDF and save it
HtmlConverter.convertToPdf(HTML, new FileOutputStream("string-to-pdf.pdf"));
System.out.println("PDF Created!");
}
}package com.hmkcode;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.html2pdf.HtmlConverter;
public class App {
// HTML content to be converted to PDF
public static final String HTML = "<h1>Hello</h1>"
+ "<p>This was created using iText</p>"
+ "<a href='http://hmkcode.com'>hmkcode.com</a>";
public static void main(String[] args) throws FileNotFoundException, IOException {
// Convert the HTML content to PDF and save it
HtmlConverter.convertToPdf(HTML, new FileOutputStream("string-to-pdf.pdf"));
System.out.println("PDF Created!");
}
}使用 IronPDF for Java 将 HTML 字符串转换为 PDF
IronPDF 的 PdfDocument 类提供了多种静态方法,允许 Java 开发人员从各种来源生成 HTML 文本。 其中一种方法是 PdfDocument.renderHtmlAsPdf 方法,它将 HTML 标记的字符串转换为 PDF 文档。
import com.ironsoftware.ironpdf.*;
import java.nio.file.Paths;
public class IronPdfExample {
public static void main(String[] args) {
// Set the license and log path
License.setLicenseKey("YOUR-LICENSE-KEY");
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Convert the HTML content to a PDF
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
// Save the generated PDF
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}import com.ironsoftware.ironpdf.*;
import java.nio.file.Paths;
public class IronPdfExample {
public static void main(String[] args) {
// Set the license and log path
License.setLicenseKey("YOUR-LICENSE-KEY");
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Convert the HTML content to a PDF
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
// Save the generated PDF
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}使用 ITextPDF Java 将 HTML 文件转换为 PDF
convertToPdf 方法可以将任何 HTML 文件转换为 PDF。
HTML 文件中可能包含图像和 CSS 文件。然而,它们必须与 HTML 文件位于同一位置。我们可以使用 ConverterProperties 类来设置引用的 CSS 和图像的基本路径。 当 HTML 文件资源位于不同目录时,这很有用。
考虑一个包含以下标记的 index.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><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>下面的源代码将 index.html 文件转换为 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 {
// Convert the HTML file to PDF and save it
HtmlConverter.convertToPdf(new FileInputStream("index.html"),
new FileOutputStream("index-to-pdf.pdf"));
System.out.println("PDF Created!");
}
}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 {
// Convert the HTML file to PDF and save it
HtmlConverter.convertToPdf(new FileInputStream("index.html"),
new FileOutputStream("index-to-pdf.pdf"));
System.out.println("PDF Created!");
}
}使用 IronPDF for Java 将 HTML 文件转换为 PDF
IronPDF 的 PdfDocument.renderHtmlFileAsPdf 方法可以转换位于计算机或网络文件路径上的 HTML 文件。
import com.ironsoftware.ironpdf.*;
import java.nio.file.Path;
import java.nio.file.Paths;
public class IronPdfExample {
public static void main(String[] args) {
// Convert the HTML file to PDF and save it
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
}
}import com.ironsoftware.ironpdf.*;
import java.nio.file.Path;
import java.nio.file.Paths;
public class IronPdfExample {
public static void main(String[] args) {
// Convert the HTML file to PDF and save it
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
}
}使用 IronPDF for Java 将图像添加到 PDF 文件
您可以使用 IronPDF 的 PdfDocument.fromImage 方法将一组图像呈现到一个单一的 PDF 文件中。下一个示例在位于不同文件系统路径上的短列表的图像上使用此方法。
import com.ironsoftware.ironpdf.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class IronPdfExample {
public static void main(String[] args) {
// 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"));
}
}import com.ironsoftware.ironpdf.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class IronPdfExample {
public static void main(String[] args) {
// 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"));
}
}使用 ITextPDF Java 将图像添加到 PDF
下面的完整代码示例使用 iText 将当前工作目录中的两个图像转换为一个 PDF 文件。
import java.io.*;
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
String pdfPath = currDir + "/InsertImage.pdf";
// Creating PdfWriter object to write the PDF file to the path
PdfWriter writer = new PdfWriter(pdfPath);
// Creating PdfDocument object
PdfDocument pdfDoc = new PdfDocument(writer);
// Creating a Document object
Document doc = new Document(pdfDoc);
// Creating ImageData from images on disk (from given paths) using ImageDataFactory
ImageData imageDataA = ImageDataFactory.create(currDir + "/imageA.jpg");
Image imgA = new Image(imageDataA);
ImageData imageDataB = ImageDataFactory.create(currDir + "/imageB.jpg");
Image imgB = new Image(imageDataB);
// Adding images to the document
doc.add(imgA);
doc.add(imgB);
// Close the document
doc.close();
System.out.println("Image added successfully and PDF file created!");
}
}import java.io.*;
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
String pdfPath = currDir + "/InsertImage.pdf";
// Creating PdfWriter object to write the PDF file to the path
PdfWriter writer = new PdfWriter(pdfPath);
// Creating PdfDocument object
PdfDocument pdfDoc = new PdfDocument(writer);
// Creating a Document object
Document doc = new Document(pdfDoc);
// Creating ImageData from images on disk (from given paths) using ImageDataFactory
ImageData imageDataA = ImageDataFactory.create(currDir + "/imageA.jpg");
Image imgA = new Image(imageDataA);
ImageData imageDataB = ImageDataFactory.create(currDir + "/imageB.jpg");
Image imgB = new Image(imageDataB);
// Adding images to the document
doc.add(imgA);
doc.add(imgB);
// Close the document
doc.close();
System.out.println("Image added successfully and PDF file created!");
}
}许可
iTextSharp 是开源的,并且根据 AGLP 许可。
这确保任何使用包含 iTextSharp 应用程序的人都有权获得该应用程序源代码的完整副本,即使他们通过公司网络或互联网这样做。
如果您打算将其用于业务应用,请直接联系 iText 讨论许可证定价。
IronPDF 对于开发是免费的,并且始终可以为商业部署获得许可证。 IronPDF 许可证详情 包括单项目使用、个人开发者、代理机构和全球公司,以及用于 SaaS 和 OEM 再分发。 所有许可证包括30天退款保证、一年产品支持和更新,可用于开发/演示/生产环境,还提供永久许可(一次性购买)。
Lite 套餐的起价为 $799。
- 开发人员可以无限制地使用该库进行开发。 就一般许可而言,价格非常划算。
- 免费的一年无限制支持。
- 也为许可证目的提供免费试用。
- 所有许可证均包含 30 天退款保证。
- 许可证适用于所有环境(开发、暂置、生产等)。
- 许可证包含一年的无条件支持。
- IronPDF 许可证需要一次性购买。
IronPDF 和 iText 比较
iText 和 IronPDF 之间存在几个显著差异。
iText 的 API 是围绕编程模型结构化的。 在此模型下操作 PDF 的属性和内容更低级和细粒度。 虽然这为程序员提供了更多的控制和灵活性,但也需要编写更多的代码来实现用例。
IronPDF 的 API 结构围绕优化开发人员的生产力。 IronPDF 通过允许开发人员只需几行代码即可完成这些复杂任务,从而简化 PDF 编辑、操作、创建和其他复杂任务。
结论
Iron Software 的所有客户都有选择只需两次点击即可购买整个套件的选项。 您目前可以从 Iron Software Suite 购买所有五个库以及每个库的持续支持,只需套件中两个库的价格。
常见问题解答
如何使用 Java 库将 HTML 转换为 PDF?
您可以使用 IronPDF 的 renderHtmlAsPdf 方法将 HTML 字符串转换为 PDF 文档。您还可以使用 renderHtmlFileAsPdf 方法将 HTML 文件转换为 PDF。
Java PDF 库的安装步骤是什么?
要安装 IronPDF for Java,您需要在 pom.xml 文件中声明为依赖项,或从 IronPDF Maven 仓库手动下载 .jar,并将其包含在项目的类路径中。
IronPDF for Java 如何与 iTextPDF 比较?
IronPDF 专注于通过更少的代码行简化任务,使用 Chrome 引擎进行 HTML 到 PDF 转换。iTextPDF 提供通过程序化 API 进行更细致的控制,但需要更多的代码进行操作。
我可以使用 Java PDF 库向 PDF 添加图像吗?
可以,通过 IronPDF,您可以使用 PdfDocument.fromImage 方法提供一系列图像文件路径,将图像渲染到单个 PDF 文件中。
Java PDF 库提供哪些许可选项?
IronPDF 提供灵活的许可选项,包括单项目、个人开发者、代理和企业许可证,以及 SaaS 和 OEM 重新分发选项。所有许可证都有 30 天退款保证和一年支持。
可以使用 IronPDF for Java 编辑 PDF 吗?
可以,IronPDF 允许您编辑、合并和操作 PDF 文档。它提供添加页眉、页脚及处理多种网页格式如 HTML、ASPX 和 MVC 的工具。
Java PDF 库是否有免费选项?
iTextPDF 是在 AGPL 许可下的开源,允许免费使用但需要共享使用它的应用程序的源代码。IronPDF 对于开发是免费的,但商用部署需要许可证。
在 Java 中转换 HTML 文件到 PDF 的最佳方式是什么?
IronPDF 的 renderHtmlFileAsPdf 方法允许您从本地或网络文件路径将 HTML 文件转换为 PDF 文档,为开发人员提供简化的流程。
IronPDF 提供哪些用于 PDF 生成的功能?
IronPDF 支持创建、读取和编辑 PDF,具有 HTML5、JavaScript、CSS 和图像转换为 PDF、添加页眉/页脚等功能。它专注于开发人员的生产力和易用性。
将 iTextPDF 集成到 Java 项目中需要什么?
要在您的 Java 项目中包含 iTextPDF,您需要在 pom.xml 文件中添加其 Maven 仓库详细信息,并下载必要的 .jar 文件进行集成。

















