通过 Java 将表格添加到 PDF

使用适用于 Java 库的 Aspose.PDF 以编程方式将表格插入到 PDF 文档

如何使用 Java 庫在 PDF 文件中添加表格

為了新增表格,我們將使用 Aspose.PDF for Java API,這是一個功能豐富、強大且易於使用的 Java 平台轉換 API。您可以直接從 Maven 下載其最新版本,並透過在 pom.xml 檔案中新增以下設定將其安裝到您的 Maven 專案中。

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

通過Java將表格添加到 PDF


您需要 Aspose.PDF for Java 才能在您的環境中測試程式碼。

  1. 使用 Document 實例載入 PDF。
  2. 透過索引存取頁面。
  3. 建立表格物件。
  4. 設定表格設定(例如,設定邊框)。
  5. 填充表格。
  6. 將表格新增至頁面。
  7. 儲存文件。

在 PDF 中新增表格 - Java

Document pdfDocument = new Document(DATA_DIR.resolve("input.pdf").toString());
alizes a new instance of the Table
com.aspose.pdf.Table table = new com.aspose.pdf.Table();
he table border color as LightGray
table.setBorder(new BorderInfo(BorderSide.All, .5f, Color.getLightGray()));
he border for table cells
table.setDefaultCellBorder(new BorderInfo(BorderSide.All, .5f, Color.getLightGray()));
e a loop to add 10 rows
for (int row_count = 1; row_count < 10; row_count++) {
    // add row to table
    Row row = table.getRows().add();
    // add table cells
    row.getCells().add("Column (" + row_count + ", 1)");
    row.getCells().add("Column (" + row_count + ", 2)");
    row.getCells().add("Column (" + row_count + ", 3)");
}
able object to first page of input document
pdfDocument.getPages().get_Item(1).getParagraphs().add(table);
updated document containing table object
pdfDocument.save(DATA_DIR.resolve("document_with_table.pdf").toString());
pdfDocument.close();