跳至頁尾內容
使用 IRONPDF FOR JAVA

Java 庫 PDF 生成(完整代碼示例)

本文將探討IronPDF庫,這是一個用於在 Java 中建立 PDF 的優秀工具。

IronPDF:Java PDF 函式庫

IronPDF是一個流行的Java PDF 庫,它允許開發人員輕鬆建立 PDF 文件、PDF 表單、對 PDF 文件進行數位簽名等等。 使用IronPDF,您可以將現有的 PDF 文件用作模板來產生新的 PDF 文件,將 PDF 資料儲存在資料庫中以供將來使用,將 PDF 轉換為其他格式(如 HTML),甚至將多個 PDF 合併為一個

IronPDF允許用戶在 PDF 文件中添加文字註釋,從而個性化他們創建的文件。 此外,使用IronPDF,您可以在 PDF 檔案中新增安全性設置,例如密碼或浮水印。 它有助於將 PDF 功能整合到 Java 程式中。 IronPDF是一款功能極為強大且用途廣泛的工具,可快速且安全地產生 PDF 檔案。 讓我們來看看如何使用IronPDF建立 PDF 檔案。

使用IronPDF產生 PDF 文件

IronPDF是創建 PDF 文件非常寶貴的工具。 它具備將文件、網頁和圖像快速轉換為穩定、安全的 PDF 文件所需的所有功能,方便用戶輕鬆共享。 讓我們在這個示範程式中安裝IronPDF 。

安裝IronPDF Java PDF 庫

若要在Maven專案中安裝IronPDF Java,您可以將下列相依性新增至專案的 pom.xml 檔案:

<dependencies>

    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>YOUR-VERSION-HERE</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>YOUR-VERSION-HERE</version>
    </dependency>
</dependencies>
<dependencies>

    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>YOUR-VERSION-HERE</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>YOUR-VERSION-HERE</version>
    </dependency>
</dependencies>
XML

這將會新增IronPDF 適用於 Java 函式庫及其使用的 SLF4J 日誌記錄器。 建議使用最新版本的IronPDF 適用於 Java。 新增依賴項後,您可以執行 mvn install 將相依性安裝到本機儲存庫中,您的專案即可使用IronPDF 適用於 Java。

用於建立 PDF 文件的 Java 程式碼

這段程式碼是用 Java 編寫的,並使用IronPDF庫將HTML 轉換為 PDF 文件

// 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 to store log files generated by IronPDF
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Define the HTML content to convert into a PDF
        String html = "<!DOCTYPE html>\r\n"
                + "<html>\r\n"
                + "  <head>\r\n"
                + "    <link href='https://fonts.googleapis.com/css2?family=Popin&display=swap' rel='stylesheet'>\r\n"
                + "    <style>\r\n"
                + "      /* Add CSS styles for the invoice here */\r\n"
                + "      body {\r\n"
                + "        font-family: 'Popin', cursive;\r\n"
                + "      }\r\n"
                + "      .invoice {\r\n"
                + "        width: 80%;\r\n"
                + "        margin: 0 auto;\r\n"
                + "        border: 1px solid #ccc;\r\n"
                + "        padding: 20px;\r\n"
                + "        background-color: #f5f5f5;\r\n"
                + "        color: #333;\r\n"
                + "      }\r\n"
                + "      .invoice h1 {\r\n"
                + "        text-align: center;\r\n"
                + "      }\r\n"
                + "      .invoice .invoice-info {\r\n"
                + "        display: flex;\r\n"
                + "        justify-content: space-between;\r\n"
                + "        margin-bottom: 20px;\r\n"
                + "      }\r\n"
                + "      .invoice .invoice-info div {\r\n"
                + "        width: 45%;\r\n"
                + "      }\r\n"
                + "      .invoice table {\r\n"
                + "        width: 100%;\r\n"
                + "        border-collapse: collapse;\r\n"
                + "      }\r\n"
                + "      .invoice table th, .invoice table td {\r\n"
                + "        border: 1px solid #ccc;\r\n"
                + "        padding: 10px;\r\n"
                + "      }\r\n"
                + "      .invoice table th {\r\n"
                + "        text-align: left;\r\n"
                + "        background-color: #f5f5f5;\r\n"
                + "      }\r\n"
                + "      .invoice table td {\r\n"
                + "        text-align: right;\r\n"
                + "      }\r\n"
                + "      .invoice table td.total {\r\n"
                + "        font-weight: bold;\r\n"
                + "      }\r\n"
                + "    </style>\r\n"
                + "  </head>\r\n"
                + "  <body>\r\n"
                + "    <div class=\"invoice\">\r\n"
                + "      <h1>Invoice</h1>\r\n"
                + "      <div class=\"invoice-info\">\r\n"
                + "        <div>\r\n"
                + "          <p><strong>From:</strong></p>\r\n"
                + "          <p>Your Company Name</p>\r\n"
                + "          <p>123 Main St</p>\r\n"
                + "          <p>City, State ZIP</p>\r\n"
                + "        </div>\r\n"
                + "        <div>\r\n"
                + "          <p><strong>To:</strong></p>\r\n"
                + "          <p>Customer Name</p>\r\n"
                + "          <p>456 Park Ave</p>\r\n"
                + "          <p>City, State ZIP</p>\r\n"
                + "        </div>\r\n"
                + "      </div>\r\n"
                + "      <table>\r\n"
                + "        <thead>\r\n"
                + "          <tr>\r\n"
                + "            <th>Product</th>\r\n"
                + "            <th>Quantity</th>\r\n"
                + "            <th>Price</th>\r\n"
                + "            <th>Total</th>\r\n"
                + "          </tr>\r\n"
                + "        </thead>\r\n"
                + "        <tbody>\r\n"
                + "          <tr>\r\n"
                + "            <td>Product 1</td>\r\n"
                + "            <td>1</td>\r\n"
                + "            <td>$10.00</td>\r\n"
                + "            <td>$10.00</td>\r\n"
                + "          </tr>\r\n"
                + "          <tr>\r\n"
                + "            <td>Product 2</td>\r\n"
                + "            <td>2</td>\r\n"
                + "            <td>$5.00</td>\r\n"
                + "            <td>$10.00</td>\r\n"
                + "          </tr>\r\n"
                + "          <tr>\r\n"
                + "            <td colspan=\"3\" class=\"total\">Total:</td>\r\n"
                + "            <td class=\"total\">$20.00</td>\r\n"
                + "          </tr>\r\n"
                + "        </tbody>\r\n"
                + "      </table>\r\n"
                + "    </div>\r\n"
                + "  </body>\r\n"
                + "</html>";

        // Convert HTML to PDF document
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(html);

        // Save the PDF document to a specified path
        myPdf.saveAs(Paths.get("C://HTMLtoPDF.pdf"));
    }
}
// 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 to store log files generated by IronPDF
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Define the HTML content to convert into a PDF
        String html = "<!DOCTYPE html>\r\n"
                + "<html>\r\n"
                + "  <head>\r\n"
                + "    <link href='https://fonts.googleapis.com/css2?family=Popin&display=swap' rel='stylesheet'>\r\n"
                + "    <style>\r\n"
                + "      /* Add CSS styles for the invoice here */\r\n"
                + "      body {\r\n"
                + "        font-family: 'Popin', cursive;\r\n"
                + "      }\r\n"
                + "      .invoice {\r\n"
                + "        width: 80%;\r\n"
                + "        margin: 0 auto;\r\n"
                + "        border: 1px solid #ccc;\r\n"
                + "        padding: 20px;\r\n"
                + "        background-color: #f5f5f5;\r\n"
                + "        color: #333;\r\n"
                + "      }\r\n"
                + "      .invoice h1 {\r\n"
                + "        text-align: center;\r\n"
                + "      }\r\n"
                + "      .invoice .invoice-info {\r\n"
                + "        display: flex;\r\n"
                + "        justify-content: space-between;\r\n"
                + "        margin-bottom: 20px;\r\n"
                + "      }\r\n"
                + "      .invoice .invoice-info div {\r\n"
                + "        width: 45%;\r\n"
                + "      }\r\n"
                + "      .invoice table {\r\n"
                + "        width: 100%;\r\n"
                + "        border-collapse: collapse;\r\n"
                + "      }\r\n"
                + "      .invoice table th, .invoice table td {\r\n"
                + "        border: 1px solid #ccc;\r\n"
                + "        padding: 10px;\r\n"
                + "      }\r\n"
                + "      .invoice table th {\r\n"
                + "        text-align: left;\r\n"
                + "        background-color: #f5f5f5;\r\n"
                + "      }\r\n"
                + "      .invoice table td {\r\n"
                + "        text-align: right;\r\n"
                + "      }\r\n"
                + "      .invoice table td.total {\r\n"
                + "        font-weight: bold;\r\n"
                + "      }\r\n"
                + "    </style>\r\n"
                + "  </head>\r\n"
                + "  <body>\r\n"
                + "    <div class=\"invoice\">\r\n"
                + "      <h1>Invoice</h1>\r\n"
                + "      <div class=\"invoice-info\">\r\n"
                + "        <div>\r\n"
                + "          <p><strong>From:</strong></p>\r\n"
                + "          <p>Your Company Name</p>\r\n"
                + "          <p>123 Main St</p>\r\n"
                + "          <p>City, State ZIP</p>\r\n"
                + "        </div>\r\n"
                + "        <div>\r\n"
                + "          <p><strong>To:</strong></p>\r\n"
                + "          <p>Customer Name</p>\r\n"
                + "          <p>456 Park Ave</p>\r\n"
                + "          <p>City, State ZIP</p>\r\n"
                + "        </div>\r\n"
                + "      </div>\r\n"
                + "      <table>\r\n"
                + "        <thead>\r\n"
                + "          <tr>\r\n"
                + "            <th>Product</th>\r\n"
                + "            <th>Quantity</th>\r\n"
                + "            <th>Price</th>\r\n"
                + "            <th>Total</th>\r\n"
                + "          </tr>\r\n"
                + "        </thead>\r\n"
                + "        <tbody>\r\n"
                + "          <tr>\r\n"
                + "            <td>Product 1</td>\r\n"
                + "            <td>1</td>\r\n"
                + "            <td>$10.00</td>\r\n"
                + "            <td>$10.00</td>\r\n"
                + "          </tr>\r\n"
                + "          <tr>\r\n"
                + "            <td>Product 2</td>\r\n"
                + "            <td>2</td>\r\n"
                + "            <td>$5.00</td>\r\n"
                + "            <td>$10.00</td>\r\n"
                + "          </tr>\r\n"
                + "          <tr>\r\n"
                + "            <td colspan=\"3\" class=\"total\">Total:</td>\r\n"
                + "            <td class=\"total\">$20.00</td>\r\n"
                + "          </tr>\r\n"
                + "        </tbody>\r\n"
                + "      </table>\r\n"
                + "    </div>\r\n"
                + "  </body>\r\n"
                + "</html>";

        // Convert HTML to PDF document
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(html);

        // Save the PDF document to a specified path
        myPdf.saveAs(Paths.get("C://HTMLtoPDF.pdf"));
    }
}
JAVA
  • 第一步是使用 setLicenseKey 方法應用許可證金鑰。 密鑰以字串參數的形式傳遞; 在這種情況下," YOUR-LICENSE-KEY "應替換為實際的許可證密鑰。
  • 下一步是使用 setLogPath 方法設定日誌路徑。 IronPDF引擎的日誌檔案將會保存在這裡。 在這種情況下,它被設定為" C:/tmp/IronPdfEngine.log "。
  • 定義了主方法,並透過呼叫 @@--CODE-416--@@ 方法建立 @@--CODE-415--@@ 對象,並將 HTML 字串作為參數傳遞。 這將把 HTML 轉換為 PDF 並將其儲存在 myPdf 物件中。
  • 最後一步是使用 @@--CODE-418--@@ 方法將 @@--CODE-419--@@ 物件儲存到檔案中。 文件位置以 Paths 物件的形式作為參數傳遞,在本例中為"HTMLtoPDF.pdf"。

在這裡你可以看到上述程式的輸出結果,其中使用IronPDF Java PDF 庫建立了一個 PDF 檔案。

Java庫PDF產生(完整程式碼範例),圖1:從HTML字串輸出PDF檔案 從 HTML 字串輸出 PDF 文件

從 URL 建立 PDF 文件

IronPDF可以從各種來源(包括本地網路和外部伺服器)將網頁渲染成 PDF

import com.ironsoftware.ironpdf.*;

import java.io.IOException;
import java.nio.file.Paths;

public class UrlToPdfExample {
    public static void main(String[] args) throws IOException {
        // Apply your license key
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Set a log path to store log files generated by IronPDF
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Convert a webpage to a PDF by specifying the URL
        PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");

        // Save the PdfDocument to a file
        myPdf.saveAs(Paths.get("url.pdf"));
    }
}
import com.ironsoftware.ironpdf.*;

import java.io.IOException;
import java.nio.file.Paths;

public class UrlToPdfExample {
    public static void main(String[] args) throws IOException {
        // Apply your license key
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Set a log path to store log files generated by IronPDF
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Convert a webpage to a PDF by specifying the URL
        PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");

        // Save the PdfDocument to a file
        myPdf.saveAs(Paths.get("url.pdf"));
    }
}
JAVA
  • PdfDocument.renderUrlAsPdf 方法專門為此目的而設計,它接受一個包含要轉換的網頁 URL 的字串。 此方法檢索網頁的 HTML 內容並將其轉換為 PDF 文件。 IronPDF保留了所有 Web 元件的外觀,同時讓互動功能(連結、表單欄位等)能夠正常運作。

結果如下:

Java庫PDF產生(完整程式碼範例),圖2:從URL輸出的PDF檔案 從 URL 輸出的 PDF 文件

概括

總之, IronPDF是一個很有價值的 Java 程式庫,它提供了許多用於建立和操作 PDF 檔案的功能。 無論您需要對 PDF 文件進行數位簽名填寫 PDF 表單或執行其他任務, IronPDF都能讓您輕鬆完成,而且只需極少的編碼。

IronPDF 提供免費試用,定價方案靈活,起價為 $999,對於希望在專案中加入 PDF 功能的開發人員來說, IronPDF是一個經濟高效的解決方案。

常見問題

如何在Java中建立PDF文件?

使用IronPDF,您可以利用其全面的 API 在 Java 中建立 PDF 文檔,從頭開始產生新的 PDF,或將現有文檔轉換為 PDF 格式。

如何在Java中將HTML轉換為PDF?

要在 Java 中將 HTML 轉換為 PDF, IronPDF提供了renderHtmlAsPdf方法,讓您可以輸入 HTML 字串並接收 PDF 文件作為輸出。

如何在Java應用程式中將網頁URL轉換為PDF?

IronPDF允許使用renderUrlAsPdf方法將網頁 URL 轉換為 PDF。此方法從 URL 中檢索 HTML 內容並將其轉換為 PDF 文件。

我可以使用Java庫對PDF文件進行數位簽章嗎?

是的, IronPDF提供 PDF 文件的數位簽章的功能,確保文件的真實性和完整性。

如何使用Java為PDF新增安全功能?

IronPDF提供密碼保護和浮水印等安全功能,可套用於 PDF 檔案以增強其安全性。

在 Maven 專案中安裝 PDF 庫需要哪些步驟?

要在 Maven 專案中安裝IronPDF ,您需要將IronPDF依賴項和 SLF4J 日誌記錄相依性新增至pom.xml檔案中,然後執行mvn install指令。

如何使用Java修改現有的PDF檔案?

IronPDF可讓您透過提供編輯文字、合併文件、新增註釋和套用數位簽章等方法來操作現有的 PDF 檔案。

購買前是否可以測試 IronPDF 的各項功能?

是的, IronPDF提供免費試用版,讓開發人員在做出購買決定之前測試其功能。

在Java中使用PDF函式庫有哪些好處?

在 Java 中使用IronPDF等 PDF 函式庫可以簡化建立、編輯和轉換 PDF 的過程,減少大量編碼的需求,提高效率。

如何使用Java將多個PDF檔案合併成一個檔案?

IronPDF包含將多個 PDF 文件合併到單一文件中的功能,可以輕鬆地將多個 PDF 文件合併到一個文件中。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我