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 適用於 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>
注意:您可以手動下載 .jar 檔案:
- 前往IronPDF 適用於 Java Maven倉庫存取Maven倉庫頁面。
- 解壓縮檔案內容。
- 建立一個資料夾,並將壓縮資料夾的內容複製到該資料夾內。
- 開啟 Eclipse IDE。
- 建立一個新的 Java 專案。
- 將IronPDF .jar 檔案加入到類路徑中。
- 完成專案建立精靈。 就是這樣!
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>
直接下載 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 輸入為File或InputStream,並傳回OutputStream或Document物件。
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!");
}
}
使用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"));
}
}
使用 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>
以下原始程式碼將 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!");
}
}
使用IronPDF 適用於 Java 將 HTML 檔案轉換為 PDF
IronPDF 的 PdfDocument.renderHtmlFileAsPdf 方法可轉換位於電腦或網頁檔案路徑上的 HTML 檔案。
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"));
}
}
使用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"));
}
}
使用 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!");
}
}
授權
iTextSharp 是開源軟體,並根據 AGLP 授權協議授權。
這樣就確保了任何使用包含 iTextSharp 的應用程式的人都有權獲得該應用程式原始碼的完整副本,即使他們是透過公司網路或互聯網進行操作。
如果您打算將 iText 用於商業應用,請直接聯絡 iText 討論授權價格。
IronPDF可免費用於開發,並可隨時獲得商業部署許可。 IronPDF許可證詳情,適用於單一專案使用、個人開發者、代理商和全球公司,以及 SaaS 和 OEM 再分發。 所有許可證均包含 30 天退款保證、一年產品支援和更新、適用於開發/測試/生產環境,以及永久許可證(一次性購買)。
Lite套餐的價格從 $999 起。
- 開發者可以無限次使用該程式庫進行開發。 就一般許可而言,這些費率非常划算。
- 免費一年無限次技術支援。
- 也提供免費試用版,可用於授權授權。
- 所有許可證均提供 30 天退款保證。
- 許可證適用於所有環境(開發、測試、生產等)。
- 許可證包含一年的無條件支援。 IronPDF許可證需要一次購買。
IronPDF與 iText
iText 和IronPDF之間存在幾個顯著差異。
iText 的 API 是圍繞著程式化模型建構的。 在這種模式下,對 PDF 屬性和內容的操作更加底層和細粒度。 雖然這賦予了程式設計師更大的控制權和靈活性,但也需要編寫更多的程式碼來實現用例。
IronPDF 的 API 架構旨在優化開發人員的生產力。 IronPDF簡化了 PDF 的編輯、操作、建立和其他複雜任務,使開發人員只需幾行程式碼即可完成這些任務。
結論
Iron Software的所有客戶都可以選擇只需點擊兩下即可購買Suite軟體包。 目前,您只需支付Iron Software Suite中兩個庫的價格,即可購買該Suite中的全部五個庫,以及每個庫的持續支援。
常見問題解答
如何使用Java庫將HTML轉換為PDF?
您可以使用 IronPDF 的renderHtmlAsPdf方法將 HTML 字串轉換為 PDF 文件。您也可以使用renderHtmlFileAsPdf方法將 HTML 檔案轉換為 PDF。
Java PDF 函式庫的安裝步驟是什麼?
要安裝IronPDF 適用於 Java,您需要在pom.xml檔案中將其宣告為依賴項,或從IronPDF Maven 儲存庫手動下載 .jar 檔案並將其包含在專案的類別路徑中。
IronPDF for Java 與 iTextPDF 相比如何?
IronPDF專注於以更少的程式碼簡化任務,並使用 Chrome 引擎進行 HTML 到 PDF 的轉換。 iTextPDF 透過程式設計 API 提供更精細的控制,但需要編寫更多程式碼才能進行操作。
我可以使用Java PDF庫為PDF中新增圖片嗎?
是的,使用IronPDF,您可以透過提供影像檔案路徑列表,使用PdfDocument.fromImage方法將影像渲染到單一 PDF 檔案中。
Java PDF 函式庫提供哪些授權選項?
IronPDF提供靈活的許可選項,包括單一項目許可、個人開發者許可、代理商許可和企業許可,以及 SaaS 和 OEM 分發選項。所有許可證均提供 30 天退款保證和一年技術支援。
是否可以使用IronPDF 適用於 Java 編輯 PDF 檔案?
是的, IronPDF可讓您編輯、合併和操作 PDF 文件。它提供了新增頁首、頁尾以及處理多種 Web 格式(例如 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 專案中需要哪些條件?
要將 iTextPDF 新增到您的 Java 專案中,您需要在pom.xml檔案中新增其 Maven 儲存庫詳細信息,並下載必要的 .jar 檔案進行整合。











