Update Tables in Existing PDF Documents in C++
Contents
[
Hide
]
Replace text in a table cell
To update a single cell, the code changes the second text fragment in the upper-left cell of the first table detected on page 1 and saves the PDF.
- Open the input PDF as a
Documentand select page 1. - Call
TableAbsorber::Visit()to find the tables on that page. - Select the first table, its first row, and its first
AbsorbedCell. - Set the text of the second
TextFragmenttoNew Value. The sample requires at least two text fragments in that cell. - Save the
Documentto the output 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/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 <system/collections/list.h>
#include <stdexcept>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;
void ReplaceCells(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");
auto rows = absorber->get_TableList()->idx_get(0)->get_RowList();
if (rows->get_Count() == 0)
throw std::runtime_error("The target table has no rows");
auto cells = rows->idx_get(0)->get_CellList();
if (cells->get_Count() == 0)
throw std::runtime_error("The target row has no cells");
auto fragments = cells->idx_get(0)->get_TextFragments();
if (fragments->get_Count() == 0)
throw std::runtime_error("The target cell has no text fragments");
fragments->idx_get(1)->set_Text(u"New Value");
document->Save(outputFile);
}
Replace an existing table
A new two-row, three-column table takes the place of the first table detected on page 1.
- Open the input PDF as a
Documentand select page 1. - Call
TableAbsorber::Visit()to find the tables on that page. - Create a new
Tablewith two rows, three columns, and default cell borders. - Call
TableAbsorber::Replace()to replace the first detected table, then save the PDF.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Generator/BorderInfo.h>
#include <Aspose.PDF.Cpp/Generator/BorderSide.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Table/Cell.h>
#include <Aspose.PDF.Cpp/Table/Cells.h>
#include <Aspose.PDF.Cpp/Table/Row.h>
#include <Aspose.PDF.Cpp/Table/Rows.h>
#include <Aspose.PDF.Cpp/Table/Table.h>
#include <Aspose.PDF.Cpp/Text/TableAbsorber/AbsorbedTable.h>
#include <Aspose.PDF.Cpp/Text/TableAbsorber/TableAbsorber.h>
#include <system/collections/list.h>
#include <stdexcept>
#include <initializer_list>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;
void ReplaceTable(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");
auto table = MakeObject<Table>();
table->set_ColumnWidths(u"100 100 100");
table->set_DefaultCellBorder(MakeObject<BorderInfo>(BorderSide::All, 1.0f));
auto row = table->get_Rows()->Add();
for (const auto &text : {u"Col 1", u"Col 2", u"Col 3"})
row->get_Cells()->Add(text);
row = table->get_Rows()->Add();
for (const auto &text : {u"Col 12", u"Col 22", u"Col 32"})
row->get_Cells()->Add(text);
absorber->Replace(page, absorber->get_TableList()->idx_get(0), table);
document->Save(outputFile);
}