
IronPDF for Java 与 Apache PDFBox 的比较
本文将介绍在Java中处理PDF文件的两大最受欢迎的库:
- IronPDF
- Apache PDFBox
现在我们应该使用哪个库? 在本文中,我将比较两个库的核心功能,以帮助您决定哪个最适合您的生产环境。
如何在 Apache PDFBOX 中将 HTML 转换为 PDF
- 安装 Java 库将 HTML 转换为 PDF
- 使用 Apache PDFBox 创建新文档和页面实例
- 以文档和页面为输入创建新的
PDPageContentStream - 使用
PDPageContentStream实例配置和添加内容 - 使用
save方法导出 PDF 文档
IronPDF
IronPDF 库支持Java 8+、Kotlin和Scala的HTML到PDF转换。 此创建者提供跨平台支持,即Windows、Linux或云平台。 它特别为Java设计,优先考虑准确性、易用性和速度。
IronPDF开发的目的是帮助软件开发人员创建、编辑和提取PDF文档中的内容。 它基于IronPDF for .NET的成功和受欢迎。
IronPDF 的突出功能包括
使用HTML资产
- HTML(5及以下)、CSS(屏幕和打印)、图像(JPG、PNG、GIF、TIFF、SVG、BMP)、JavaScript(+渲染延迟)
- 字体(网络和图标)
HTML到PDF
- HTML文件/字符串到PDF文档的创建和操作
- URL到PDF
转换图像
- 图像转为新的PDF文档
- PDF到图像
自定义纸张设置
- 自定义纸张尺寸、方向和旋转
- 边距(毫米、英寸和零)
- 彩色和灰度、分辨率和JPEG质量
附加功能
- 网站和系统登录
- 自定义用户代理和代理服务器
- HTTP头
Apache PDFBox库
Apache PDFBox是一个用于处理PDF文件的开源Java库。 它允许生成、编辑和操作现有文档。 它还可以从文件中提取内容。 该库提供了多种实用工具,用于对文档执行各种操作。
以下是Apache PDFBox的突出特点。
提取文本
- 从文件中提取Unicode文本。
拆分与合并
- 将单个PDF拆分成多个文件
- 合并多个文档。
填写表单
- 从表单中提取数据
- 填写PDF表单。
预检
- 根据PDF/A-1b标准对文件进行验证。
打印
- 使用标准打印API打印PDF。
保存为图像
- 将PDF保存为PNG、JPEG或其他图像类型。
创建PDF
- 从头开始开发一个包含嵌入字体和图像的PDF。
签名
- 数字签署文件。
概述
文章的其余部分如下:
- IronPDF 安装
- Apache PDFBox安装
- 创建PDF文档
- 图像到文档
- 加密文档
- 授权 7.结论
现在我们将下载并安装库以比较它们强大的功能。
1.IronPDF 安装
安装IronPDF for Java很简单。 有多种方法可以执行此操作。 本节将演示两种最流行的方法。
1.1. 下载JAR并添加库
要下载IronPDF JAR文件,请访问Maven网站的IronPDF并下载最新版本的IronPDF。
- 单击"下载"选项并下载JAR。
下载JAR后,现在是时候将库安装到我们的Maven项目中。 您可以使用任何IDE,但我们将使用NetBeans。 在项目部分:
- 右键单击"Libraries"文件夹并选择Add JAR/Folder选项。
- 转到您下载JAR的文件夹。
- 选择IronPDF JAR并单击"打开"按钮。
1.2. 通过Maven作为依赖项安装
下载和安装IronPDF的另一种方法是使用Maven。 您可以简单地在pom.xml中添加依赖项或使用NetBeans的依赖项工具将其包含在项目中。
在pom.xml中添加库依赖项
在您的pom.xml中添加以下依赖项:
<dependencies>
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>YOUR_VERSION_HERE</version>
</dependency>
</dependencies>
使用依赖功能添加库
- 右键点击dependencies
- 选择"添加依赖项",并用最新版本填写以下详细信息
现在让我们安装Apache PDFBox。
2. Apache PDFBox安装
我们可以使用与IronPDF相同的方法下载和安装PDFBox。
2.1. 下载JAR并手动添加库
要安装PDFBox JAR,请访问官方网站并下载PDFBox库的最新版本。
在创建项目后,在项目部分:
- 右键单击"Libraries"文件夹并选择Add JAR/Folder选项。
- 转到您下载JAR的文件夹。
- 选中PDFBox JAR并点击"打开"按钮。
2.2. 通过Maven作为依赖项安装
在pom.xml中添加依赖项
复制以下代码并粘贴到pom.xml中。
<dependencies>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-app</artifactId>
<version>3.0.0-alpha3</version>
</dependency>
</dependencies>
这将自动下载PDFBox依赖项并将其安装到存储库文件夹中。 现在它就可以使用了。
使用依赖功能添加依赖项
- 在项目部分右键依赖项
- 选择"添加依赖项",并用最新版本填写以下详细信息
3. 创建PDF文档
3.1.使用 IronPDF.
IronPDF提供了不同的方法来创建文件。 让我们看看两个最重要的方法。
将现有 URL 转换为 PDF.
IronPDF使从HTML生成文档变得非常简单。 下面的代码示例将网页的URL转换为PDF。
import com.ironsoftware.ironpdf.*;
import java.nio.file.Paths;
// Set the license key and log path
License.setLicenseKey("YOUR-LICENSE-KEY");
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Convert a URL to a PDF
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
// Save the PDF document to a file
myPdf.saveAs(Paths.get("url.pdf"));
输出是以下URL,格式良好并保存如下:
将 HTML 输入字符串转换为 PDF 文件
以下示例代码展示了如何使用HTML字符串在Java中渲染PDF。 您可以简单地使用HTML字符串或文档来将其转换为新文档。
import com.ironsoftware.ironpdf.*;
import java.nio.file.Paths;
// Set the license key and log path
License.setLicenseKey("YOUR-LICENSE-KEY");
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Convert an HTML string to a PDF
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
// Save the PDF document to a file
myPdf.saveAs(Paths.get("html_saved.pdf"));
译文如下:
3.2. 使用Apache PDFBox
PDFBox也可以从不同的格式生成新的PDF文档,但它不能直接从URL或HTML字符串转换。
以下代码示例创建了一个带有一些文本的文档:
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.common.*;
import org.apache.pdfbox.pdmodel.font.*;
import org.apache.pdfbox.pdmodel.graphics.image.*;
import org.apache.pdfbox.pdmodel.interactive.annotation.*;
import org.apache.pdfbox.pdmodel.interactive.form.*;
import java.io.IOException;
public class PDFBoxExample {
public static void main(String[] args) throws IOException {
// Create a document object
PDDocument document = new PDDocument();
// Add a blank page to the document
PDPage blankPage = new PDPage();
document.addPage(blankPage);
// Retrieve the page of the document
PDPage paper = document.getPage(0);
try (PDPageContentStream contentStream = new PDPageContentStream(document, paper)) {
// Begin the content stream
contentStream.beginText();
// Set the font to the content stream
contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
// Set the position for the line
contentStream.newLineAtOffset(25, 700);
String text = "This is the sample document and we are adding content to it.";
// Add text in the form of a string
contentStream.showText(text);
// End the content stream
contentStream.endText();
System.out.println("Content added");
// Save the document
document.save("C:/PdfBox_Examples/my_doc.pdf");
System.out.println("PDF created");
}
// Closing the document
document.close();
}
}
然而,如果我们从上述代码示例中移除contentStream.newLineAtOffset(25, 700);然后运行项目,它会生成一个在页面底部输出的PDF。 这对于某些开发人员来说可能会感到相当烦人,因为他们必须使用(x,y)坐标来调整文本。 y = 0意味着文本将出现在底部。
4. 图像到文档
4.1. 使用IronPDF
IronPDF可以轻松地将多个图像转换为一个单独的PDF。 将多个图像添加到单个文档的代码如下:
import com.ironsoftware.ironpdf.*;
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(Paths.get("output.pdf"));
4.2. 使用Apache PDFBox
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.graphics.image.*;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
public class ImageToPdf {
public static void main(String[] args) {
// 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);
// Create PDImageXObject object
PDImageXObject pdImage = PDImageXObject.createFromFile(imageFiles.get(i).toString(), doc);
// Create 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);
}
}
}
5. 加密文档
5.1.使用 IronPDF.
在IronPDF中为PDF加密的代码如下:
import com.ironsoftware.ironpdf.*;
import java.nio.file.Paths;
// 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"));
5.2. 使用Apache PDFBox
Apache PDFBox还提供文档加密以使文件更安全。 您还可以添加其他信息,如元数据。 代码如下
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.encryption.*;
import java.io.File;
import java.io.IOException;
public class PDFEncryption {
public static void main(String[] args) throws IOException {
// Load an existing document
File file = new File("C:/PdfBox_Examples/sample.pdf");
PDDocument document = PDDocument.load(file);
// Create access permission object
AccessPermission ap = new AccessPermission();
// Create StandardProtectionPolicy object
StandardProtectionPolicy spp = new StandardProtectionPolicy("1234", "1234", ap);
// Setting the length of the encryption key
spp.setEncryptionKeyLength(128);
// Set the access permissions
spp.setPermissions(ap);
// Protect the document
document.protect(spp);
System.out.println("Document encrypted");
// Save the document
document.save("C:/PdfBox_Examples/encrypted.pdf");
// Close the document
document.close();
}
}
6.定价和许可
IronPDF。 定价和许可
IronPDF可以免费用于开发简单的PDF应用程序,并可随时获得商业使用许可。IronPDF提供单个项目许可证、单个开发人员许可证、适用于代理机构和跨国公司的许可证,以及 SaaS 和 OEM 再分发许可和支持。 所有许可证都提供免费试用、30 天退款保证和一年的软件支持和升级。
Lite套餐已可用于$999。 IronPDF产品绝对没有任何经常性费用。 关于软件许可的更详细信息,请参阅产品IronPDF 许可页。
Apache PDFBox 定价和许可
Apache PDFBox 是免费提供的,无需任何收费。 无论用于个人、内部还是商业用途,都是免费的。
您可以从Apache License 2.0 文本中包含Apache License 2.0(当前版本)。 要包含许可副本,只需在您的工作中包含它。 您还可以将以下通知作为注释附加在源代码的顶部。
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.
结论
相比之下,IronPDF在功能和产品支持两方面都比Apache PDFBox更具优势。 它还提供SaaS和OEM支持,这是现代软件开发的要求。 但是,这个库不像Apache PDFBox那样免费供商业使用。
大型软件应用的公司可能需要来自第三方供应商的持续错误修复和支持,以解决在软件开发过程中出现的问题。 这是许多像Apache PDFBox这样的开源解决方案所缺乏的,这些解决方案依赖于其开发者社区的志愿支持来维持。 总之,IronPDF更适合于商业和市场使用,而Apache PDFBox更适合于个人和非商业用途。
此外还有免费试用来测试IronPDF的功能。 试一试或购买 IronPDF。
您现在可以在Iron Suite中以大幅折扣价获得所有Iron Software产品。访问这个Iron Suite网页以了解更多关于这个惊人交易的信息。

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.
Related Articles
















