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 文檔
- PDF文件的URL
轉換影像
將圖像新增至新的 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 檔案。
簽名
對文件進行數位簽章。
概述
文章的其他部分如下:
1.安裝 IronPdf
- Apache PDFBox 安裝
- 建立 PDF 文檔
- 文件中的影像
- 文檔加密
- 許可 7.總結
現在,我們將下載並安裝這些庫,以便比較它們及其強大的功能。
1.IronPdf 安裝。
安裝適用於 Java 的 IronPDF 非常簡單。 有很多不同的方法可以做到這一點。 本節將介紹兩種最常用的方法。
1.1 下載 JAR 檔案並新增庫
若要下載 IronPDF JAR 文件,請造訪IronPDF 的 Maven 網站並下載最新版本的 IronPDF。
- 點選"下載"選項,下載 JAR 檔案。
JAR 檔案下載完成後,現在就可以將程式庫安裝到我們的 Maven 專案中了。 您可以使用任何整合開發環境 (IDE),但我們將使用 NetBeans。 在"專案"部分:
- 右鍵單擊"庫"資料夾,然後選擇"新增 JAR/資料夾"選項。
- 移動到您下載 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>
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>YOUR_VERSION_HERE</version>
</dependency>
</dependencies>使用相依性功能新增庫
- 右鍵單擊依賴項
- 選擇"新增依賴項",並使用更新後的版本填寫以下詳細資訊
現在我們來安裝 Apache PDFBox。
2. Apache PDFBox 安裝
我們可以使用與 IronPDF 相同的方法下載和安裝 PDFBox。
2.1. 下載 JAR 檔案並手動新增庫
要安裝 PDFBox JAR,請造訪官方網站並下載最新版本的 PDFBox 庫。
建立專案後,在專案部分:
- 右鍵單擊"庫"資料夾,然後選擇"新增 JAR/資料夾"選項。
- 移動到您下載 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><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"));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"));輸出結果如下,格式正確,並已儲存:
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"));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();
}
}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"));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);
}
}
}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"));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();
}
}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 再分發許可和支援。 All licenses are available with a free trial, a 30-day money-back guarantee, and one year of software support and upgrades.
Lite 套件適用於$799。 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)所缺乏的,Apache PDFBox 依靠其開發者社群的自願支援來維持其維護。 簡而言之,IronPDF 最適合用於商業和市場用途,而 Apache PDFBox 更適合個人和非商業應用。
此外,用戶還可以免費試用 IronPDF 的功能。 不妨試用一下,或是購買 IronPDF 。
現在您可以以超低價格購買 Iron Suite 套裝中的所有 Iron Software 產品。請造訪Iron Suite 網頁以了解更多關於此超值優惠的資訊。
常見問題解答
如何在 Java 中將 HTML 轉換為 PDF?
您可以使用 IronPDF 的 Java library 將 HTML 轉換為 PDF。該函式庫提供將 HTML 字串、檔案或 URL 輕鬆轉換成 PDF 的方法。
使用 IronPDF for Java 有哪些優勢?
IronPDF for Java 提供 HTML 至 PDF 轉換、圖像轉換、自訂紙張設定,以及支援網站登入和自訂 HTTP 標頭等功能。它的設計易於使用,並提供商業支援。
IronPDF 可以將圖像轉換為 PDF 嗎?
是的,IronPDF 可以將圖像轉換為 PDF。此功能可讓您以最少的工作量從各種影像格式產生 PDF 文件。
Apache PDFBox 在功能上與 IronPDF 有何不同?
雖然 Apache PDFBox 在文字萃取、表單處理和數位簽署方面表現良好,但它缺乏 HTML 到 PDF 的直接轉換。IronPDF 則提供 HTML 和 URL 到 PDF 的直接轉換,以及進階的 PDF 處理功能。
IronPdf 是否適合企業使用?
是的,IronPDF 非常適合企業使用,因為其商業支援、強大的功能和授權選項,使其成為商業應用的理想選擇。
將 HTML 轉換成 PDF 時有哪些常見問題?
常見的問題包括複雜 HTML/CSS 的不正確呈現、圖片遺失以及頁面佈局不正確。IronPDF 可利用自訂紙張設定和影像支援等功能解決這些問題。
如何將 IronPDF for Java 整合到我的 Java 專案中?
您可以透過從 Maven 下載 JAR 檔案或在專案的 pom.xml 檔案中將 IronPDF 新增為相依性,將 IronPDF 整合到您的 Java 專案中。
Apache PDFBox 用來做什麼?
Apache PDFBox 用於建立、編輯和處理 PDF 文件。它支援文字萃取、文件分割與合併、表格填寫以及數位簽章。
IronPDF 是否有相關的授權費用?
IronPDF 提供免費試用,基本開發免費,但商業使用則需要授權。有多種授權選項可供選擇,以滿足不同的需求。
為什麼有人會選擇 Apache PDFBox 而不是 IronPDF?
如果有人需要一個免費、開放源碼的解決方案供個人或非商業使用,並且不需要 HTML 轉換為 PDF,那麼他可能會選擇 Apache PDFBox 而非 IronPDF。






















