Iterate through the collection and print table cells.
The following example shows how to extract tables from the whole document:
// Create an instance of Parser class
try(Parserparser=newParser(Constants.SampleInvoicePagesPdf)){// Check if the document supports table extraction
if(!parser.getFeatures().isTables()){System.out.println("Document isn't supports tables extraction.");return;}// Create the layout of tables
TemplateTableLayoutlayout=newTemplateTableLayout(java.util.Arrays.asList(newDouble[]{50.0,95.0,275.0,415.0,485.0,545.0}),java.util.Arrays.asList(newDouble[]{325.0,340.0,365.0,395.0}));// Create the options for table extraction
PageTableAreaOptionsoptions=newPageTableAreaOptions(layout);// Get the document info
IDocumentInfodocumentInfo=parser.getDocumentInfo();// Check if the document has pages
if(documentInfo.getPageCount()==0){System.out.println("Document hasn't pages.");return;}// Iterate over pages
for(intpageIndex=0;pageIndex<documentInfo.getPageCount();pageIndex++){// Print a page number
System.out.println(String.format("Page %d/%d",pageIndex+1,documentInfo.getPageCount()));// Extract tables from the document page
Iterable<PageTableArea>tables=parser.getTables(pageIndex,options);// Iterate over tables
for(PageTableAreat:tables){// Iterate over rows
for(introw=0;row<t.getRowCount();row++){// Iterate over columns
for(intcolumn=0;column<t.getColumnCount();column++){// Get the table cell
PageTableAreaCellcell=t.getCell(row,column);if(cell!=null){// Print the table cell text
System.out.print(cell.getText());System.out.print(" | ");}}System.out.println();}System.out.println();}}}
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples: