產品比較

IronPDF for Java 和 Apache PDFBox 的比較

發佈 2023年1月14日
分享:

本文將涵蓋兩個在Java中用於處理PDF文件的最受歡迎的庫:

  1. IronPDF

  2. Apache PDFBox

現在我們應該使用哪個庫?在本文中,我將比較這兩個庫的核心功能,以便您可以決定哪一個更適合您的生產環境。

IronPDF

IronPDF 庫支持 HTML轉PDF Java 8+、Kotlin 和 Scala 的轉換器。此創作者提供跨平台支持,即 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 是一個開源 Java 庫,用於處理 PDF 文件。它允許生成、編輯和操作現有文件。它還可以從文件中提取內容。該庫提供了一些實用工具,用於對文件執行各種操作。

以下是 Apache PDFBox 的突出功能。

提取文字

  • 從檔案中提取Unicode文字。

分割和合併

  • 將單一PDF分割成多個文件
  • 合併多個文件。

填寫表單

  • 從表單中提取數據
  • 填寫PDF表單。

預檢

  • 根據 PDF/A-1b 標準驗證檔案。

列印

  • 使用標準列印 API 列印 PDF。

儲存為圖片

  • 將PDF儲存為PNG、JPEG或其他圖像類型。

創建PDF文件

  • 從頭開始製作嵌入字體和圖片的PDF。

簽署

  • 數位簽署檔案。

概述

本文的其餘部分如下:

  1. IronPDF 安裝

  2. Apache PDFBox 安裝

  3. 創建 PDF 文件

  4. 圖片轉文件

  5. 文件加密

  6. 授權

  7. 結論

現在,我們將下載並安裝這些庫來比較它們強大的功能。

1. 安裝 IronPDF

安裝 IronPDF for Java 很簡單。有多種不同的方法可以完成。本節將展示兩種最流行的方法。

1.1. 下载 JAR 并添加库

要下载 IronPDF JAR 文件,请访问 Maven 網站 並下載最新版本的IronPDF。

  • 點擊下載選項並下載JAR。
A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 1: 下載 IronPDF JAR

下載 IronPDF JAR

下載JAR檔後,現在就可以將該庫安裝到我們的Maven專案中。您可以使用任何IDE,但我們會使用Netbeans。在專案部分中:

  • 右擊Libraries資料夾,選擇 Add JAR/Folder 選項。
A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 2: 在 Netbeans 中添加 IronPDF 函式庫

在 Netbeans 中添加 IronPDF 函式庫

  • 移動到您下載 JAR 的資料夾。
  • 選擇 IronPDF JAR,然後點擊打開按鈕。
A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 3: 打開IronPDF JAR打開IronPDF JAR

打開IronPDF JAR

1.2. 作為 Maven 依賴安裝

另一種下載和安裝 IronPDF 的方法是使用 Maven。你可以簡單地在 pom.xml 中添加依賴項,或使用 Netbeans 的依賴工具將其包含在你的專案中。

在 pom.xml 中新增庫依賴項目

將以下程式碼複製並貼到 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>
XML
<dependencies>
    <dependency>  
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox-app</artifactId>
        <version>3.0.0-alpha3</version>
    </dependency>
</dependencies>
XML

這將自動下載 PDFBox 依賴項並安裝到倉庫文件夾中。現在可以使用了。

使用依赖功能添加依赖项

  • 右键点击项目部分中的依赖项
  • 选择添加依赖项并填写以下更新后的详细信息
A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 7: 添加 PDFBox 依賴

添加 PDFBox 依賴

建立 PDF 文件

3.1. 使用 IronPDF

IronPDF 提供了多種創建文件的方法。我們來看看兩個最重要的方法。

現有網址轉換成 PDF

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"));
JAVA

輸出的網址是以下格式良好的網址並保存如下:

IronPDF For Java 與 Apache PDF Box for Java 的比較 - 圖 8:新增 PDFBox 依賴項

IronPDF 網址輸出

HTML String 輸入到 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"));
JAVA

輸出如下:

A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 9: IronPDF HTML 輸出

IronPDF HTML 輸出

3.2. 使用 Apache PDFBox

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();
JAVA
A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 10: PDFBox定位輸出

PDFBox定位輸出

但是,如果我們移除 contentStream.newLineAtOffset(25, 700)從上述代碼範例中刪除 ; 然後運行項目,它會生成一個底部帶有輸出的 PDF。這對於某些開發人員來說可能相當惱人,因為他們必須使用 (x,y) 座標。y = 0 表示文本將出現在底部。

IronPDF for Java 和 Apache PDF Box for Java 的比較 - 圖表11:PDFBox 定位輸出

PDFBox 不進行定位輸出

4. 圖片轉文檔

4.1. 使用 IronPDF

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");
JAVA
IronPDF For Java 與 Apache PDF Box for Java 的比較 - 圖12:PDFBox 定位輸出

IronPDF 圖片輸出

4.2. 使用 Apache PDFBox

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);
}
JAVA
IronPDF for Java 與 Apache PDF Box for Java 的比較 - 圖13:PDFBox 圖像輸出

IronPDF 圖片輸出

5. 加密文件

5.1. 使用 IronPDF

在 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"));
JAVA

5.2. 使用 Apache PDFBox

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();
JAVA

6. 價格和授權

IronPDF 價格與授權

IronPDF 可用於開發簡單的 PDF 應用程式,並可在任何時候取得商業用途授權。IronPDF 提供單個專案授權、單個開發者授權、代理機構及跨國組織授權,以及 SaaS 和 OEM 重新分發授權和支援。所有授權均提供 免費試用,30 天內退款保證,以及 1 年的軟件支援和升級。

Lite 套裝的價格為 $749。IronPDF 產品絕對沒有任何經常性費用。更多有關軟件授權的詳細資訊可以在該產品 授權 頁面。

A Comparison between IronPDF For Java and Apache PDF Box for Java - Figure 14: IronPDF 授權

IronPDF 授權

Apache PDFBox定價和許可證

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 產品。請訪問此 網頁 了解這個驚人的優惠的更多信息。

< 上一頁
IronPDF for Java 與 Spire.PDF Java 的比較
下一個 >
IronPDF for Java 和 iTextPDF itext7 for Java 的比較

準備開始了嗎? 版本: 2024.9 剛剛發布

免費 Maven 下載 查看許可證 >