跳過到頁腳內容
使用 IRONPDF FOR JAVA

PDF 創建者 For Java(分步教程)

本文將演示如何使用用Java編程語言編寫的PDF創建器來創建PDF文件。

使用Java創建PDF文件在IT產業中有很多應用。 開發人員偶爾可能需要在其專案中整合一個PDF創建器。 本文討論的PDF創建器是IronPDF for Java。 它幫助開發人員從頭創建新的PDF文件並將不同的文件格式轉換為PDF文件。

1. IronPDF for Java

IronPDF for Java 是一個用於創建、準備和管理PDF文件的Java程式庫。 它允許開發人員在不需要安裝任何Adobe軟體的情況下讀取、製作和修改PDF文件。

IronPDF還非常擅長從PDF文件創建PDF表單。IronPDF for Java可以創建自定義頁首和頁尾創建簽名,添加附件,並添加密碼加密和安全機制。 IronPDF還支持多線程和異步功能以提高性能。

2. 先決條件

在開始之前,有一些先決條件必須滿足來執行PDF創建。

  1. 系統上應安裝Java,並且其路徑應設置在環境變量中。 如果您尚未安裝Java,請參考此安裝指南
  2. 應該安裝一個好的Java IDE,比如Eclipse或IntelliJ。 要下載Eclipse,請訪問此下載頁面,或者點擊此安裝頁面以安裝IntelliJ。
  3. 在開始進行PDF轉換之前,Maven應該與IDE整合。 有關安裝Maven並將其整合到環境中的教程,請訪問以下JetBrains的整合連結

3. IronPDF for Java安裝

一旦所有先決條件都滿足,對於新的Java開發人員而言,安裝IronPDF for Java非常簡單和容易。

本文將使用JetBrains IntelliJ IDEA來安裝程式庫和執行代碼範例。

首先,打開JetBrains IntelliJ IDEA並創建一個新的Maven專案。

Java的PDF創建器(逐步)教程,圖1:新專案 新專案

將會出現一個新窗口。 輸入專案的名稱,然後點擊完成。

Java的PDF創建器(逐步)教程,圖2:配置新專案 配置新專案

點擊完成後,將在pom.xml文件中開啟一個新專案,然後使用該文件添加一些Maven專案依賴。

Java的PDF創建器(逐步)教程,圖3:pom.xml文件 pom.xml文件

pom.xml文件中添加以下依賴。

<dependencies>
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>1.0.0</version> 
    </dependency>
</dependencies>
<dependencies>
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>1.0.0</version> 
    </dependency>
</dependencies>
XML

一旦您將依賴放置在pom.xml文件中,文件的右上角將顯示一個小圖標。

Java的PDF創建器(逐步)教程,圖4:pom.xml文件,帶有安裝依賴的圖標 pom.xml文件,帶有安裝依賴的圖標

點擊此圖標以安裝IronPDF for Java的Maven依賴項。 這只需幾分鐘,具體取決於您的網絡連接。

4. 創建PDF文件

此部分將討論如何使用IronPDF for Java創建新PDF文件。 IronPDF使創建新PDF文件非常簡單,僅需幾行代碼。

使用IronPDF for Java有許多不同的方式來創建PDF文件

  • [renderHtmlAsPdf](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#renderHtmlAsPdf(java.lang.String)方法可以將HTML字串轉換為PDF文件
  • [renderHtmlFileAsPdf](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#renderHtmlFileAsPdf(java.lang.String)方法可以將HTML文件轉換為PDF文件
  • [renderUrlAsPdf](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#renderUrlAsPdf(java.lang.String)方法可以直接從URL創建PDF文件,並將結果保存到指定文件夾。

4.1. 使用HTML字串創建PDF文件

下面的代碼範例顯示瞭如何使用renderHtmlAsPdf方法將HTML標記字串轉換為PDF。 請注意,使用renderHtmlAsPdf方法需要對HTML工作原理有基本的認識。

import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        License.setLicenseKey("YOUR-LICENSE-KEY"); // Set your IronPDF license key
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Set the path for log files
        // Convert a simple HTML string into a PDF document
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
        // Save the PDF document to the specified path
        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 {
        License.setLicenseKey("YOUR-LICENSE-KEY"); // Set your IronPDF license key
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Set the path for log files
        // Convert a simple HTML string into a PDF document
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1> ~Hello World~ </h1> Made with IronPDF!");
        // Save the PDF document to the specified path
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    }
}
JAVA

上面代碼的輸出如下所示。

Java的PDF創建器(逐步)教程,圖5:輸出文件 輸出文件

4.2. 從HTML文件創建PDF文件

IronPDF程式庫的renderHtmlFileAsPdf方法允許您從HTML源文件生成新的PDF文件。該方法將HTML文檔中的所有內容轉換成PDF,包括圖形、CSS、表單等等。

import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        License.setLicenseKey("YOUR-LICENSE-KEY"); // Set your IronPDF license key
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Set the path for log files
        // Convert an HTML file into a PDF document
        PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("index.html");
        // Save the PDF document to the specified path
        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 {
        License.setLicenseKey("YOUR-LICENSE-KEY"); // Set your IronPDF license key
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Set the path for log files
        // Convert an HTML file into a PDF document
        PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("index.html");
        // Save the PDF document to the specified path
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    }
}
JAVA

上面代碼範例的輸出如下所示。

Java的PDF創建器(逐步)教程,圖6:來自HTML文件的輸出文件 來自HTML文件的輸出文件

4.3. 從URL創建PDF文件

使用IronPDF,您可以快速從網頁的URL精確創建PDF。 它還提供從憑證鎖定網站創建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 {
        License.setLicenseKey("YOUR-LICENSE-KEY"); // Set your IronPDF license key
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Set the path for log files
        // Convert a web page URL into a PDF document
        PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://www.alibaba.com/");
        // Save the PDF document to the specified path
        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 {
        License.setLicenseKey("YOUR-LICENSE-KEY"); // Set your IronPDF license key
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log")); // Set the path for log files
        // Convert a web page URL into a PDF document
        PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://www.alibaba.com/");
        // Save the PDF document to the specified path
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    }
}
JAVA

上面代碼的輸出如下所示。

Java的PDF創建器(逐步)教程,圖7:來自阿里巴巴網站的輸出PDF文件 來自阿里巴巴網站的輸出PDF文件

4.4. 從圖像創建PDF文件

IronPDF for Java也可以從一個或多個圖像創建PDF文件。

import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        Path imageDirectory = Paths.get("assets/images"); // Define the directory to search for images
        List<Path> imageFiles = new ArrayList<>();
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(imageDirectory, "*.{png,jpg}")) {
            for (Path entry: stream) {
                imageFiles.add(entry); // Add each image file to the list
            }
            // Convert images into a composite PDF document
            PdfDocument.fromImage(imageFiles).saveAs(Paths.get("assets/composite.pdf"));
        } catch (IOException exception) {
            // Handle the exception if an error occurs during image-to-PDF conversion
            throw new RuntimeException(String.format("Error converting images to PDF from directory: %s: %s",
                    imageDirectory,
                    exception.getMessage()),
                exception);
        }
    }
}
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        Path imageDirectory = Paths.get("assets/images"); // Define the directory to search for images
        List<Path> imageFiles = new ArrayList<>();
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(imageDirectory, "*.{png,jpg}")) {
            for (Path entry: stream) {
                imageFiles.add(entry); // Add each image file to the list
            }
            // Convert images into a composite PDF document
            PdfDocument.fromImage(imageFiles).saveAs(Paths.get("assets/composite.pdf"));
        } catch (IOException exception) {
            // Handle the exception if an error occurs during image-to-PDF conversion
            throw new RuntimeException(String.format("Error converting images to PDF from directory: %s: %s",
                    imageDirectory,
                    exception.getMessage()),
                exception);
        }
    }
}
JAVA

上面代碼的輸出如下所示。

Java的PDF創建器(逐步)教程,圖8:來自圖像的輸出PDF文件 來自圖像的輸出PDF文件

5. 結論

本文演示了如何從頭開始創建PDF或從已存在的HTML文件或URL創建PDF。 IronPDF允許您從多個格式轉換以生成高質量的PDF,並使操縱和格式化這些PDF文件非常簡單。

IronPDF非常適合需要處理、修改或操縱PDF文件的軟體開發人員和企業。 想瞭解更多關於IronPDF for Java的信息並獲得如何使用Java操作PDF的類似教程,請參考官方文件。 有關使用Java創建PDF文件的教程,請訪問此Java代碼示例

IronPDF for Java可以免費用於開發目的,但商業用途需要授權。 有關授權的更多信息,請訪問以下授權頁面

常見問題解答

如何使用Java建立PDF檔案?

您可以使用IronPDF for Java 庫建立 PDF 文件,該庫允許您從頭開始生成 PDF,或將 HTML、URL 或圖像等各種格式轉換為 PDF 文件。

如何在 IntelliJ IDEA 中安裝 PDF 函式庫?

若要在 IntelliJ IDEA 中安裝IronPDF ,請建立一個 Maven 項目,並將 IronPDF 的 Maven 依賴項新增至pom.xml檔案中。然後,使用 IDE 管理安裝過程。

我可以用Java為PDF添加自訂頁首和頁尾嗎?

是的, IronPDF for Java 允許您使用其 API 方法新增頁首和頁尾來自訂 PDF 文件。

如何在Java中將HTML檔案轉換為PDF文件?

您可以使用IronPDF for Java 庫中的renderHtmlFileAsPdf方法將 HTML 檔案轉換為 PDF 文件。此方法會保留圖形、CSS 和表單。

是否可以使用 Java 對 PDF 檔案進行加密?

是的, IronPDF for Java 支援 PDF 檔案的密碼加密,讓您能夠有效地保護文件安全。

如何在Java中使用影像建立PDF文件?

IronPDF for Java 提供了一個名為fromImage方法,可讓您從一個或多個影像建立 PDF 文件。

IronPDF for Java 是否支援多執行緒?

是的, IronPDF for Java 包含了多執行緒和非同步功能,可以提高處理 PDF 文件時的效能。

如果我在Java中建立PDF失敗,我該怎麼辦?

請確保已正確安裝IronPDF for Java,且 Java 環境配置正確。檢查程式碼中是否有使用IronPDF方法的錯誤,並參考官方文件進行故障排除。

我可以用Java將網頁URL轉換為PDF嗎?

是的, IronPDF for Java 允許您使用renderUrlAsPdf方法將網頁 URL 轉換為 PDF 文件。

哪裡可以找到學習如何使用IronPDF for Java 的資源?

您可以在IronPDF官方文件頁面上找到有關使用IronPDF for Java 的全面教學和文件。

Darrius Serrant
全棧軟件工程師 (WebOps)

Darrius Serrant 擁有邁阿密大學計算機科學學士學位,目前任職於 Iron Software 的全栈 WebOps 市場營銷工程師。從小就迷上編碼,他認為計算既神秘又可接近,是創意和解決問題的完美媒介。

在 Iron Software,Darrius 喜歡創造新事物,並簡化複雜概念以便於理解。作為我們的駐場開發者之一,他也自願教學生,分享他的專業知識給下一代。

對 Darrius 來說,工作令人滿意因為它被重視且有實際影響。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me