IronPDF for Java 與 OpenPDF 的比較
發現最佳的Java應用程式PDF程式庫
在本文中,我們將探索如何通過Java PDF程式庫提升您的專案,並根據其功能、授權成本和教程來比較兩個頂級程式庫。 過去,在Java中使用PDF檔案是很有挑戰性的,但隨著技術的進步,現在有許多Java API可供管理。
我們將要檢視的兩個程式庫是:
這兩個程式庫都提供在Java中建立、編輯和列印PDF檔案的能力。 在此次比較中,我們將簡明地評估它們的綜合能力、程式碼庫以及安裝的簡易性。
首先,我們來看看IronPDF。
IronPDF for Java
IronPDF for Java建立和管理PDF檔案。 它允許開發人員閱讀、建立和編輯PDF檔案而不需要Adobe軟體。 開發人員可以新增自訂的頁眉和頁腳、新增簽名和其他安全限制。 全面的多執行緒和異步支援幫助IronPDF在開發人員需要時達到高性能。
IronPDF功能
IronPDF for Java提供多種PDF製作和操作工具,讓開發人員能夠快速提升Java應用程式的速度。
-
HTML到PDF轉換:此功能允許從HTML檔案、MVC視圖、網頁表單和URL生成PDF文件。
-
PDF成像:允許開發人員從影像建立PDF,並從PDF中提取影像。
-
PDF編輯:可以新增浮水印、頁眉、頁腳、背景和前景,新增和刪除頁面等等。
-
PDF內容提取:可以從PDF中提取文字和影像(從嵌入PDF影像中提取文字可能需要使用IronOCR程式庫)。
- 相容性:與Java 8+及所有現代操作系統和IDE相容。
OpenPDF
OpenPDF是一個開源的、免費的Java程式庫,具有雙重LGPL和MPL授權,專為處理PDF而設計。 它允許生成和操作PDF檔案,以及編輯現有文件和提取內容。 OpenPDF是處理PDF文件時的便利工具,無論是建立新文件還是編輯現有文件。
OpenPDF功能
-
建立PDF與列印:讓您從頭建立新的PDF文件,並使用Java列印API列印PDF文件。
-
分割與合併:可以將單個PDF拆分成多個文件,或將多個PDF文件合併成單個PDF文件。
-
提取文字:讓您從PDF文件和PDF表單中提取文字。
-
簽名:允許開發人員以數位方式簽署PDF。
-
保存為影像:可以將PDF保存為影像文件,如JPEG或PNG。
-
Office到PDF:可以將MS Word, MS PowerPoint, 及MS Excel轉換為PDF文件。
- 解析HTML:它還提供將HTML文件解析為PDF文件的功能。
IronPDF for Java安裝
安裝IronPDF for Java是一個簡單的過程,即使是新手Java開發人員也不例外。
要使用IronPDF for Java,您需要一個IDE。本文中,我們將使用JetBrains IntelliJ IDEA進行安裝和範例。
首先,打開JetBrains IntelliJ IDEA並建立一個新的Maven專案:
- 會出現一個新窗口,您需輸入專案名稱並點擊"完成"按鈕。
- 點擊"完成"後,會打開一個新專案並且預設的
pom.xml文件會被打開。 這很好,因為我們需要在此文件中新增IronPDF for Java的Maven依賴項。
將以下依賴項新增到pom.xml文件中:
<dependencies>
</dependencies>
<dependencies>
</dependencies>
一旦您將依賴項新增到pom.xml文件的右上角會出現一個小圖標。點擊此圖標以安裝IronPDF for Java的Maven依賴項。 此過程可能需要幾分鐘,具體取決於您的網路連線速度。
OpenPDF安裝
安裝OpenPDF與安裝IronPDF for Java類似。 按照以下步驟進行:
- 打開JetBrains IntelliJ IDEA並建立一個新的Maven專案。
- 輸入專案名稱並點擊"完成"按鈕。
- 會打開一個新的專案並顯示預設的
pom.xml文件。 這很好,因為您需要新增OpenPDF的Maven依賴項。
將OpenPDF依賴項新增到pom.xml部分:
<dependencies>
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.30</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.30</version>
</dependency>
</dependencies>
當您新增了儲存庫和依賴項程式碼後,pom.xml文件的右上角會出現一個小圖標。點擊該圖標以安裝OpenPDF for Java依賴項。 幾分鐘後,它將被安裝並可供使用。
建立新PDF文件
除了對使用者的吸引力外,PDF還提供多種技術優勢。 PDF文件是平台獨立的,可以在任何操作系統或裝置上閱讀。它們還保留格式和佈局,確保無論誰打開文件,它的外觀都相同。 PDF文件也很緊湊,便於分享和儲存。 通過在您的Java應用程式中使用PDF,您可以為使用者提供一種高效且受支援的文件格式,提供廣泛的功能和相容性。
在這裡,我們將討論如何使用IronPDF和OpenPDF程式庫建立一個新的PDF文件。
使用IronPDF建立新PDF文件
使用IronPDF for Java建立和編輯PDF文件非常簡單,只需幾行程式碼即可完成。 以下是一個範例:
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render a simple HTML to PDF
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
// Save the rendered PDF
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render a simple HTML to PDF
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
// Save the rendered PDF
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}
使用OpenPDF建立新PDF文件
使用OpenPDF可以建立一個新的PDF文件。 這裡是一個程式碼範例:
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class Main {
/**
* Generates a PDF file with the text 'Hello World'
*/
public static void main(String[] args) {
System.out.println("Generating Hello World PDF");
Document document = new Document();
try (FileOutputStream fileOutputStream = new FileOutputStream("HelloWorld.pdf")) {
PdfWriter.getInstance(document, fileOutputStream);
// Open document for writing content
document.open();
// Add content to the PDF
document.add(new Paragraph("Hello World"));
} catch (DocumentException | IOException e) {
System.err.println("Error creating PDF: " + e.getMessage());
} finally {
// Ensure document is closed
document.close();
}
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class Main {
/**
* Generates a PDF file with the text 'Hello World'
*/
public static void main(String[] args) {
System.out.println("Generating Hello World PDF");
Document document = new Document();
try (FileOutputStream fileOutputStream = new FileOutputStream("HelloWorld.pdf")) {
PdfWriter.getInstance(document, fileOutputStream);
// Open document for writing content
document.open();
// Add content to the PDF
document.add(new Paragraph("Hello World"));
} catch (DocumentException | IOException e) {
System.err.println("Error creating PDF: " + e.getMessage());
} finally {
// Ensure document is closed
document.close();
}
}
}
將HTML轉換為PDF
人們經常存取網頁。 有幾個網站您可能想經常查看。 每次存取網站可能不太可行。 如果您需要經常存取資訊,將其儲存為一個文件更方便,這樣您可以隨時從手機或筆記型電腦上存取。PDF格式是一個更好的選擇,因為它提供了密碼保護的好處,讓文件更加安全。
將HTML轉換為PDF是PDF程式庫中最常用的功能之一,幾乎所有開發者都因其好處而使用。 在本節中,我們將討論IronPDF for Java和OpenPDF的程式碼範例。
使用IronPDF將HTML轉換為PDF
IronPDF的先進渲染器提供了無縫的HTML到PDF轉換,通過三種不同的方法。
HTML文件轉PDF
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render HTML file to PDF
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("index.html");
// Save the rendered PDF
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render HTML file to PDF
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("index.html");
// Save the rendered PDF
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
}
}
HTML字串轉PDF
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render HTML string to PDF
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> Example of HTML to PDF using IronPDF for Java </h1> IronPDF for Java is a robust Java API for creating, converting, and manipulating PDF files");
// Save the rendered PDF
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render HTML string to PDF
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> Example of HTML to PDF using IronPDF for Java </h1> IronPDF for Java is a robust Java API for creating, converting, and manipulating PDF files");
// Save the rendered PDF
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}
URL轉PDF
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render URL to PDF
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20");
try {
// Save the rendered PDF
myPdf.saveAs(Paths.get("url.pdf"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
// Set your license key for IronPDF
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set the log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render URL to PDF
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20");
try {
// Save the rendered PDF
myPdf.saveAs(Paths.get("url.pdf"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用OpenPDF將HTML轉換為PDF
OpenPDF提供將HTML文件解析為PDF文件的選項,但缺乏將URL轉換為PDF的功能。 這裡是一個例子:
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.html.HtmlParser;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
/**
* Parses HTML and generates a PDF file
*/
public static void main(String[] args) {
System.out.println("Parsing HTML to create PDF");
try (Document document = new Document()) {
FileOutputStream outputStream = new FileOutputStream("contact.pdf");
PdfWriter.getInstance(document, outputStream);
// Open the document
document.open();
// Parse the HTML document and write to PDF
HtmlParser.parse(document, Main.class.getClassLoader().getResourceAsStream("contact.html"));
} catch (DocumentException | IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.html.HtmlParser;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
/**
* Parses HTML and generates a PDF file
*/
public static void main(String[] args) {
System.out.println("Parsing HTML to create PDF");
try (Document document = new Document()) {
FileOutputStream outputStream = new FileOutputStream("contact.pdf");
PdfWriter.getInstance(document, outputStream);
// Open the document
document.open();
// Parse the HTML document and write to PDF
HtmlParser.parse(document, Main.class.getClassLoader().getResourceAsStream("contact.html"));
} catch (DocumentException | IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}
圖像到PDF轉換器
將圖像轉換為PDF是有優勢的。 它可以將照片變成更易讀和可傳輸的格式,並在保持圖像品質的同時大大減少文件大小。
使用IronPDF的圖像到PDF轉換器
使用IronPDF,您可以輕鬆地將任何圖像格式轉換為PDF文件。
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Path to the directory containing images
Path imageDirectory = Paths.get("assets/images");
List<Path> imageFiles = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(imageDirectory, "*.{png,jpg}")) {
for (Path entry : stream) {
imageFiles.add(entry);
}
// Convert images to a single PDF document
PdfDocument.fromImage(imageFiles).saveAs(Paths.get("assets/composite.pdf"));
} catch (IOException exception) {
throw new RuntimeException("Error converting images to PDF from directory: " + imageDirectory + ": " + exception.getMessage(), exception);
}
}
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Path to the directory containing images
Path imageDirectory = Paths.get("assets/images");
List<Path> imageFiles = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(imageDirectory, "*.{png,jpg}")) {
for (Path entry : stream) {
imageFiles.add(entry);
}
// Convert images to a single PDF document
PdfDocument.fromImage(imageFiles).saveAs(Paths.get("assets/composite.pdf"));
} catch (IOException exception) {
throw new RuntimeException("Error converting images to PDF from directory: " + imageDirectory + ": " + exception.getMessage(), exception);
}
}
}
使用OpenPDF的圖像到PDF轉換器
OpenPDF提供圖像到PDF的轉換,但僅支援PNG、JPG和TIFF格式。
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfWriter;
public class Main {
public static void main(String[] args) {
System.out.println("Converting Images to PDF");
// Step 1: Create a document object
Document document = new Document();
try {
// Step 2: Create a PdfWriter that listens to the document
// and directs a PDF-stream to a file
FileOutputStream fileOutputStream = new FileOutputStream("Images.pdf");
PdfWriter.getInstance(document, fileOutputStream);
// Step 3: Open the document
document.open();
// Step 4: Add images to the PDF
Image jpg = Image.getInstance("11.png");
document.add(jpg);
} catch (DocumentException | IOException e) {
System.err.println("Error creating PDF: " + e.getMessage());
} finally {
// Step 5: Close the document
document.close();
}
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfWriter;
public class Main {
public static void main(String[] args) {
System.out.println("Converting Images to PDF");
// Step 1: Create a document object
Document document = new Document();
try {
// Step 2: Create a PdfWriter that listens to the document
// and directs a PDF-stream to a file
FileOutputStream fileOutputStream = new FileOutputStream("Images.pdf");
PdfWriter.getInstance(document, fileOutputStream);
// Step 3: Open the document
document.open();
// Step 4: Add images to the PDF
Image jpg = Image.getInstance("11.png");
document.add(jpg);
} catch (DocumentException | IOException e) {
System.err.println("Error creating PDF: " + e.getMessage());
} finally {
// Step 5: Close the document
document.close();
}
}
}
定價和授權
IronPDF for Java是一個強大的PDF程式庫,可用於個人和商業目的。 它提供多種授權選擇,包含單一專案授權、SaaS和OEM再分發,以及跨國公司授權。 Lite套餐的費用從USD開始,其中包括永久授權、30天退款保證以及一年軟體支援和升級。 IronPDF的一大優勢是沒有經常性的成本,這意味著一旦購買,授權可以用於終身使用。
OpenPDF是開源軟體,根據LGPL和MPL開源授權條款。 這意味著任何使用包含OpenPDF應用程式的人(即使是在業務網路或網際網路上),如果根據LGPL和MPL授權,有可能獲得程式完整源程式碼的權利,這使其非常適合學術用途。 然而,對於商業專案,通常建議聯絡OpenPDF以估算任何相關成本。
結論
Java開發人員和IT專業人員可以輕鬆地使用IronPDF程式庫將PDF功能整合整合到其Java應用程式中。 它提供廣泛的功能,包括格式化PDF文件、生成圖表和圖形、將HTML和圖像轉換為PDF、分割和合併PDF文件,以及修改PDF文件。 IronPDF支援從Java 8開始的所有Java版本及JVM語言(如Java、Kotlin和Scala)。 該程式庫還配備了增強的PDF檔案安全功能。
OpenPDF是一款具備LGPL和MPL授權的開源免費Java程式庫。 它允許建立和修改PDF文件,並從中提取內容。 儘管OpenPDF在生成和編輯PDF文件方面很有用,但其操作PDF文件的功能相比IronPDF有限。
僅以授權來比較IronPDF和OpenPDF是不可能的,因為一個是商業程式庫,另一個是開源的。 然而,就功能而言,OpenPDF在操作PDF文件方面的選項有限。 另一方面,IronPDF for Java為開發者提供免費試用授權以測試程式庫及其先進功能。
IronPDF提供比OpenPDF更多的功能。 此外,IronPDF提供了大量的文件,這簡化了程式庫的編碼。 OpenPDF傾向於產生冗長且複雜的程式碼,並且可用的文件非常少。
OpenPDF的HTML轉PDF轉換功能不適合用於大型HTML文件,並且不支援URL到PDF的轉換。
常見問題
如何在 Java 中將 HTML 轉換為 PDF?
您可以使用 IronPDF 的 RenderHtmlAsPdf 方法將 HTML 字串轉換為 PDF。此外,您也可以使用 RenderHtmlFileAsPdf 將 HTML 文件轉換成 PDF。
IronPDF 和 OpenPDF 有哪些主要區別?
IronPDF 提供全面的 PDF 操作工具,包括 HTML 到 PDF 轉換、PDF 圖像處理、編輯和內容提取,並支持多執行緒及非同步操作。OpenPDF 作為開源程式庫,支持 PDF 建立、基本 HTML 解析和文字提取,但不支持 URL 轉換為 PDF。
有什麼樣的授權選擇可以用於將 HTML 轉換為 PDF 在 Java 中?
IronPDF 提供多種適合不同需求的授權選項,包括單一專案授權、SaaS 和 OEM 再分發,以及適用於跨國公司的授權。
如何安裝支持 HTML 到 PDF 轉換的 Java 程式庫?
要安裝 IronPDF for Java,您需要在您的 IDE 中建立一個新的 Maven 專案,然後將必要的 Maven 相依項新增到 pom.xml 文件中並安裝它們。
我可以使用 Java PDF 程式庫將 URL 轉換為 PDF 嗎?
是的,IronPDF 允許您直接將 URL 轉換為 PDF,此外也可處理 HTML 文件及字串,提供靈活的 PDF 建立選擇。
IronPDF 提供哪些 PDF 操作功能?
IronPDF 提供 HTML 到 PDF 轉換、PDF 圖像及編輯、內容提取,以及對多執行緒和非同步操作的支持,這使其成為一個穩健的 PDF 管理選擇。
OpenPDF 適合商業使用嗎?
儘管 OpenPDF 可免費使用且為開源,但由於功能有限,可能更適合學術或基本用途。IronPDF 提供廣泛的文件、進階功能和商業授權選項,更適合商業用途。
OpenPDF 支持 URL 到 PDF 的轉換嗎?
不,OpenPDF 不支持 URL 到 PDF 的轉換。它僅提供基本的 HTML 解析到 PDF。
IronPDF 能在 Java 中處理圖像到 PDF 的轉換嗎?
是的,IronPDF 可以將各種圖像格式轉換為單一 PDF 文件,提供對不同內容型別處理的靈活性。
什麼使得 IronPDF 成為 Java 應用中全面 PDF 功能的更佳選擇?
IronPDF 提供穩健的功能、易於整合、豐富的文件、進階功能和更佳的支援,使其成為開發者尋求在 Java 應用中全面 PDF 功能時的首選。





