在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
本文将介绍 Java 中用于处理 PDF 文件的以下两个最常用的库:
IronPDF
Apache PDFBox
现在我们应该使用哪个库? 在本文中,我将比较这两个库的核心功能,以便您决定哪一个最适合您的生产环境。
PDPageContentStream
PDPageContentStream
实例进行配置和添加内容save
方法导出PDF文档IronPDF 库支持 Java 8+、Kotlin 和 Scala 的 HTML 到 PDF 转换。 该创建者提供跨平台支持,即 Windows、Linux 或云平台。 它特别针对优先考虑准确性、易用性和速度的 Java 而设计。
IronPDF 的开发目的是帮助软件开发人员创建、编辑和提取 PDF 文档中的内容。 它基于 IronPDF for .NET 的成功和受欢迎程度。
IronPDF 的突出功能包括
Apache PDFBox 是一个用于处理 PDF 文件的开源 Java 库。 它允许人们生成、编辑和操作现有文档。 它还可以从文件中提取内容。 该库提供了多个实用程序,用于对文档执行各种操作。
以下是 Apache PDFBox 的突出特点。
文章的其余部分如下:
IronPDF 安装
Apache PDFBox 安装
创建 PDF 文档
文档图片
加密文档
许可
结论
现在,我们将下载并安装这些库,以比较它们及其强大的功能。
IronPDF for Java 的安装非常简单。 有多种不同的方法。 本节将展示两种最常用的方法。
要下载 IronPDF JAR 文件,请访问 Maven 的 IronPDF 网站 下载最新版本的 IronPDF。
单击下载选项并下载 JAR。
下载 JAR 后,现在就需要将库安装到我们的 Maven 项目中。 您可以使用任何集成开发环境,但我们将使用 NetBeans。 在项目部分:
另一种下载和安装 IronPDF 的方法是使用 Maven。 您只需在 pom.xml 中添加依赖关系,或使用 NetBeans 的依赖关系工具将其包含在项目中即可。
复制以下代码并将其粘贴到 pom.xml 中。
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>com.ironsoftware</artifactId>
<version>2025.3.6</version>
</dependency>
现在,让我们安装 Apache PDFBox。
我们可以使用与 IronPDF 相同的方法下载和安装 PDFBox。
要安装PDFBox JAR,请访问官方网站并下载最新版本的PDFBox库。
创建项目后,在项目部分:
复制以下代码并将其粘贴到 pom.xml 中。
<dependencies>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-app</artifactId>
<version>3.0.0-alpha3</version>
</dependency>
</dependencies>
这将自动下载 PDFBox 依赖项并将其安装到存储库文件夹中。 现在即可使用。
IronPDF 提供不同的文件创建方法。 让我们来看看其中两种最重要的方法。
IronPDF 使 从 HTML 生成文档 非常简单。 以下代码示例将网页的 URL 转换为 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"));
输出结果为以下格式良好的 URL,并保存如下:
以下示例代码展示了如何使用 HTML 字符串在 Java 中渲染 PDF。 您只需使用 HTML 字符串或文档将其转换为新文档即可。
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"));
输出结果如下
PDFBox 还可以从不同格式生成新的 PDF 文档,但不能直接从 URL 或 HTML 字符串进行转换。
下面的代码示例创建了一个包含一些文本的文档:
//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 a 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();
但是,如果我们从上述代码示例中移除contentStream.newLineAtOffset(25, 700);
,然后运行项目,它会生成一个输出在页面底部的PDF。 这对一些开发人员来说可能相当恼人,因为他们必须使用 (x,y) 坐标来调整文本。 y = 0
表示文本将出现在底部。
IronPDF 可以轻松地将多个图像转换为一个 PDF。 在单个文档中添加多个图像的代码如下:
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);
}
下面给出了在 IronPDF 中使用密码加密 PDF 的代码:
// 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 还提供文档加密功能,使文件更加安全。 您还可以添加元数据等其他信息。 代码如下
//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 可免费用于开发简单的 PDF 应用程序,也可随时授权用于商业用途。IronPDF 提供单个项目许可、单个开发人员许可、面向机构和跨国组织的许可,以及 SaaS 和 OEM 再分发许可和支持。 所有许可证均提供免费试用、30天退款保证,以及一年的软件支持和升级。
Lite包的价格为$749。 IronPDF产品绝无任何循环费用。 有关软件许可的更多详细信息,请访问产品IronPDF许可页面。
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 网页了解更多关于此诱人交易的信息。