Skip to content

Instantly share code, notes, and snippets.

@aspose-com-kb
Last active February 22, 2022 13:27
How to Read PDF Table in Java. For more information, please follow link: https://kb.aspose.com/pdf/java/how-to-read-pdf-table-in-java/
import com.aspose.pdf.License;
import com.aspose.pdf.AbsorbedCell;
import com.aspose.pdf.AbsorbedRow;
import com.aspose.pdf.AbsorbedTable;
import com.aspose.pdf.Document;
import com.aspose.pdf.TableAbsorber;
import com.aspose.pdf.TextFragmentCollection;
public class ReadPDFTableInJava {
public static void main(String[] args) throws Exception { // main function for reading PDF table data in ReadPDFTableInJava
// For avoiding the trial version limitation, load the Aspose.PDF license prior to reading table data
License licenseForHtmlToPdf = new License();
licenseForHtmlToPdf.setLicense("Aspose.Pdf.lic");
// Load a source PDF document which contains a table in it
Document pdfDocument = new Document("PdfWithTable.pdf");
// Instantiate the TableAbsorber object for PDF tables extraction
TableAbsorber tableAbsorber = new TableAbsorber();
// visit the table collection in the input PDF
tableAbsorber.visit(pdfDocument.getPages().get_Item(1));
// Access the desired table from the tables collection
AbsorbedTable absorbedTable = tableAbsorber.getTableList().get(0);
// Parse all the rows and get each row using the AbsorbedRow
for (AbsorbedRow pdfTableRow : absorbedTable.getRowList())
{
// Access each cell in the cells collection using AbsorbedCell
for (AbsorbedCell pdfTableCell : pdfTableRow.getCellList())
{
// Access each text fragment from the cell
TextFragmentCollection textFragmentCollection = pdfTableCell.getTextFragments();
// Access each text fragment from the fragments collection
for (com.aspose.pdf.TextFragment textFragment : textFragmentCollection)
{
// Display the table cell text
System.out.println(textFragment.getText());
}
}
}
System.out.println("Done");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment