跳過到頁腳內容
產品比較

IronPDF For Java 和 iTextPDF itext7 for Java 的比較

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

軟體開發流程的未來在於自動化。 長期以來,PDF 文件為開發人員帶來了許多難題。 在使用 PDF 檔案時(即,製作內容和將內容從其他格式轉換為 PDF),有許多需要考慮的因素。 隨著眾多旨在幫助閱讀、編寫、創建甚至轉換多種格式 PDF 的庫的開發,這些需求現在已經得到滿足。

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

  • IronPDF Java 庫:一個專注於從 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. 前往IronPDF for Java Maven 倉庫存取 Maven 倉庫頁面。
  2. 解壓縮檔案內容。
  3. 建立一個資料夾,並將壓縮資料夾的內容複製到該資料夾內。
  4. 開啟 Eclipse IDE。
  5. 建立一個新的 Java 專案。
  6. 將 IronPDF .jar 檔案加入到類路徑中。
  7. 完成專案建立精靈。 就是這樣!

IronPDF 功能。

透過功能強大的 IronPDF PDF 庫,開發人員可以快速產生、讀取和操作 PDF 文件。 IronPDF 以 Chrome 引擎為核心,提供豐富的實用強大功能,包括將 HTML5、JavaScript、CSS 和圖片檔案轉換為 PDF 的功能。 IronPDF 還可以添加獨特的頁首和頁腳,並建立與在網頁瀏覽器中顯示完全一致的 PDF 檔案。 IronPDF 支援多種 Web 格式,包括 HTML、ASPX、Razor View 和 MVC。 IronPDF的主要特點如下:

  • 使用 Java 程式輕鬆建立、讀取和編輯 PDF。
  • 從任何網站 URL 連結建立 PDF,這些連結具有 User-Agent、Proxy、Cookie、HTTP 標頭和表單變數的設置,以支援使用 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 庫功能

  • 從 HTML、XML 和圖像(png、jpg 等)建立 PDF。
  • 在 PDF 文件中新增書籤、頁碼和標記。
  • 將一個 PDF 文件拆分成多個 PDF 文件,或將多個 PDF 文件合併成一個 PDF 文件。
  • 以程式設計方式編輯 PDF 表單。
  • 在 PDF 中添加文字、圖像和幾何圖形。

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

在 iText for Java 中, HTMLConverter是用來將 HTML 轉換為 PDF 的主要類別。

HTMLConverter中有三種主要方法:

  • convertToDocument ,傳回一個Document物件。
  • convertToElements ,傳回IElement物件列表。
  • convertToPdf處理將 HTML 內容轉換為 PDF。 此方法接受 HTML 輸入作為StringFileInputStream ,並傳回FileOutputStreamDocument物件。
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 itext7 的比較 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 檔案。

圖片和 CSS 文件可以包含在 HTML 文件中。但是,它們必須與 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 itext7 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 將影像新增至 PDF

下面的完整程式碼範例使用 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 itext7 的比較 Java - 圖 3

授權

iTextSharp 是開源軟體,並根據 AGLP 授權協議授權。

IronPDF for Java 與 iTextPDF itext7 的比較 Java - 圖 4

這樣就確保了任何使用包含 iTextSharp 的應用程式的人都有權獲得該應用程式原始碼的完整副本,即使他們是透過公司網路或互聯網進行操作。

IronPDF for Java 與 iTextPDF itext7 for Java 的比較 - 圖 5

如果您打算將 iText 的產品用於商業應用,請直接聯絡 iText 洽談授權價格。

IronPDF for Java 與 iTextPDF itext7 的比較 Java - 圖 6

IronPDF 可免費用於開發,並可隨時獲得商業部署許可。 IronPDF 許可證詳情,適用於單一專案使用、個人開發者、代理商和全球公司,以及 SaaS 和 OEM 再分發。 所有授權均包含 30 天退款保證、一年的產品支援與更新、開發/暫存/生產的有效性,以及永久授權(一次性購買)。

Lite 套餐的定價從$799起。

  • 開發者可以無限次使用該程式庫進行開發。 就一般授權而言,費率非常符合成本效益。
  • 免費一年無限次技術支援。
  • 也提供免費試用版,可用於授權授權。
  • 所有許可證均提供 30 天退款保證。
  • 許可證適用於所有環境(開發、測試、生產等)。
  • 許可證包含一年的無條件支援。 IronPDF 許可證需要一次購買。
IronPDF for Java 與 iTextPDF itext7 的比較 Java - 圖 7

IronPDF 與 iText

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

iText 的 API 是圍繞著程式化模型建構的。 在這種模式下,對 PDF 屬性和內容的操作更加底層和細粒度。 雖然這賦予了程式設計師更大的控制權和靈活性,但也需要編寫更多的程式碼來實現用例。

IronPDF 的 API 架構旨在優化開發人員的生產力。 IronPDF 簡化了 PDF 的編輯、操作、創建和其他複雜任務,使開發人員只需幾行程式碼即可完成這些任務。

結論

Iron Software 的所有客戶都可以選擇只需點擊兩下即可購買整套軟體包。 目前,您只需支付Iron Software Suite中兩個庫的價格,即可購買該套件中的全部五個庫,以及每個庫的持續支援。

請注意iTextPDF 是其各自擁有者的註冊商標。 本網站與 iTextPDF 無任何關聯、背書或贊助。 所有產品名稱、標誌和品牌均為其各自擁有者的財產。 比較資料僅供參考,並反映撰寫時的公開資訊。

常見問題解答

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

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

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

若要安裝 IronPDF for Java,您需要在 pom.xml 檔案中將其宣告為依賴項目,或是手動從 IronPDF Maven Repository 下載 .jar 並將其納入專案的 class 路徑中。

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