跳至頁尾內容
產品比較

IronPDF For Java 和 iTextPDF itext7 for Java 的比較

如今,由於技術的不斷進步,開發人員可以受益於更好的解決方案。

軟體開發流程的未來在於自動化。 長期以來,PDF 文件給開發人員帶來了困難。 當處理 PDFs(例如,生成內容和將其他格式的內容轉換為 PDF)時,需要考慮許多因素。 隨著旨在協助讀取、寫入、建立,甚至從多種格式轉換 PDF 的眾多庫的開發,這些需求現在得到滿足。

本文將比較兩個受 Java 開發人員歡迎的 PDF 庫,用於編輯、列印和建立 PDF 文件:

  • IronPDF Java Library:一個專注於從 HTML 生成 PDF 的 Java PDF 庫。
  • ITextPDF 庫:一個以 Java 為首的開源庫,專注於使用可編程 API 生成 PDF。

我們將在轉向將 PDF 轉換和處理的性能花費之前,檢查這兩個庫的功能,以確定哪個庫最適合您的應用程式。 此外,將紀錄每個庫的持續時間,以供以後進行研究。

安裝 IronPDF Java

要安裝 IronPDF for Java,您只需將其宣告為一個依賴項。 要定義 IronPDF 作為依賴項,請在您的 pom.xml 中新增以下內容:

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>YOUR_VERSION_HERE</version>
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>YOUR_VERSION_HERE</version>
</dependency>
XML

注意:您可以手動下載 .jar:

  1. Go to IronPDF for Java Maven Repository to access the Maven Repo page.
  2. 解壓縮 zip 文件的內容。
  3. 建立一個資料夾,並複製 zip 文件夾的內容。
  4. 打開 Eclipse IDE。
  5. 建立一個新的 Java 專案。
  6. 在類路徑中新增 IronPDF .jar 文件。
  7. 完成專案建立嚮導。 就是這樣!

IronPDF 特性

藉助強大的 IronPDF PDF 程式庫,開發人員可以快速生成、讀取和操作 PDF。 IronPDF 在其核心使用 Chrome 引擎,提供了豐富的實用和強大的功能,包括將 HTML5、JavaScript、CSS 和圖像文件轉換為 PDF 的能力。 IronPDF 也可以新增獨特的標頭和頁腳,並精確建立與網頁瀏覽器中顯示相同的 PDF 文件。 IronPDF 支援多種網頁格式,包括 HTML、ASPX、Razor View 和 MVC。 IronPDF 的關鍵屬性如下:

  • 使用 Java 程式輕鬆建立、讀取和編輯 PDF。
  • 從任何具有 User-Agents、Proxies、Cookies、HTTP Headers 和 Form Variables 設定的網站 URL 連結建立 PDF,以支援使用 HTML 登入表單的登入。
  • 從現有的 PDF 出版物中移除照片。
  • 向 PDF 新增文字、相片、書籤、水印和其他元素。
  • 合併和分割多個 PDF 文件的頁面。
  • 將媒體型別文件(包括 CSS 文件)轉換為文件。

安裝 ITextPDF Java

ITextPDF 可在 iTextPDF 官網免費獲取。 此庫是根據 AGPL 軟體許可模式開源的。

要將 iText 庫新增到您的程式中,請將以下 Maven 儲存庫納入您的 pom.xml 文件中。

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.2</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.2</version>
</dependency>
XML

直接下載 iTextPDF .jar 文件和 slf4j.jar 文件。 要使用這些庫,將 iTextPDF .jar 文件放入系統的類路徑中。

iText Library 特性

  • 從 HTML、XML 和圖像(png、jpg 等)建立 PDF。
  • 向 PDF 文件中新增書籤、頁碼和標記。
  • 將一個 PDF 文件拆分為多個 PDF,或將多個 PDF 組合為一個 PDF。
  • 編程式化編輯 PDF 表單。
  • 向 PDF 新增文字、圖片和幾何圖形。

使用 iTextPDF Java 將 HTML 字串轉換為 PDF

在 Java 中,HTMLConverter 是用於將 HTML 轉換為 PDF 的主類。

HTMLConverter 中有三個主要方法:

  • convertToDocument,返回一個 Document 物件。
  • convertToElements,返回一個 IElement 物件清單。
  • convertToPdf 處理將 HTML 內容轉換為 PDF 的任務。 此方法接受以 FileInputStream 形式提供的 HTML 輸入,並返回 OutputStreamDocument 物件。
package com.hmkcode;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.html2pdf.HtmlConverter;

public class App {
    // HTML content to be converted to PDF
    public static final String HTML = "<h1>Hello</h1>"
            + "<p>This was created using iText</p>"
            + "<a href='http://hmkcode.com'>hmkcode.com</a>";

    public static void main(String[] args) throws FileNotFoundException, IOException {
        // Convert the HTML content to PDF and save it
        HtmlConverter.convertToPdf(HTML, new FileOutputStream("string-to-pdf.pdf"));
        System.out.println("PDF Created!");
    }
}
package com.hmkcode;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.html2pdf.HtmlConverter;

public class App {
    // HTML content to be converted to PDF
    public static final String HTML = "<h1>Hello</h1>"
            + "<p>This was created using iText</p>"
            + "<a href='http://hmkcode.com'>hmkcode.com</a>";

    public static void main(String[] args) throws FileNotFoundException, IOException {
        // Convert the HTML content to PDF and save it
        HtmlConverter.convertToPdf(HTML, new FileOutputStream("string-to-pdf.pdf"));
        System.out.println("PDF Created!");
    }
}
JAVA
對比 IronPDF For Java 與 iTextPDF iText for Java - 圖1

使用 IronPDF Java 將 HTML 字串轉換為 PDF

IronPDF 的 PdfDocument 類提供多種靜態方法,允許 Java 開發人員從多種來源生成 HTML 文字。 其中一種方法是 PdfDocument.renderHtmlAsPdf 方法,它將 HTML 標記的字串轉換為 PDF 文件。

import com.ironsoftware.ironpdf.*;

import java.nio.file.Paths;

public class IronPdfExample {

    public static void main(String[] args) {
        // Set the license and log path
        License.setLicenseKey("YOUR-LICENSE-KEY");
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Convert the HTML content to a PDF
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");

        // Save the generated PDF
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    }
}
import com.ironsoftware.ironpdf.*;

import java.nio.file.Paths;

public class IronPdfExample {

    public static void main(String[] args) {
        // Set the license and log path
        License.setLicenseKey("YOUR-LICENSE-KEY");
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Convert the HTML content to a PDF
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");

        // Save the generated PDF
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    }
}
JAVA

使用 ITextPDF Java 將 HTML 文件轉換為 PDF

可以使用 convertToPdf 方法將任何 HTML 文件轉換為 PDF。

HTML 文件中可能包含圖片和 CSS 文件。然而,它們必須與 HTML 文件在相同位置。我們可以使用 ConverterProperties 類為引用的 CSS 和圖片設置基徑。 這在 HTML 文件資產位於不同目錄中時非常有用。

考慮一個名為 index.html 的文件,包含以下標記。

<html>
<head>
    <title>HTML to PDF</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <h1>HTML to PDF</h1>
    <p>
        <span class="itext">itext</span> 7.1.9 
        <span class="description"> converting HTML to PDF</span>
    </p>
    <table>
        <tr>
            <th class="label">Title</th>
            <td>iText - Java HTML to PDF</td>
        </tr>
        <tr>
            <th>URL</th>
            <td>http://hmkcode.com/itext-html-to-pdf-using-java</td>
        </tr>
    </table>
</body>
</html>
<html>
<head>
    <title>HTML to PDF</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <h1>HTML to PDF</h1>
    <p>
        <span class="itext">itext</span> 7.1.9 
        <span class="description"> converting HTML to PDF</span>
    </p>
    <table>
        <tr>
            <th class="label">Title</th>
            <td>iText - Java HTML to PDF</td>
        </tr>
        <tr>
            <th>URL</th>
            <td>http://hmkcode.com/itext-html-to-pdf-using-java</td>
        </tr>
    </table>
</body>
</html>
HTML

下述程式碼將 index.html 文件轉換為 PDF:

package com.hmkcode;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.html2pdf.HtmlConverter;

public class App {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // Convert the HTML file to PDF and save it
        HtmlConverter.convertToPdf(new FileInputStream("index.html"), 
                new FileOutputStream("index-to-pdf.pdf"));

        System.out.println("PDF Created!");
    }
}
package com.hmkcode;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.html2pdf.HtmlConverter;

public class App {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        // Convert the HTML file to PDF and save it
        HtmlConverter.convertToPdf(new FileInputStream("index.html"), 
                new FileOutputStream("index-to-pdf.pdf"));

        System.out.println("PDF Created!");
    }
}
JAVA
對比 IronPDF For Java 與 iTextPDF iText for Java - 圖2

使用 IronPDF for Java 將 HTML 文件轉換為 PDF

IronPDF 的 PdfDocument.renderHtmlFileAsPdf 方法將位於電腦或網路文件路徑上的 HTML 文件轉換為 PDF。

import com.ironsoftware.ironpdf.*;

import java.nio.file.Path;
import java.nio.file.Paths;

public class IronPdfExample {

    public static void main(String[] args) {
        // Convert the HTML file to PDF and save it
        PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
        myPdf.saveAs(Paths.get("html_file_saved.pdf"));
    }
}
import com.ironsoftware.ironpdf.*;

import java.nio.file.Path;
import java.nio.file.Paths;

public class IronPdfExample {

    public static void main(String[] args) {
        // Convert the HTML file to PDF and save it
        PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
        myPdf.saveAs(Paths.get("html_file_saved.pdf"));
    }
}
JAVA

使用 IronPDF Java 向 PDF 文件新增圖片

您可以使用 IronPDF 的 PdfDocument.fromImage 方法,將一組圖片渲染為單個 PDF 文件。接下來的範例使用此方法對位於不同文件系統路徑上的短圖片清單進行操作。

import com.ironsoftware.ironpdf.*;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class IronPdfExample {

    public static void main(String[] args) {
        // Create an ArrayList containing the list of images that you want to combine
        // into ONE PDF document
        Path imageA = Paths.get("directoryA/1.png");
        Path imageB = Paths.get("directoryB/2.png");
        Path imageC = Paths.get("3.png");
        List<Path> imageFiles = new ArrayList<>();
        imageFiles.add(imageA);
        imageFiles.add(imageB);
        imageFiles.add(imageC);

        // Convert the three images into a PDF and save them.
        PdfDocument.fromImage(imageFiles).saveAs(Paths.get("assets/composite.pdf"));
    }
}
import com.ironsoftware.ironpdf.*;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class IronPdfExample {

    public static void main(String[] args) {
        // Create an ArrayList containing the list of images that you want to combine
        // into ONE PDF document
        Path imageA = Paths.get("directoryA/1.png");
        Path imageB = Paths.get("directoryB/2.png");
        Path imageC = Paths.get("3.png");
        List<Path> imageFiles = new ArrayList<>();
        imageFiles.add(imageA);
        imageFiles.add(imageB);
        imageFiles.add(imageC);

        // Convert the three images into a PDF and save them.
        PdfDocument.fromImage(imageFiles).saveAs(Paths.get("assets/composite.pdf"));
    }
}
JAVA

使用 ITextPDF Java 向 PDFs 新增圖片

下面的完整程式碼範例使用 iText 將當前工作目錄中的兩幅圖片轉換為一個 PDF 文件。

import java.io.*;
import com.itextpdf.io.image.*;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;

public class InsertImagePDF {
    public static void main(String[] args) throws IOException {
        String currDir = System.getProperty("user.dir");

        // Getting path of current working directory to create the pdf file in the same directory
        String pdfPath = currDir + "/InsertImage.pdf";

        // Creating PdfWriter object to write the PDF file to the path
        PdfWriter writer = new PdfWriter(pdfPath);

        // Creating PdfDocument object
        PdfDocument pdfDoc = new PdfDocument(writer);

        // Creating a Document object
        Document doc = new Document(pdfDoc);

        // Creating ImageData from images on disk (from given paths) using ImageDataFactory
        ImageData imageDataA = ImageDataFactory.create(currDir + "/imageA.jpg");
        Image imgA = new Image(imageDataA);
        ImageData imageDataB = ImageDataFactory.create(currDir + "/imageB.jpg");
        Image imgB = new Image(imageDataB);

        // Adding images to the document
        doc.add(imgA);
        doc.add(imgB);

        // Close the document
        doc.close();

        System.out.println("Image added successfully and PDF file created!");
    }
}
import java.io.*;
import com.itextpdf.io.image.*;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;

public class InsertImagePDF {
    public static void main(String[] args) throws IOException {
        String currDir = System.getProperty("user.dir");

        // Getting path of current working directory to create the pdf file in the same directory
        String pdfPath = currDir + "/InsertImage.pdf";

        // Creating PdfWriter object to write the PDF file to the path
        PdfWriter writer = new PdfWriter(pdfPath);

        // Creating PdfDocument object
        PdfDocument pdfDoc = new PdfDocument(writer);

        // Creating a Document object
        Document doc = new Document(pdfDoc);

        // Creating ImageData from images on disk (from given paths) using ImageDataFactory
        ImageData imageDataA = ImageDataFactory.create(currDir + "/imageA.jpg");
        Image imgA = new Image(imageDataA);
        ImageData imageDataB = ImageDataFactory.create(currDir + "/imageB.jpg");
        Image imgB = new Image(imageDataB);

        // Adding images to the document
        doc.add(imgA);
        doc.add(imgB);

        // Close the document
        doc.close();

        System.out.println("Image added successfully and PDF file created!");
    }
}
JAVA
對比 IronPDF For Java 與 iTextPDF iText for Java - 圖3

許可

iText 是開源的,並受 AGPL 許可。

對比 IronPDF For Java 與 iTextPDF iText for Java - 圖4

這確保了任何使用包含 iText 的應用程式的人,即使在公司網路或互聯網上,也有權獲得應用程式的完整源程式碼。

對比 IronPDF For Java 與 iTextPDF iText for Java - 圖5

如果您打算將其用於商務應用,請直接聯繫 iText 以討論許可證的價格。

對比 IronPDF For Java 與 iTextPDF iText for Java - 圖6

IronPDF 對開發是免費的,並且始終可以獲得用於商業部署的許可。 IronPDF 許可詳細資訊 適用於單專案使用,個人開發人員,代理機構和全球企業,以及 SaaS 和 OEM 再分發。 所有許可證包括 30 天退款保證,一年的產品支援和更新,對開發/測試/生產的有效性,以及永久許可(一次性購買)。

Lite 套餐的價格從 $999 起。

  • 開發人員可以享受無限使用該庫以進行開發。 在一般許可方面,費率非常划算。
  • 免費的一年無限制支援。
  • 免費試用也可用於許可目的。
  • 所有許可證都有 30 天退款保證。
  • 許可證對所有環境(開發,測試,生產等)均有效。
  • 許可證包括一年的無條件支援。
  • IronPDF 許可證需要一次性購買。
對比 IronPDF For Java 與 iTextPDF iText for Java - 圖7

IronPDF 與 iText

iText 和 IronPDF 之間有幾個顯著的差異。

iText 的 API 環繞在一個程式化的模型。 在此模型下操作 PDF 屬性和內容更加低級且細化。 雖然這給予了程式員更多的控制和靈活性,也需要寫更多的程式碼來實現使用案例。

IronPDF 的 API 是圍繞優化開發者生產力而結構化的。 IronPDF 簡化了 PDF 編輯,操作,建立和其他複雜任務,允許開發人員僅用幾行程式碼就可以完成。

結論

Iron Software 的所有客戶都可以選擇通過兩次點擊購買整套包。 您目前可以從 Iron Software 套件 購買所有五個程式庫,以及對每個程式庫的持續支援,其成本僅為套件中的兩個程式庫。

請注意iTextPDF 是其相關所有者的註冊商標。 本網站與 iTextPDF 無關,未經 iTextPDF 贊助或支持。 所有產品名稱、標誌和品牌是其各自所有者的財產。 比較僅供參考,反映撰寫時的公開資訊。

常見問題

如何使用 Java 程式庫將 HTML 轉換為 PDF?

您可以使用 IronPDF 的 renderHtmlAsPdf 方法將 HTML 字串轉換成 PDF 文件。您還可以使用 renderHtmlFileAsPdf 方法將 HTML 檔案轉換為 PDF。

Java PDF 程式庫的安裝步驟是什麼?

要安裝 IronPDF for Java,您需要在 pom.xml 檔案中將其聲明為依賴項,或手動從 IronPDF Maven Repository 下載 .jar 並將其包含在專案的 class path 中。

IronPDF for Java 與 iTextPDF 有什麼比較?

IronPDF 著重於用更少的程式碼簡化任務,使用 Chrome 引擎進行 HTML 至 PDF 轉換。iTextPDF 則通過編程 API 提供更精細的控制,但需要更多程式碼進行操作。

我可以使用 Java PDF 程式庫向 PDF 新增圖片嗎?

是的,使用 IronPDF,您可以使用 PdfDocument.fromImage 方法,通過提供影像檔案路徑清單將圖像渲染至單個 PDF 檔案中。

這款 Java PDF 程式庫提供哪些授權選項?

IronPDF 提供靈活的授權選項,包括單個專案、個人開發者、代理和企業授權,以及 SaaS 和 OEM 再分發的選項。所有授權都附帶 30 天退款保證和一年支援。

IronPDF for Java 是否可以編輯 PDF ?

是的,IronPDF 允許您編輯、合併和操作 PDF 文件。它提供工具來新增頁眉、頁腳,並處理多種網頁形式如 HTML、ASPX 和 MVC。

Java PDF 程式庫是否有免費的選擇?

iTextPDF 是在 AGPL 授權下開源的,它允許免費使用但要求分享使用它的應用程式的源程式碼。IronPDF 對開發是免費的,但商業部署需要授權。

在 Java 中將 HTML 檔案轉換為 PDF 的最佳方法是什麼?

IronPDF 的 renderHtmlFileAsPdf 方法允許您將本地或網路檔案路徑的 HTML 檔案轉換為 PDF 文件,為開發者提供了簡化的流程。

IronPDF 在 PDF 生成方面有哪些功能?

IronPDF 支持建立、閱讀和編輯 PDF,具備 HTML5、JavaScript、CSS 和圖像轉為 PDF、新增頁眉/頁腳等功能。它專注於提高開發者生產力和易用性。

將 iTextPDF 整合進 Java 專案需要什麼?

要在您的 Java 專案中包含 iTextPDF,您需要在 pom.xml 文件中新增其 Maven 儲存庫資訊並下載必要的 .jar 檔案進行整合。

Darrius Serrant
全端軟體工程師(WebOps)

Darrius Serrant擁有邁阿密大學的電腦科學學士學位,並在Iron Software擔任全端WebOps行銷工程師。從小就對程式設計有興趣,他認為計算既神秘又易於理解,成為創意和問題解決的完美媒介。

在Iron Software,Darrius喜歡創造新事物並簡化複雜的概念,使其更易於理解。作為我們的常駐開發人員之一,他還志願教學,將他的專業知識傳授給下一代。

對Darrius來說,他的工作是有意義的,因為它有價值且對社會有真正的影響。

iText Logo

厭倦了昂貴的續費和過時的產品更新?

iText輕鬆切換,透過我們的工程遷移支援和更好的交易。

IronPDF Logo

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話