IronPDF for Java - 在 Java 應用程式中建立、編輯和讀取 PDF
關於 Java 版 IronPDF
IronPDF for Java 是由 Iron Software 開發和維護的程式庫,它可以幫助軟體工程師在 Java 8+、Kotlin 和 Scala 專案中建立、編輯和提取 PDF 內容。
IronPDF for Java是在IronPDF for .NET的成功和流行基礎上建立的。
IronPDF for Java 使用 gRPC 與IronPdfEngine通訊。
IronPDF 在以下方面表現出色
- 支援從以下格式產生 PDF:HTML、URL、JavaScript、CSS 和多種影像格式
- 新增頁首/頁尾、簽名、附件、密碼和安全性設置
- 效能最佳化:完全支援多執行緒和非同步編程 還有更多! 造訪我們的網站,查看所有程式碼範例和超過 50 項功能的完整清單。
使用 IronPDF for Java
將 IronPDF 定義為 Java 依賴項
pom.xml 依賴項
若要將 IronPDF 定義為依賴項,請將以下內容新增至您的pom.xml :
<dependencies>
<!-- Adds IronPDF Java. Use the latest version in the version tag. -->
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>20xx.xx.xxxx</version>
</dependency>
<!-- Adds the slf4j logger which IronPDF Java uses. -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies><dependencies>
<!-- Adds IronPDF Java. Use the latest version in the version tag. -->
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>20xx.xx.xxxx</version>
</dependency>
<!-- Adds the slf4j logger which IronPDF Java uses. -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies>下載 jar 文件
您也可以手動下載 IronPDF JAR 檔案以供獨立使用。
首先建置並運行
首次運行專案時, IronPdfEngine二進位檔案將自動下載。首次呼叫任何 IronPdf 函數時, IronPdfEngine程序將啟動;應用程式關閉或進入空閒狀態時,該進程將停止。
安裝 IronPDF Engine 作為 Maven 依賴項
透過將 IronPdfEngine 新增為 Maven 依賴項,將在載入依賴項期間下載二進位檔案:
- 這種方法可以避免冗長的啟動過程,因為 IronPdfEngine 二進位檔案已經下載完畢。 此外,對於不允許從外部來源下載的部署設定來說,這將是有益的。
如果您正在開發多平台應用程序,只需將以下一個或多個程式碼片段新增至您的pom.xml檔案中:
適用於 Windows x64
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf-engine-windows-x64</artifactId>
<version>20xx.xx.xxxx</version>
</dependency><dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf-engine-windows-x64</artifactId>
<version>20xx.xx.xxxx</version>
</dependency>適用於 Windows x86
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf-engine-windows-x86</artifactId>
<version>20xx.xx.xxxx</version>
</dependency><dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf-engine-windows-x86</artifactId>
<version>20xx.xx.xxxx</version>
</dependency>適用於 Linux x64
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf-engine-linux-x64</artifactId>
<version>20xx.xx.xxxx</version>
</dependency><dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf-engine-linux-x64</artifactId>
<version>20xx.xx.xxxx</version>
</dependency>適用於 macOS x64 (Intel)
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf-engine-macos-x64</artifactId>
<version>20xx.xx.xxxx</version>
</dependency><dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf-engine-macos-x64</artifactId>
<version>20xx.xx.xxxx</version>
</dependency>適用於 macOS Arm(Apple Silicon)
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf-engine-macos-arm64</artifactId>
<version>20xx.xx.xxxx</version>
</dependency><dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf-engine-macos-arm64</artifactId>
<version>20xx.xx.xxxx</version>
</dependency>開始編寫 Java 程式碼
定義好依賴關係後,就可以開始在 Java 程式碼的頂端新增import com.ironsoftware.ironpdf.*語句了。 以下是一個簡單的 HTML 轉 PDF 範例,供您快速入門:
// Import statement for IronPDF Java
import com.ironsoftware.ironpdf.*;
// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the HTML as a PDF. Stored in myPdf as type PdfDocument
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_saved.pdf"));// Import statement for IronPDF Java
import com.ironsoftware.ironpdf.*;
// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the HTML as a PDF. Stored in myPdf as type PdfDocument
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_saved.pdf"));這是另一個示範如何將 URL 轉換為 PDF 的簡單範例:
// Import statement for IronPDF Java
import com.ironsoftware.ironpdf.*;
// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the URL as a PDF. Stored in myPdf as type PdfDocument
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com/java");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("url_saved.pdf"));// Import statement for IronPDF Java
import com.ironsoftware.ironpdf.*;
// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the URL as a PDF. Stored in myPdf as type PdfDocument
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com/java");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("url_saved.pdf"));完整的 Main.java 範例
package org.example;
// Import statement for IronPDF Java
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the HTML as a PDF. Stored in myPdf as type PdfDocument
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}package org.example;
// Import statement for IronPDF Java
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");
// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
// Render the HTML as a PDF. Stored in myPdf as type PdfDocument
PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_saved.pdf"));
}
}更多設定信息
注意:請注意,所有設定、日誌記錄和許可操作必須在呼叫任何 IronPDF 方法之前執行。
應用許可證密鑰
若要套用您的許可證金鑰,請將以下內容新增至您的方法頂部:
com.ironsoftware.ironpdf.License.setLicenseKey("YOUR-LICENSE-KEY");com.ironsoftware.ironpdf.License.setLicenseKey("YOUR-LICENSE-KEY");記錄
IronPDF Java 使用slf4j 日誌記錄器進行日誌記錄。 若要啟用日誌記錄,請使用:
com.ironsoftware.ironpdf.Settings.setDebug(true);com.ironsoftware.ironpdf.Settings.setDebug(true);若要指定IronPdfEngine日誌路徑,請新增:
com.ironsoftware.ironpdf.Settings.setLogPath(Paths.get("C:/tmp/myIronPdfEngineLog.log"));com.ironsoftware.ironpdf.Settings.setLogPath(Paths.get("C:/tmp/myIronPdfEngineLog.log"));提供許可和支持
購買 IronPDF 許可證,即可在實際項目中使用。 我們也為試用用戶提供 30 天試用許可證。
如需查看完整的程式碼範例、教學課程、許可資訊和文檔,請造訪: IronPDF for Java resources 。
如需更多協助或有任何疑問,請聯絡我們的支援團隊。







