Extract Tables from PDF in C++
Contents
[
Hide
]
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.
- Open the input PDF as a
Documentand iterate over its pages. - Create a fresh
TableAbsorberfor each page and callTableAbsorber::Visit(). - Traverse the
AbsorbedTablerows andAbsorbedCellobjects. 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;
}
}
}
}