Extract tables from document page

GroupDocs.Parser provides the functionality to extract tables from document page by the getTable(int, PageTableAreaOptions) method:

Iterable<PageTableArea> getTables(int pageIndex, PageTableAreaOptions options);

This method returns a collection of PageTableArea object:

MemberDescription
getRectangleThe rectangular area that bounds text area.
getPageThe page information (page index and page size)
getRowCountThe total number of the table rows.
getColumnCountThe total number of the table columns.
getCell(int, int)The table cell by row and column indexes.
getRowHeight(int)The the row height.
getColumnWidth(int)Returns the column width.

getTables(int, PageTableAreaOptions) accepts PageTableAreaOption object that contains TemplateTableLayout object with table layout (see this article for more details).

Here are the steps to extract tables from the whole document:

The following example shows how to extract tables from the whole document:

// Create an instance of Parser class
try (Parser parser = new Parser(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
    TemplateTableLayout layout = new TemplateTableLayout(
            java.util.Arrays.asList(new Double[]{50.0, 95.0, 275.0, 415.0, 485.0, 545.0}),
            java.util.Arrays.asList(new Double[]{325.0, 340.0, 365.0, 395.0}));
    // Create the options for table extraction
    PageTableAreaOptions options = new PageTableAreaOptions(layout);
    // Get the document info
    IDocumentInfo documentInfo = parser.getDocumentInfo();
    // Check if the document has pages
    if (documentInfo.getPageCount() == 0) {
        System.out.println("Document hasn't pages.");
        return;
    }
    // Iterate over pages
    for (int pageIndex = 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 (PageTableArea t : tables) {
            // Iterate over rows
            for (int row = 0; row < t.getRowCount(); row++) {
                // Iterate over columns
                for (int column = 0; column < t.getColumnCount(); column++) {
                    // Get the table cell
                    PageTableAreaCell cell = 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:

Free online document parser App

Along with full featured Java library we provide simple, but powerful free Apps.

You are welcome to extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Parser App.