Remove Tables from Existing PDF Documents in C++

Remove the first table from a page

After detecting tables on page 1, the code removes the first one and saves the modified PDF.

  1. Open the input PDF as a Document and select page 1.
  2. Call TableAbsorber::Visit() to find the tables on that page.
  3. Call TableAbsorber::Remove() with the first detected table and save the PDF.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Text/TableAbsorber/AbsorbedTable.h>
#include <Aspose.PDF.Cpp/Text/TableAbsorber/TableAbsorber.h>
#include <system/collections/list.h>
#include <stdexcept>

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

void RemoveOneTable(String inputFile, String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto page = document->get_Pages()->idx_get(1);
    auto absorber = MakeObject<TableAbsorber>();
    absorber->Visit(page);
    if (absorber->get_TableList()->get_Count() == 0)
        throw std::runtime_error("No tables found");
    absorber->Remove(absorber->get_TableList()->idx_get(0));
    document->Save(outputFile);
}

Remove all detected tables from a page

To clear page 1 of detected tables, the code removes each one and saves the updated PDF.

  1. Open the input PDF as a Document and select page 1.
  2. Call TableAbsorber::Visit() to find the tables on that page.
  3. Copy the detected AbsorbedTable objects into a vector before removing them.
  4. Call TableAbsorber::Remove() for every copied table and save the PDF.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Text/TableAbsorber/AbsorbedTable.h>
#include <Aspose.PDF.Cpp/Text/TableAbsorber/TableAbsorber.h>
#include <system/collections/list.h>
#include <vector>

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

void RemoveAllTables(String inputFile, String outputFile)
{
    auto document = MakeObject<Document>(inputFile);
    auto page = document->get_Pages()->idx_get(1);
    auto absorber = MakeObject<TableAbsorber>();
    absorber->Visit(page);
    std::vector<SharedPtr<AbsorbedTable>> tables;
    for (auto table : absorber->get_TableList())
        tables.push_back(table);
    for (auto table : tables)
        absorber->Remove(table);
    document->Save(outputFile);
}