IronPDF for Java - 在Java應用程式中創建、編輯和讀取PDFs

This article was translated from English: Does it need improvement?
Translated
View the article in English

關於 IronPDF for Java

IronPDF for Java 是由 Iron Software 開發和維護的一個庫,協助軟體工程師在 Java 8+、Kotlin 和 Scala 專案中創建、編輯和提取 PDF 內容。

IronPDF for Java 以其成功與受歡迎的基礎上进行构建。IronPDF for .NET.

IronPDF for Java 使用 gRPC 與 IronPdfEngine 進行通信。

IronPDF擅長於

  • 從 HTML、URL、JavaScript、CSS 和許多圖像格式生成 PDF
  • 添加頁眉/頁腳、簽名、附件、密碼和安全性
  • 性能優化:全面的多線程和異步支援
  • 還有更多! 訪問我們的網站查看我們所有的代碼範例和一個我們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>
XML

下載 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>
XML

適用於 Windows x86

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-windows-x86</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
XML

適用於 Linux x64

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-linux-x64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
XML

適用於 macOS x64 (英特爾)

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-macos-x64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
XML

適用於 macOS Arm(Apple Silicon)

<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf-engine-macos-arm64</artifactId>
    <version>20xx.xx.xxxx</version>
</dependency>
XML

開始編寫 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"));
JAVA

這是另一個簡單的範例,示範了 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"));

PdfDocument pdfDocument = PdfDocument.renderUrlAsPdf("https://ironpdf.com/java");
JAVA

完整的 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"));

    }  

}
JAVA

進一步設定資訊

請注意,在調用任何IronPDF方法之前,必須執行所有設置、記錄和授權操作。

套用授權金鑰

要應用您的許可證鑰匙,請在方法的頂部添加以下內容:

com.ironsoftware.ironpdf.License.setLicenseKey("YOUR-LICENSE-KEY");
JAVA

日誌記錄

IronPDF Java 使用slf4j 日誌記錄器為了記錄用途。 要啟用日誌記錄,請使用:

com.ironsoftware.ironpdf.Settings.setDebug(true);
JAVA

若要指定 IronPdfEngine 日誌路徑,請新增:

com.ironsoftware.ironpdf.Settings.setLogPath(Paths.get("C:/tmp/myIronPdfEngineLog.log"));
JAVA

授權與支持可用

購買 IronPDF 的授權!在實際專案中使用。 30天試用許可證也可用試用用戶.

若要查看我們的完整程式碼範例、教學、授權資訊和文件,請造訪:IronPDF for Java 資源.

如需更多支援和查詢,請聯絡我們的支援團隊.