Remove Tables from Existing PDF Documents in C++
Contents
[
Hide
]
Remove the first table from a page
After detecting tables on page 1, the code removes the first one and saves the modified PDF.
- Open the input PDF as a
Documentand select page 1. - Call
TableAbsorber::Visit()to find the tables on that page. - 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.
- Open the input PDF as a
Documentand select page 1. - Call
TableAbsorber::Visit()to find the tables on that page. - Copy the detected
AbsorbedTableobjects into a vector before removing them. - 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);
}