In order to add 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>
<id>AsposeJavaAPI</id>
<name>Aspose Java AP</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
<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.
- Load the PDF with an instance of Document.
- Access the Page via its index.
- Create Table object.
- Set table setting (e.g. set the borders).
- Populate table.
- Add the table to a page.
- Save the file.
Add Table in 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();