跳至頁尾內容
使用 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>
    <!-- Add IronPDF dependency -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>YOUR-VERSION-HERE</version>
    </dependency>
    <!-- Add SLF4J logging dependency -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>YOUR-VERSION-HERE</version>
    </dependency>
</dependencies>
<dependencies>
    <!-- Add IronPDF dependency -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>YOUR-VERSION-HERE</version>
    </dependency>
    <!-- Add SLF4J logging dependency -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>YOUR-VERSION-HERE</version>
    </dependency>
</dependencies>
XML

這將會新增 IronPDF for Java 函式庫及其使用的 SLF4J 日誌記錄器。 建議使用最新版本的 IronPDF for Java。 新增依賴項後,您可以執行mvn install將相依性安裝到本機儲存庫中,您的專案就可以使用 IronPDF for 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 "。
  • 定義了主要方法,並透過呼叫renderHtmlAsPdf 方法建立了PdfDocument對象,並將 HTML 字串作為參數傳遞。 這將把 HTML 轉換為 PDF 並將其儲存在myPdf物件中。 最後一步是使用[saveAs](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#saveAs(java.lang.String)方法將myPdf物件儲存到檔案中。 文件位置以 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 提供免費試用版,定價方案靈活,起價僅為$799 ,對於希望在專案中添加 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擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

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