How to Add Rows to Table in DOCX using C++

In this topic, we will learn how to add Rows to Table in DOCX using C++. Tables have wide utility inside Word document and you can easily insert rows inside table of Word document by using a very simple C++ API interface.

Steps to Insert Rows inside Table in DOCX using C++

  1. Install Aspose.Words.Cpp using NuGet package Manager
  2. Add reference to Aspose::Words and Aspose::Words::Tables namespaces
  3. Instantiate Document Class object to load document for managing Table Rows
  4. Access the Table in first section by using Table Class object
  5. Add rows in Table using multiple options
  6. Save the Word document with table row to DOCX format using Save method

In the following example we have shown, how you can insert rows from scratch inside table of a Word document. It also describes, how you can clone existing rows inside table and add new ones using cloned rows inside DOCX table.

Code to Insert Rows inside Table in DOCX using C++

#pragma once
#include <cstdint>
#include <iostream>
#include <Aspose.Words.Cpp/Document.h>
#include <Aspose.Words.Cpp/Section.h>
#include <Aspose.Words.Cpp/Body.h>
#include <Aspose.Words.Cpp/Paragraph.h>
#include <Aspose.Words.Cpp/DocumentBase.h>
#include <Aspose.Words.Cpp/DocumentBuilder.h>
#include <Aspose.Words.Cpp/Drawing/Shape.h>
#include <Aspose.Words.Cpp/HeaderFooterType.h>
#include <Aspose.Words.Cpp/Saving/SaveOutputParameters.h>
#include <system/array.h>
#include <system/exceptions.h>
#include <Aspose.Words.Cpp/Tables/Cell.h>
#include <Aspose.Words.Cpp/Tables/CellCollection.h>
#include <Aspose.Words.Cpp/Tables/CellFormat.h>
#include <Aspose.Words.Cpp/Tables/PreferredWidth.h>
#include <Aspose.Words.Cpp/Tables/Row.h>
#include <Aspose.Words.Cpp/Tables/RowCollection.h>
#include <Aspose.Words.Cpp/Tables/Table.h>
#include <Aspose.Words.Cpp/Tables/TableCollection.h>
//#include <Aspose.Words.Cpp/Tables/Tables.h>s
#include <Aspose.Words.Cpp/Run.h>
#include <Aspose.Words.Cpp/RunCollection.h>
#include <system/enumerator_adapter.h>
#include <Aspose.Words.Cpp/License.h>
#include <system/io/path.h>
using System::ArrayPtr;
using System::MakeArray;
using System::MakeObject;
using System::SharedPtr;
using System::String;
using namespace Aspose::Words;
using namespace Aspose::Words::Tables;
class AddTableInWordDocumentUsingCpp
{
public:
void AddTableRows()
{
// Load and Set API License
System::String LicFilePath = u"Aspose.Total.CPP.lic";
SharedPtr<License> WordsCPPLicenseForTable = System::MakeObject<License>();
// Setting product license
WordsCPPLicenseForTable->SetLicense(LicFilePath);
// Instantiate Document class to load document for managing table
SharedPtr<Document> AddTableToDOCX = MakeObject<Document>(u"WordDocument.docx");
// Access the Table by index in first section
SharedPtr <Table> tableToAddRowsTo = AddTableToDOCX->get_FirstSection()->get_Body()
->get_Tables()->idx_get(0);
// Instantiate a new Row Object
SharedPtr <Row> row = MakeObject<Row>(AddTableToDOCX);
// Add 3 Cells for added Row by accessing it cells collection
for (int iRowIndex = 0; iRowIndex < 3; iRowIndex++)
{
SharedPtr < Cell> cell = MakeObject <Cell>(AddTableToDOCX);
cell->AppendChild(MakeObject <Paragraph>(AddTableToDOCX));
cell->get_FirstParagraph()->get_Runs()->Add(MakeObject <Run>(AddTableToDOCX, u"Text in Cell " + iRowIndex));
row->get_Cells()->Add(cell);
}
// Insert the Row after first row in table
tableToAddRowsTo->get_Rows()->Insert(1, row);
// Now, clone the existing Row in Table
SharedPtr <Row> cloneOfRow = System::DynamicCast<Row>(tableToAddRowsTo->get_FirstRow()->Clone(true));
// Clear all content from all Cells of the row
for(SharedPtr<Node> node : cloneOfRow->get_Cells())
{
SharedPtr<Cell> cell = System::DynamicCast<Cell>(node);
cell->RemoveAllChildren();
cell->EnsureMinimum();
}
// Now see, how to add multiple empty rows at the end of table
for (int iRowCounter = 0; iRowCounter < 10; iRowCounter++)
{
SharedPtr<Row> emptyRow = System::DynamicCast<Row>(cloneOfRow->Clone(true));
tableToAddRowsTo->get_Rows()->Add(emptyRow);
}
// Save the word document with table to DOCX format
AddTableToDOCX->Save(u"InsertTableinDocx.docx");
}
};

In previous example, we looked in to How to Insert Header and Footer in DOCX using C++. Whereas in this topic, we have learnt about adding rows to table of a Word document using C++.

 English