
PDF is a widely used document format that can contain text, graphics, tables, annotations, and more. When you need to programmatically pull data from PDF tables—such as invoice line items—this article explains how to extract tables from PDFs using Java.
- Java API to Extract Data from Tables in PDF
- Extract Data from Tables in PDF
- Extract Table from a Specific Area of Page
Java Library to Extract PDF Table
To read and extract tables from PDF files, we use Aspose.PDF for Java, a comprehensive library for creating, editing, and analyzing PDF documents. You can obtain the JAR directly or add it to your project with Maven.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>22.12</version>
</dependency>
Extract Tables from a PDF in Java
Follow these steps to extract every table from a PDF document using Java:
- Load the PDF with the Document class.
- Iterate through each page via Document.getPages().
- For each page, create a TableAbsorber instance and call TableAbsorber.visit(Page).
- Retrieve the list of detected tables using TableAbsorber.getTableList().
- For every AbsorbedTable, loop through its rows with AbsorbedTable.getRowList().
- Inside each row, iterate over cells via AbsorbedRow.getCellList().
- Finally, extract the text from each cell’s TextFragment collection and output it.
The code snippet below demonstrates the complete process.
Java: Extract PDF Table from a Specific Area
When you only need tables located in a particular region of a page, use the following approach:
- Open the PDF with Document.
- Select the target Page from Document.getPages().
- Identify the rectangular area (Square annotation) that defines the region of interest.
- Initialise a TableAbsorber and call TableAbsorber.visit(Page) for the chosen page.
- Get the tables via TableAbsorber.getTableList() and filter them by the defined region.
- For each matching AbsorbedTable, process rows with AbsorbedTable.getRowList().
- Iterate through cells using AbsorbedRow.getCellList() and read the text from TextFragment collections.
The example below shows how to extract tables from a specific page area.
Free Java PDF Library to Extract Tables
You can request a free temporary license that removes all usage limits, allowing you to extract tables from PDFs without restrictions.
Explore Java PDF Library
Find detailed usage instructions in the official documentation and join the community on our forum for support and best‑practice tips.
Conclusion
This guide demonstrated how to extract tables from PDF files using Java and Aspose.PDF. You learned both full‑page extraction and region‑specific extraction techniques, and you can integrate the provided code samples directly into your applications.