Manage Tables in PDF via 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 documentUsing 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 via 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 setting (e.g. set the borders).
  5. Populate table.
  6. Add the table to a page.
  7. Save the file.

Add Table in PDF - Java


    // Load source PDF document
    Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir+ "AddTable.pdf");
    // Initializes a new instance of the Table
    Aspose.Pdf.Table table = new Aspose.Pdf.Table();
    // Set the table border color as LightGray
    table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
    // Set the border for table cells
    table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
    // Create a loop to add 10 rows
    for (int row_count = 1; row_count < 10; row_count++)
    {
        // Add row to table
        Aspose.Pdf.Row row = table.Rows.Add();
        // Add table cells
        row.Cells.Add("Column (" + row_count + ", 1)");
        row.Cells.Add("Column (" + row_count + ", 2)");
        row.Cells.Add("Column (" + row_count + ", 3)");
    }
    // Add table object to first page of input document
    doc.Pages[1].Paragraphs.Add(table);
    dataDir = dataDir + "document_with_table_out.pdf";