通过 Java 管理 PDF 中的表格

添加、提取、删除 PDF 文档中的表格。使用 Aspose.PDF for Java 以编程方式修改 PDF 文件

最受欢迎的表格操作 - Java

如何管理 PDF 文档中的表使用 Java 库

为了使用 table,我们将使用 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. 创建 Table 对象。
  4. 设置表格设置(例如,设置边框)。
  5. 填充表格。
  6. 将表格添加到页面。
  7. 保存文件。

在 PDF 中添加表格-Java

Document pdfDocument = new Document(DATA_DIR.resolve("input.pdf").toString());
com.aspose.pdf.Table table = new com.aspose.pdf.Table();

able and cell borders
BorderInfo border = new BorderInfo(BorderSide.All, 0.5f, Color.getLightGray());
table.setBorder(border);
table.setDefaultCellBorder(border);

0 rows with 3 columns each
for (int i = 1; i < 10; i++) {
    Row row = table.getRows().add();
    for (int j = 1; j <= 3; j++) {
        row.getCells().add("Column (" + i + ", " + j + ")");
    }
}

able to the first page and save the document
pdfDocument.getPages().get_Item(1).getParagraphs().add(table);
pdfDocument.save(DATA_DIR.resolve("document_with_table.pdf").toString());
pdfDocument.close();