A table lets the readers go through the data quickly without reading a lot of text. Therefore, tables are considered an integral part of the documents. While working on PDF automation and generating the PDF files programmatically, you may come across the need to create tables very often. To accomplish that, this article covers how to create tables in PDF files using Java.
- Java API to Create Table in PDF
- Create a Table in a PDF File
- Set Borders and Margins in PDF Tables
- Apply Column Adjustment to PDF Tables
Java API to Create Table in PDF - Free Download
Aspose.PDF for Java is a popular API to perform PDF generation and manipulation within Java applications. Using the API, you can seamlessly create PDF files of simple and complex layouts. We will use this API to create tables in new or existing PDF files. You can download the API or install it using the following Maven configurations.
Repository:
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>22.4</version>
</dependency>
Create a Table in a PDF in Java
You can create tables in either a new or an existing PDF file using Aspose.PDF for Java. The following are the steps to create a table in a PDF file in Java.
- Load the PDF file (or create a new one) using Document class.
- Initialize a table and set its columns and rows using Table class.
- Set table’s settings (i.e. borders).
- Populate the table by creating rows using Table.getRows().add() method.
- Add the table to the page using Document.getPages().get_Item(index).getParagraphs().add(Table) method.
- Save the PDF file using Document.save(String) method.
The following code sample shows how to create a table in a PDF file using Java.
The following is the output of the above code sample.
Java Create PDF Table with Customized Borders and Margins
You can also customize the borders and margins of the tables in PDF as per your requirements. For example, you can set the border width, border style, and top, bottom, left, and right margins. The following are the steps to set borders and margins for the tables in PDF files in Java.
- Load the PDF file (or create a new one) using Document class.
- Initialize a table and set its columns and rows using Table class.
- Create an instance of BorderInfo class to apply border styling.
- Create an instance of MarginInfo class to set margins for the table.
- Populate the table by creating rows using Table.getRows().add() method.
- Add the table to the page using Document.getPages().get_Item(index).getParagraphs().add(Table) method.
- Save the PDF file using Document.save(String) method.
The following code sample shows how to set borders and margins for tables in PDF using Java.
The following screenshot shows the output of the above code sample.
Java Create PDF Tables - Apply Column Adjustment
Aspose.PDF for Java also allows you to customize the column adjustment of the table in a PDF. For example, you can auto-fit the table to the window or the content. The following are the steps to set the column adjustment of a table in PDF using Java.
- Load the PDF file (or create a new one) using Document class.
- Initialize a table and set its columns and rows using Table class.
- Set borders and margins.
- Set column adjustment with the desired value of ColumnAdjustment enum.
- Populate the table by creating rows using Table.getRows().add() method.
- Add the table to the page using Document.getPages().get_Item(index).getParagraphs().add(Table) method.
- Save the PDF file using Document.save(String) method.
The following code sample shows how to set the column adjustment of the table in PDF using Java.
Java API to Create PDF Table - Get a Free License
You can get a free temporary license in order to use Aspose.PDF for Java without evaluation limitations.
Conclusion
In this article, you have learned how to create tables in PDF files using Java. Furthermore, you have seen how to customize the borders. margins, and column adjustment of a table in PDF. Besides, you can explore more about the Java PDF API using the documentation. In case you would have any questions or queries, you can contact us via our forum.