Manage Tables in PDF using Java

Add, Extract, Delete Tables in PDF document. Use Aspose.PDF for Java to modify PDF files programmatically

Most popular action with Tables via Java

How to manage Tables in PDF document using Aspose.PDF for Java library

In order to work with table, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful and easy to use conversion API for Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.

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>

Add Table to PDF using Java


You need Aspose.PDF for Java to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Access the Page via its index.
  3. Create Table object.
  4. Set table settings (e.g. set the borders).
  5. Populate table.
  6. Add the table to a page.
  7. Save the file.

Add Table in 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();