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 .NETIronPDF 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 檔案

可選 下載 .jar 文件 手動。

首次構建和運行

當專案首次運行時,IronPdfEngine 的二進制文件將自動下載。當您首次調用任何 IronPdf 函數時,IronPdfEngine 進程將啟動,並在您的應用程式關閉或進入閒置階段時停止。

將 IronPDF 引擎安裝為 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 page 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。。

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