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

如何在 PDF 文檔中使用 Java 創建表格(教程)

這篇文章將演示如何使用 Java 在 PDF 文件中創建表格。

使用 Java 程序在 PDF 文檔中創建表格需要第三方庫。 有多個可用的庫可以用來在 PDF 中創建表格,使用 Java 程序。 然而,它們可能昂貴、難以使用,或者存在性能問題。 找到易於使用、開發免費且性能有效的庫可能很棘手。

IronPDF 是一個非常有用的庫,用於操作 PDF 文件。 您可以通過點擊IronPDF 官方 Java 頁面找到更多關於 IronPDF 的信息。

本文涵蓋以下步驟:

  1. 創建一個新項目
  2. 安裝 IronPDF 庫
  3. 創建一個新的 PDF 文件
  4. 為其創建一個表格
  5. 向 PDF 文件添加動態值

創建一個新的 Java 項目

打開您喜歡的 IDE。在本文中推薦使用 IntelliJ,因此使用其他 IDE 創建新項目的步驟可能有所不同。

打開 IntelliJ IDE,從頂部菜單欄中點擊文件 > 新建項目。 命名您的項目,選擇位置、語言、構建系統和 JDK,如下所示。

如何在 PDF 文檔中使用 Java 創建表格 (教程), 圖 1:IntelliJ IDE 新建項目窗口 IntelliJ IDE 新建項目窗口

點擊創建項目按鈕,將創建一個新項目。

安裝 IronPDF 庫

現在,在新創建的項目中安裝 IronPDF 庫。 繼續執行以下步驟。

打開pom.xml文件,添加使用 IronPDF 所需的依賴項和倉庫。 本文未提供pom.xml的具體內容,但請確保使用 Maven 的依賴管理正確包含 IronPDF 庫。

<!-- Add IronPDF dependencies here -->
<dependencies>
    <!-- Example dependency for IronPDF, actual group and artifact id to be retrieved from official documentation -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>VERSION_NUMBER</version>
    </dependency>
</dependencies>
<!-- Add IronPDF dependencies here -->
<dependencies>
    <!-- Example dependency for IronPDF, actual group and artifact id to be retrieved from official documentation -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>VERSION_NUMBER</version>
    </dependency>
</dependencies>
XML

在終端中輸入以下命令,然後按 Enter 鍵以安裝指定的 Maven 依賴項。

mvn install
mvn install
SHELL

這將在此項目中安裝 IronPDF。

添加以下導入語句以使用 IronPDF 類。

import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.*;
JAVA

首先,我們將學習如何用 Java 創建簡單的 PDF 文檔。

創建 PDF 文件

以下示例代碼將創建一個新的 PDF 文檔。

public static void main(String[] args) throws IOException {
    // Create a PDF document from an HTML string
    PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("This is a sample PDF file");

    try {
        // Save the created PDF document to a file
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
public static void main(String[] args) throws IOException {
    // Create a PDF document from an HTML string
    PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("This is a sample PDF file");

    try {
        // Save the created PDF document to a file
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
JAVA

[renderHtmlAsPdf](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#renderHtmlAsPdf(java.lang.String)方法接收一個字符串作為參數,並將該字符串轉換為 PDF 文檔的實例。

[saveAs](/java/object-reference/api/com/ironsoftware/ironpdf/PdfDocument.html#saveAs(java.lang.String)函數以文件路徑為參數,並將新創建的 PDF 文檔保存到參數中指定的文件路徑。

PDF 是從上述代碼創建的,顯示在以下圖像中。

如何在 PDF 文檔中使用 Java 創建表格 (教程), 圖 2:新的 PDF 文件 新的 PDF 文件

為 PDF 文件創建表格

以下代碼將在 PDF 中創建一個表格。

public static void main(String[] args) throws IOException {
    // HTML content for creating a table
    String tableContent = "<table>\n" +
        "  <tr>\n" +
        "    <th>Company</th>\n" +
        "    <th>Contact</th>\n" +
        "    <th>Country</th>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Alfreds Futterkiste</td>\n" +
        "    <td>Maria Anders</td>\n" +
        "    <td>Germany</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Centro comercial Moctezuma</td>\n" +
        "    <td>Francisco Chang</td>\n" +
        "    <td>Mexico</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Ernst Handel</td>\n" +
        "    <td>Roland Mendel</td>\n" +
        "    <td>Austria</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Island Trading</td>\n" +
        "    <td>Helen Bennett</td>\n" +
        "    <td>UK</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Laughing Bacchus Winecellars</td>\n" +
        "    <td>Yoshi Tannamuri</td>\n" +
        "    <td>Canada</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Magazzini Alimentari Riuniti</td>\n" +
        "    <td>Giovanni Rovelli</td>\n" +
        "    <td>Italy</td>\n" +
        "  </tr>\n" +
        "</table>";

    // Create a PDF document with table content
    PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1>Example of adding table in a PDF</h1>" + tableContent);

    try {
        // Save the created PDF document to a file
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
public static void main(String[] args) throws IOException {
    // HTML content for creating a table
    String tableContent = "<table>\n" +
        "  <tr>\n" +
        "    <th>Company</th>\n" +
        "    <th>Contact</th>\n" +
        "    <th>Country</th>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Alfreds Futterkiste</td>\n" +
        "    <td>Maria Anders</td>\n" +
        "    <td>Germany</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Centro comercial Moctezuma</td>\n" +
        "    <td>Francisco Chang</td>\n" +
        "    <td>Mexico</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Ernst Handel</td>\n" +
        "    <td>Roland Mendel</td>\n" +
        "    <td>Austria</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Island Trading</td>\n" +
        "    <td>Helen Bennett</td>\n" +
        "    <td>UK</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Laughing Bacchus Winecellars</td>\n" +
        "    <td>Yoshi Tannamuri</td>\n" +
        "    <td>Canada</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Magazzini Alimentari Riuniti</td>\n" +
        "    <td>Giovanni Rovelli</td>\n" +
        "    <td>Italy</td>\n" +
        "  </tr>\n" +
        "</table>";

    // Create a PDF document with table content
    PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1>Example of adding table in a PDF</h1>" + tableContent);

    try {
        // Save the created PDF document to a file
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
JAVA

上述代碼使用簡單的 HTML 標籤來在 PDF 中創建表格,使用 Java。 因此,要創建表格,您必須具備使用 HTML 標籤的基本知識。 幾乎每個 Java 程序員都具備 HTML 知識,因此使用 HTML 標籤創建新表格和表格單元格非常簡單。

此程序生成的 PDF 文件顯示在以下圖像中:

如何在 PDF 文檔中使用 Java 創建表格 (教程), 圖 3:從 HTML 生成的表格的 PDF 文件 從 HTML 生成的表格的 PDF 文件

這是一個沒有樣式的簡單表格。

現在讓我們為這個表格添加一些樣式,例如設置表格寬度、邊距、佈局、字體、背景色等。

為表格添加樣式

以下示例代碼將格式化我們的表格並為我們的單元格添加樣式。

public static void main(String[] args) throws IOException {
    // HTML and CSS content for styling the table
    String htmlStyle = "<!DOCTYPE html>\n" +
        "<html>\n" +
            "<head>\n" +
                "<style>\n" +
                    "table {\n" +
                    "  font-family: arial, sans-serif;\n" +
                    "  border-collapse: collapse;\n" +
                    "  width: 100%;\n" +
                    "}\n" +
                    "\n" +
                    "td, th {\n" +
                    "  border: 1px solid #dddddd;\n" +
                    "  text-align: left;\n" +
                    "  padding: 8px;\n" +
                    "}\n" +
                    "\n" +
                    "tr:nth-child(even) {\n" +
                    "  background-color: #dddddd;\n" +
                    "}\n" +
                "</style>\n" +
            "</head>\n" +
        "<body>";

    String tableContent = "<table>\n" +
        "  <tr>\n" +
        "    <th>Company</th>\n" +
        "    <th>Contact</th>\n" +
        "    <th>Country</th>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Alfreds Futterkiste</td>\n" +
        "    <td>Maria Anders</td>\n" +
        "    <td>Germany</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Centro comercial Moctezuma</td>\n" +
        "    <td>Francisco Chang</td>\n" +
        "    <td>Mexico</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Ernst Handel</td>\n" +
        "    <td>Roland Mendel</td>\n" +
        "    <td>Austria</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Island Trading</td>\n" +
        "    <td>Helen Bennett</td>\n" +
        "    <td>UK</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Laughing Bacchus Winecellars</td>\n" +
        "    <td>Yoshi Tannamuri</td>\n" +
        "    <td>Canada</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Magazzini Alimentari Riuniti</td>\n" +
        "    <td>Giovanni Rovelli</td>\n" +
        "    <td>Italy</td>\n" +
        "  </tr>\n" +
        "</table>\n</body>\n" +
        "</html>";

    // Create a PDF document with styled table content
    PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(htmlStyle + "Sample PDF" + tableContent);

    try {
        // Save the created PDF document to a file
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
public static void main(String[] args) throws IOException {
    // HTML and CSS content for styling the table
    String htmlStyle = "<!DOCTYPE html>\n" +
        "<html>\n" +
            "<head>\n" +
                "<style>\n" +
                    "table {\n" +
                    "  font-family: arial, sans-serif;\n" +
                    "  border-collapse: collapse;\n" +
                    "  width: 100%;\n" +
                    "}\n" +
                    "\n" +
                    "td, th {\n" +
                    "  border: 1px solid #dddddd;\n" +
                    "  text-align: left;\n" +
                    "  padding: 8px;\n" +
                    "}\n" +
                    "\n" +
                    "tr:nth-child(even) {\n" +
                    "  background-color: #dddddd;\n" +
                    "}\n" +
                "</style>\n" +
            "</head>\n" +
        "<body>";

    String tableContent = "<table>\n" +
        "  <tr>\n" +
        "    <th>Company</th>\n" +
        "    <th>Contact</th>\n" +
        "    <th>Country</th>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Alfreds Futterkiste</td>\n" +
        "    <td>Maria Anders</td>\n" +
        "    <td>Germany</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Centro comercial Moctezuma</td>\n" +
        "    <td>Francisco Chang</td>\n" +
        "    <td>Mexico</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Ernst Handel</td>\n" +
        "    <td>Roland Mendel</td>\n" +
        "    <td>Austria</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Island Trading</td>\n" +
        "    <td>Helen Bennett</td>\n" +
        "    <td>UK</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Laughing Bacchus Winecellars</td>\n" +
        "    <td>Yoshi Tannamuri</td>\n" +
        "    <td>Canada</td>\n" +
        "  </tr>\n" +
        "  <tr>\n" +
        "    <td>Magazzini Alimentari Riuniti</td>\n" +
        "    <td>Giovanni Rovelli</td>\n" +
        "    <td>Italy</td>\n" +
        "  </tr>\n" +
        "</table>\n</body>\n" +
        "</html>";

    // Create a PDF document with styled table content
    PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(htmlStyle + "Sample PDF" + tableContent);

    try {
        // Save the created PDF document to a file
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
JAVA

添加的 CSS 用於在 PDF 中設計表格。 因此,使用 CSS 進行所需的樣式設計非常高效。

以下是該程序生成的 PDF。

如何在 PDF 文檔中使用 Java 創建表格 (教程), 圖 4:從 HTML 生成的表格並用 CSS 設計的 PDF 文件 從 HTML 生成並用 CSS 設計的表格的 PDF 文件

上述代碼看起來非常亂。 但可以通過將所有 HTML 內容移到一個 HTML 文件中 然後從該文件生成 PDF 來清理。

使用 Java 在 PDF 中創建 HTML 文件中的表格

創建一個新的 HTML 文件,並將所有 HTML 內容添加到該文件,如下所示:

如何在 PDF 文檔中使用 Java 創建表格 (教程), 圖 5:HTML 移入自己的 HTML 文件 HTML 移入自己的 HTML 文件

將以下代碼添加到 Java 程序中。

// Create a PDF document from an HTML file
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("Create_Table.html");

try {
    // Save the created PDF document to a file
    myPdf.saveAs(Paths.get("html_saved.pdf"));
} catch (IOException e) {
    throw new RuntimeException(e);
}
// Create a PDF document from an HTML file
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("Create_Table.html");

try {
    // Save the created PDF document to a file
    myPdf.saveAs(Paths.get("html_saved.pdf"));
} catch (IOException e) {
    throw new RuntimeException(e);
}
JAVA

我們可以看到,使用 HTML 文檔生成 PDF 是多麼簡單。 如果您需要在 Java 中創建 PDF 文件,最簡單的方法是創建一個帶有內容和樣式的單獨 HTML 文檔,然後僅需用一行代碼將 HTML 文檔轉換為 PDF。

如何在 PDF 文檔中使用 Java 創建表格 (教程), 圖 6:最終帶有樣式的 PDF 文件 最終帶有樣式的 PDF 文件

總結

本教程演示了如何使用 Java 在 PDF 文件中創建樣式化的表格,並學習將 HTML 文件轉換為 PDF 文件。 IronPDF for Java also offers functionality to add images to the PDF file, split PDF files, add headers and footers, apply digital signatures, and much more. 探索 IronPDF 文檔以了解更多關於這些功能和其他功能。

Remove the IronPDF watermark from your generated PDF documents by purchasing a license key or by registering for a free trial.

常見問題解答

如何開始一個新的 Java 項目以創建 PDF?

要開始一個新的 Java 項目以創建 PDF 您可以使用像 IntelliJ 這樣的 IDE。首先設置項目,然後通過 Maven 安裝 IronPDF 庫以處理 PDF 的創建和操作。

用 Java 將 HTML 內容轉換為 PDF 涉及哪些步驟?

要在 Java 中將 HTML 內容轉換為 PDF,您可以使用 IronPDF 的 PdfDocument.renderHtmlAsPdf 方法。這包括編寫 HTML 內容,並可選擇使用 CSS 進行樣式設置,然後將其渲染為 PDF。

我可以使用 CSS 來設計 Java 生成的 PDF 中的表格嗎?

是的,您可以使用 CSS 來設計 PDF 中的表格,方法是在渲染為 PDF 之前將 CSS 包含在 HTML 內容中。這允許您定義字體、邊框、顏色等。

如何使用 Java 從外部 HTML 文件創建 PDF?

要使用 Java 從外部 HTML 文件創建 PDF,您可以使用 IronPDF 的 PdfDocument.renderHtmlFileAsPdf 方法,這會將整個 HTML 文件轉換為 PDF 文件。

使用第三方庫創建 PDF 在 Java 中有什麼好處?

使用像 IronPDF 這樣的第三方庫簡化了 Java 中的 PDF 創建過程,提供了例如 HTML 到 PDF 轉換、表格樣式設置以及附加功能如圖像和數字簽名支持。

如何使用 Java 庫在 PDF 文件中包含圖像?

您可以通過將圖像嵌入您要渲染為 PDF 的 HTML 內容來在 PDF 文件中包含圖像。此方法允許無縫結合視覺元素。

用 Java 有哪些選項可以分割 PDF?

IronPDF 提供分割 PDF 的選項,允許您將單個 PDF 分割成多個文件。此功能對於管理大型文件或提取特定部分特別有用。

如何使用 Java 將數字簽名應用於 PDF?

您可以使用 IronPDF 的內置功能將數字簽名應用於 PDF,這支持添加數字簽名以增強文件安全性和真實性。

如果我的 PDF 文件上有水印應該怎麼辦?

如果您的使用 IronPDF 創建的 PDF 文件上有水印,您可以通過購買許可證或註冊免費試用許可證來刪除水印。

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

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

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

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