在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
本文將涵蓋兩個在Java中用於處理PDF文件的最受歡迎的庫:
IronPDF
現在我們應該使用哪個庫?在本文中,我將比較這兩個庫的核心功能,以便您可以決定哪一個更適合您的生產環境。
這 IronPDF 庫支持 HTML轉PDF Java 8+、Kotlin 和 Scala 的轉換器。此創作者提供跨平台支持,即 Windows、Linux 或雲平台。它專為 Java 設計,優先考慮準確性、易用性和速度。
IronPDF 是為幫助軟體開發人員開發、編輯和提取 PDF 文件的內容而開發的。其成功和受歡迎程度建立在 IronPDF for .NET 的基礎上。
IronPDF 的突出功能包括:
字體 (網頁與圖示)
Apache PDFBox 是一個開源 Java 庫,用於處理 PDF 文件。它允許生成、編輯和操作現有文件。它還可以從文件中提取內容。該庫提供了一些實用工具,用於對文件執行各種操作。
以下是 Apache PDFBox 的突出功能。
本文的其餘部分如下:
IronPDF 安裝
Apache PDFBox 安裝
創建 PDF 文件
圖片轉文件
文件加密
授權
現在,我們將下載並安裝這些庫來比較它們強大的功能。
安裝 IronPDF for Java 很簡單。有多種不同的方法可以完成。本節將展示兩種最流行的方法。
要下载 IronPDF JAR 文件,请访问 Maven 網站 並下載最新版本的IronPDF。
下載JAR檔後,現在就可以將該庫安裝到我們的Maven專案中。您可以使用任何IDE,但我們會使用Netbeans。在專案部分中:
另一種下載和安裝 IronPDF 的方法是使用 Maven。你可以簡單地在 pom.xml 中添加依賴項,或使用 Netbeans 的依賴工具將其包含在你的專案中。
將以下程式碼複製並貼到 pom.xml 中。
請提供內容以進行翻譯。
#### 使用依賴項功能添加庫
- 右鍵點擊依賴項
- 選擇添加依賴項,並填寫以下更新的版本詳細信息
<div class="content-img-align-center">
<div class="center-image-wrapper">
<a rel="nofollow" href="/static-assets/ironpdf-java/blog/apache-pdfbox-html-to-pdf-java/apache-pdfbox-html-to-pdf-java-4.webp" target="_blank"><img src="/static-assets/ironpdf-java/blog/apache-pdfbox-html-to-pdf-java/apache-pdfbox-html-to-pdf-java-4.webp" alt="A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 4: 添加 IronPDF 依賴性" class="img-responsive add-shadow"></a>
<p class="content__image-caption">添加 IronPDF 依賴性</p>
</div>
</div>
現在讓我們安裝 Apache PDFBox。
## 2. 安裝 Apache PDFBox
我們可以使用與 IronPDF 相同的方法下載並安裝 PDFBox。
### 2.1. 下載JAR並手動添加庫
要安裝PDFBox JAR,請訪問官方網站並 [下載](https://pdfbox.apache.org/) 最新版本。
建立專案後,在專案部分:
- 右鍵點擊 Libraries 資料夾並選擇 **Add JAR/Folder** 選項。
<div class="content-img-align-center">
<div class="center-image-wrapper">
<a rel="nofollow" href="/static-assets/ironpdf-java/blog/apache-pdfbox-html-to-pdf-java/apache-pdfbox-html-to-pdf-java-5.webp" target="_blank"><img src="/static-assets/ironpdf-java/blog/apache-pdfbox-html-to-pdf-java/apache-pdfbox-html-to-pdf-java-5.webp" alt="A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 5: 新增庫" class="img-responsive add-shadow"></a>
<p class="content__image-caption">新增庫</p>
</div>
</div>
- 移動到您下載 JAR 的資料夾。
- 選擇 PDFBox JAR 並點擊打開按鈕。
<div class="content-img-align-center">
<div class="center-image-wrapper">
<a rel="nofollow" href="/static-assets/ironpdf-java/blog/apache-pdfbox-html-to-pdf-java/apache-pdfbox-html-to-pdf-java-6.webp" target="_blank"><img src="/static-assets/ironpdf-java/blog/apache-pdfbox-html-to-pdf-java/apache-pdfbox-html-to-pdf-java-6.webp" alt="A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 6: 開啟 PDFBox JAR" class="img-responsive add-shadow"></a>
<p class="content__image-caption">開啟 PDFBox JAR</p>
</div>
</div>
### 2.2. 通過 Maven 作為依賴項安裝
#### 在 pom.xml 中添加依赖項
請複製以下代碼並將其粘貼到 pom.xml 中。
```xml
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>1.0.0</version>
</dependency>
<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"));
輸出的網址是以下格式良好的網址並保存如下:
以下範例程式碼顯示如何使用 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 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 天內退款保證,以及 1 年的軟件支援和升級。
Lite 套裝的價格為 $749。IronPDF 產品絕對沒有任何經常性費用。更多有關軟件授權的詳細資訊可以在該產品 授權 頁面。
Apache PDFBox免費提供,無論其用途為何,不收取任何費用。 無論是個人用途、內部用途或商業用途,均免費。
您可以包括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 還提供免費試用版來測試其功能。 試試看 或 立即購買您現在可以以大幅折扣價格購買 Iron Suite 中的所有 Iron Software 產品。請訪問此 網頁 了解這個驚人的優惠的更多信息。