Extract Tables from PDF in C++

Extract tables from every page

Scanning every page of an existing PDF yields the text of detected tables, which the code prints to the console one row per line.

  1. Open the input PDF as a Document and iterate over its pages.
  2. Create a fresh TableAbsorber for each page and call TableAbsorber::Visit().
  3. Traverse the AbsorbedTable rows and AbsorbedCell objects. Print each cell’s text segments and end each row with a newline.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Text/TableAbsorber/AbsorbedCell.h>
#include <Aspose.PDF.Cpp/Text/TableAbsorber/AbsorbedRow.h>
#include <Aspose.PDF.Cpp/Text/TableAbsorber/AbsorbedTable.h>
#include <Aspose.PDF.Cpp/Text/TableAbsorber/TableAbsorber.h>
#include <Aspose.PDF.Cpp/Text/TextFragment.h>
#include <Aspose.PDF.Cpp/Text/TextFragmentCollection.h>
#include <Aspose.PDF.Cpp/Text/TextSegment.h>
#include <Aspose.PDF.Cpp/Text/TextSegmentCollection.h>
#include <system/collections/list.h>
#include <iostream>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;

void Extract(String inputFile, String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    for (auto page : document->get_Pages())
    {
        auto absorber = MakeObject<TableAbsorber>();
        absorber->Visit(page);
        for (auto table : absorber->get_TableList())
        {
            std::cout << "Table ----" << std::endl;
            for (auto row : table->get_RowList())
            {
                for (auto cell : row->get_CellList())
                {
                    std::cout << " | ";
                    for (auto fragment : cell->get_TextFragments())
                        for (auto segment : fragment->get_Segments())
                            std::cout << segment->get_Text();
                }
                std::cout << std::endl;
            }
        }
    }
}