Extract tables from document page

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Parser MCP server lets your agent work with files on your machine; they are never uploaded. It runs in Docker: replace /path/to/documents with the folder it may use.

claude mcp add groupdocs-parser -- docker run --rm -i -v /path/to/documents:/data ghcr.io/groupdocs-parser/parser-net-mcp:latest
Folders, licensing and more →

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

IEnumerable<PageTableArea> GetTables(int pageIndex, PageTableAreaOptions options);

This method returns a collection of PageTableArea object:

MemberDescription
RectangleThe rectangular area that bounds text area.
PageThe page information (page index and page size)
RowCountThe total number of the table rows.
ColumnCountThe total number of the table columns.
PageTableAreaCell ItemThe table cell by row and column indexes.
double GetRowHeight(int)The the row height.
double GetColumnWidth(int)Returns the column width.

GetTables(Int32, PageTableAreaOptions) accepts PageTableAreaOptions object that contains TemplateTableLayout object with table layout (see this article for more details).

Here are the steps to extract tables from a specific document page:

  1. Instantiate Parser object for the document;
  2. Check if the document supports table extraction using Features.Tables property;
  3. Get document info to determine total page count;
  4. Call GetTables(Int32, PageTableAreaOptions) method with page index and obtain collection of PageTableArea objects;
  5. Iterate through the collection and access table cells.

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

// Create an instance of Parser class
using (Parser parser = new Parser(filePath))
{
    // Check if the document supports table extraction
    if (!parser.Features.Tables)
    {
        Console.WriteLine("Document isn't supports tables extraction.");
        return;
    }
    // Create the layout of tables
    TemplateTableLayout layout = new TemplateTableLayout(
        new double[] { 50, 95, 275, 415, 485, 545 },
        new double[] { 325, 340, 365, 395 });
    // 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.PageCount == 0)
    {
        Console.WriteLine("Document hasn't pages.");
        return;
    }
    // Iterate over pages
    for (int pageIndex = 0; pageIndex < documentInfo.PageCount; pageIndex++)
    {
        // Print a page number 
        Console.WriteLine(string.Format("Page {0}/{1}", pageIndex + 1, documentInfo.PageCount));
        // Extract tables from the document page
        IEnumerable<PageTableArea> tables = parser.GetTables(pageIndex, options);
        // Iterate over tables
        foreach (PageTableArea t in tables)
        {
            // Iterate over rows
            for (int row = 0; row < t.RowCount; row++)
            {
                // Iterate over columns
                for (int column = 0; column < t.ColumnCount; column++)
                {
                    // Get the table cell
                    PageTableAreaCell cell = t[row, column];
                    if (cell != null)
                    {
                        // Print the table cell text
                        Console.Write(cell.Text);
                        Console.Write(" | ");
                    }
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
}

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online image extractor App

Along with full featured .NET library we provide simple, but powerfull free APPs.

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