Aspose::Words::Tables::Table class

Table class

Represents a table in a Word document. To learn more, visit the Working with Tables documentation article.

class Table : public Aspose::Words::CompositeNode

Methods

MethodDescription
Accept(System::SharedPtr<Aspose::Words::DocumentVisitor>) overrideAccepts a visitor.
AcceptEnd(System::SharedPtr<Aspose::Words::DocumentVisitor>) override
AcceptStart(System::SharedPtr<Aspose::Words::DocumentVisitor>) override
AppendChild(T)
AutoFit(Aspose::Words::Tables::AutoFitBehavior)Resizes the table and cells according to the specified auto fit behavior.
ClearBorders()Removes all table and cell borders on this table.
ClearShading()Removes all shading on the table.
Clone(bool)Creates a duplicate of the node.
ConvertToHorizontallyMergedCells()Converts cells horizontally merged by width to cells merged by HorizontalMerge.
EnsureMinimum()If the table has no rows, creates and appends one Row.
get_AbsoluteHorizontalDistance()Gets or sets absolute horizontal floating table position specified by the table properties, in points. Default value is 0.
get_AbsoluteVerticalDistance()Gets or sets absolute vertical floating table position specified by the table properties, in points. Default value is 0.
get_Alignment()Specifies how an inline table is aligned in the document.
get_AllowAutoFit()Allows Microsoft Word and Aspose.Words to automatically resize cells in a table to fit their contents.
get_AllowCellSpacing()Gets the “Allow spacing between cells” option.
get_AllowOverlap()Gets whether a floating table shall allow other floating objects in the document to overlap its extents when displayed. Default value is true.
get_Bidi()Gets or sets whether this is a right-to-left table.
get_BottomPadding()Gets or sets the amount of space (in points) to add below the contents of cells.
get_CellSpacing()Gets or sets the amount of space (in points) between the cells.
get_Count()Gets the number of immediate children of this node.
get_CustomNodeId() constSpecifies custom node identifier.
get_Description()Gets or sets description of this table. It provides an alternative text representation of the information contained in the table.
get_DistanceBottom()Gets or sets distance between table bottom and the surrounding text, in points.
get_DistanceLeft()Gets or sets distance between table left and the surrounding text, in points.
get_DistanceRight()Gets or sets distance between table right and the surrounding text, in points.
get_DistanceTop()Gets or sets distance between table top and the surrounding text, in points.
virtual get_Document() constGets the document to which this node belongs.
get_FirstChild() constGets the first child of the node.
get_FirstRow()Returns the first Row node in the table.
get_HasChildNodes()Returns true if this node has any child nodes.
get_HorizontalAnchor()Gets the base object from which the horizontal positioning of floating table should be calculated. Default value is Column.
get_IsComposite() overrideReturns true as this node can have child nodes.
get_LastChild() constGets the last child of the node.
get_LastRow()Returns the last Row node in the table.
get_LeftIndent()Gets or sets the value that represents the left indent of the table.
get_LeftPadding()Gets or sets the amount of space (in points) to add to the left of the contents of cells.
get_NextNode() const
get_NextSibling()Gets the node immediately following this node.
get_NodeType() const overrideReturns Table.
get_ParentNode()Gets the immediate parent of this node.
get_PreferredWidth()Gets or sets the table preferred width.
get_PreviousSibling()Gets the node immediately preceding this node.
get_PrevNode() const
get_Range()Returns a Range object that represents the portion of a document that is contained in this node.
get_RelativeHorizontalAlignment()Gets or sets floating table relative horizontal alignment.
get_RelativeVerticalAlignment()Gets or sets floating table relative vertical alignment.
get_RightPadding()Gets or sets the amount of space (in points) to add to the right of the contents of cells.
get_Rows()Provides typed access to the rows of the table.
get_Style()Gets or sets the table style applied to this table.
get_StyleIdentifier()Gets or sets the locale independent style identifier of the table style applied to this table.
get_StyleName()Gets or sets the name of the table style applied to this table.
get_StyleOptions()Gets or sets bit flags that specify how a table style is applied to this table.
get_TextWrapping()Gets or sets TextWrapping for table.
get_Title()Gets or sets title of this table. It provides an alternative text representation of the information contained in the table.
get_TopPadding()Gets or sets the amount of space (in points) to add above the contents of cells.
get_VerticalAnchor()Gets the base object from which the vertical positioning of floating table should be calculated. Default value is Margin.
GetAncestor(Aspose::Words::NodeType)Gets the first ancestor of the specified NodeType.
GetAncestorOf()
GetChild(Aspose::Words::NodeType, int32_t, bool)Returns an Nth child node that matches the specified type.
GetChildNodes(Aspose::Words::NodeType, bool)Returns a live collection of child nodes that match the specified type.
GetEnumerator() overrideProvides support for the for each style iteration over the child nodes of this node.
GetText() overrideGets the text of this node and of all its children.
GetType() const override
IndexOf(const System::SharedPtr<Aspose::Words::Node>&)Returns the index of the specified child node in the child node array.
InsertAfter(T, const System::SharedPtr<Aspose::Words::Node>&)
InsertBefore(T, const System::SharedPtr<Aspose::Words::Node>&)
Is(const System::TypeInfo&) const override
IsAncestorNode(const System::SharedPtr<Aspose::Words::Node>&)
NextPreOrder(const System::SharedPtr<Aspose::Words::Node>&)Gets next node according to the pre-order tree traversal algorithm.
static NodeTypeToString(Aspose::Words::NodeType)A utility method that converts a node type enum value into a user friendly string.
PrependChild(T)
PreviousPreOrder(const System::SharedPtr<Aspose::Words::Node>&)Gets the previous node according to the pre-order tree traversal algorithm.
Remove()Removes itself from the parent.
RemoveAllChildren()Removes all the child nodes of the current node.
RemoveChild(T)
RemoveSmartTags()Removes all SmartTag descendant nodes of the current node.
SelectNodes(const System::String&)Selects a list of nodes matching the XPath expression.
SelectSingleNode(const System::String&)Selects the first Node that matches the XPath expression.
set_AbsoluteHorizontalDistance(double)Setter for Aspose::Words::Tables::Table::get_AbsoluteHorizontalDistance.
set_AbsoluteVerticalDistance(double)Setter for Aspose::Words::Tables::Table::get_AbsoluteVerticalDistance.
set_Alignment(Aspose::Words::Tables::TableAlignment)Setter for Aspose::Words::Tables::Table::get_Alignment.
set_AllowAutoFit(bool)Setter for Aspose::Words::Tables::Table::get_AllowAutoFit.
set_AllowCellSpacing(bool)Sets the “Allow spacing between cells” option.
set_Bidi(bool)Setter for Aspose::Words::Tables::Table::get_Bidi.
set_BottomPadding(double)Setter for Aspose::Words::Tables::Table::get_BottomPadding.
set_CellSpacing(double)Setter for Aspose::Words::Tables::Table::get_CellSpacing.
set_CustomNodeId(int32_t)Setter for Aspose::Words::Node::get_CustomNodeId.
set_Description(const System::String&)Setter for Aspose::Words::Tables::Table::get_Description.
set_DistanceBottom(double)Setter for Aspose::Words::Tables::Table::get_DistanceBottom.
set_DistanceLeft(double)Setter for Aspose::Words::Tables::Table::get_DistanceLeft.
set_DistanceRight(double)Setter for Aspose::Words::Tables::Table::get_DistanceRight.
set_DistanceTop(double)Setter for Aspose::Words::Tables::Table::get_DistanceTop.
set_HorizontalAnchor(Aspose::Words::Drawing::RelativeHorizontalPosition)Setter for Aspose::Words::Tables::Table::get_HorizontalAnchor.
set_LeftIndent(double)Setter for Aspose::Words::Tables::Table::get_LeftIndent.
set_LeftPadding(double)Setter for Aspose::Words::Tables::Table::get_LeftPadding.
set_NextNode(const System::SharedPtr<Aspose::Words::Node>&)
set_PreferredWidth(const System::SharedPtr<Aspose::Words::Tables::PreferredWidth>&)Setter for Aspose::Words::Tables::Table::get_PreferredWidth.
set_PrevNode(const System::SharedPtr<Aspose::Words::Node>&)
set_RelativeHorizontalAlignment(Aspose::Words::Drawing::HorizontalAlignment)Setter for Aspose::Words::Tables::Table::get_RelativeHorizontalAlignment.
set_RelativeVerticalAlignment(Aspose::Words::Drawing::VerticalAlignment)Setter for Aspose::Words::Tables::Table::get_RelativeVerticalAlignment.
set_RightPadding(double)Setter for Aspose::Words::Tables::Table::get_RightPadding.
set_Style(const System::SharedPtr<Aspose::Words::Style>&)Setter for Aspose::Words::Tables::Table::get_Style.
set_StyleIdentifier(Aspose::Words::StyleIdentifier)Setter for Aspose::Words::Tables::Table::get_StyleIdentifier.
set_StyleName(const System::String&)Setter for Aspose::Words::Tables::Table::get_StyleName.
set_StyleOptions(Aspose::Words::Tables::TableStyleOptions)Setter for Aspose::Words::Tables::Table::get_StyleOptions.
set_TextWrapping(Aspose::Words::Tables::TextWrapping)Setter for Aspose::Words::Tables::Table::get_TextWrapping.
set_Title(const System::String&)Setter for Aspose::Words::Tables::Table::get_Title.
set_TopPadding(double)Setter for Aspose::Words::Tables::Table::get_TopPadding.
set_VerticalAnchor(Aspose::Words::Drawing::RelativeVerticalPosition)Setter for Aspose::Words::Tables::Table::get_VerticalAnchor.
SetBorder(Aspose::Words::BorderType, Aspose::Words::LineStyle, double, System::Drawing::Color, bool)Sets the specified table border to the specified line style, width and color.
SetBorders(Aspose::Words::LineStyle, double, System::Drawing::Color)Sets all table borders to the specified line style, width and color.
SetParent(const System::SharedPtr<Aspose::Words::Node>&)
SetShading(Aspose::Words::TextureIndex, System::Drawing::Color, System::Drawing::Color)Sets shading to the specified values on whole table.
SetTemplateWeakPtr(uint32_t) override
Table(const System::SharedPtr<Aspose::Words::DocumentBase>&)Initializes a new instance of the Table class.
ToString(Aspose::Words::SaveFormat)Exports the content of the node into a string in the specified format.
ToString(const System::SharedPtr<Aspose::Words::Saving::SaveOptions>&)Exports the content of the node into a string using the specified save options.
static Type()

Remarks

Table is a block-level node and can be a child of classes derived from Story or InlineStory.

Table can contain one or more Row nodes.

A minimal valid table needs to have at least one Row.

Examples

Shows how to build a formatted 2x2 table.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);

SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->get_CellFormat()->set_VerticalAlignment(CellVerticalAlignment::Center);
builder->Write(u"Row 1, cell 1.");
builder->InsertCell();
builder->Write(u"Row 1, cell 2.");
builder->EndRow();

// While building the table, the document builder will apply its current RowFormat/CellFormat property values
// to the current row/cell that its cursor is in and any new rows/cells as it creates them.
ASSERT_EQ(CellVerticalAlignment::Center, table->get_Rows()->idx_get(0)->get_Cells()->idx_get(0)->get_CellFormat()->get_VerticalAlignment());
ASSERT_EQ(CellVerticalAlignment::Center, table->get_Rows()->idx_get(0)->get_Cells()->idx_get(1)->get_CellFormat()->get_VerticalAlignment());

builder->InsertCell();
builder->get_RowFormat()->set_Height(100);
builder->get_RowFormat()->set_HeightRule(HeightRule::Exactly);
builder->get_CellFormat()->set_Orientation(TextOrientation::Upward);
builder->Write(u"Row 2, cell 1.");
builder->InsertCell();
builder->get_CellFormat()->set_Orientation(TextOrientation::Downward);
builder->Write(u"Row 2, cell 2.");
builder->EndRow();
builder->EndTable();

// Previously added rows and cells are not retroactively affected by changes to the builder's formatting.
ASPOSE_ASSERT_EQ(0, table->get_Rows()->idx_get(0)->get_RowFormat()->get_Height());
ASSERT_EQ(HeightRule::Auto, table->get_Rows()->idx_get(0)->get_RowFormat()->get_HeightRule());
ASPOSE_ASSERT_EQ(100, table->get_Rows()->idx_get(1)->get_RowFormat()->get_Height());
ASSERT_EQ(HeightRule::Exactly, table->get_Rows()->idx_get(1)->get_RowFormat()->get_HeightRule());
ASSERT_EQ(TextOrientation::Upward, table->get_Rows()->idx_get(1)->get_Cells()->idx_get(0)->get_CellFormat()->get_Orientation());
ASSERT_EQ(TextOrientation::Downward, table->get_Rows()->idx_get(1)->get_Cells()->idx_get(1)->get_CellFormat()->get_Orientation());

doc->Save(ArtifactsDir + u"DocumentBuilder.BuildTable.docx");

Shows how to create a table.

auto doc = MakeObject<Document>();
auto table = MakeObject<Table>(doc);
doc->get_FirstSection()->get_Body()->AppendChild(table);

// Tables contain rows, which contain cells, which may have paragraphs
// with typical elements such as runs, shapes, and even other tables.
// Calling the "EnsureMinimum" method on a table will ensure that
// the table has at least one row, cell, and paragraph.
auto firstRow = MakeObject<Row>(doc);
table->AppendChild(firstRow);

auto firstCell = MakeObject<Cell>(doc);
firstRow->AppendChild(firstCell);

auto paragraph = MakeObject<Paragraph>(doc);
firstCell->AppendChild(paragraph);

// Add text to the first call in the first row of the table.
auto run = MakeObject<Run>(doc, u"Hello world!");
paragraph->AppendChild(run);

doc->Save(ArtifactsDir + u"Table.CreateTable.docx");

Shows how to iterate through all tables in the document and print the contents of each cell.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
SharedPtr<TableCollection> tables = doc->get_FirstSection()->get_Body()->get_Tables();

ASSERT_EQ(2, tables->ToArray()->get_Length());

for (int i = 0; i < tables->get_Count(); i++)
{
    std::cout << "Start of Table " << i << std::endl;

    SharedPtr<RowCollection> rows = tables->idx_get(i)->get_Rows();

    // We can use the "ToArray" method on a row collection to clone it into an array.
    ASPOSE_ASSERT_EQ(rows, rows->ToArray());
    ASPOSE_ASSERT_NS(rows, rows->ToArray());

    for (int j = 0; j < rows->get_Count(); j++)
    {
        std::cout << "\tStart of Row " << j << std::endl;

        SharedPtr<CellCollection> cells = rows->idx_get(j)->get_Cells();

        // We can use the "ToArray" method on a cell collection to clone it into an array.
        ASPOSE_ASSERT_EQ(cells, cells->ToArray());
        ASPOSE_ASSERT_NS(cells, cells->ToArray());

        for (int k = 0; k < cells->get_Count(); k++)
        {
            String cellText = cells->idx_get(k)->ToString(SaveFormat::Text).Trim();
            std::cout << "\t\tContents of Cell:" << k << " = \"" << cellText << "\"" << std::endl;
        }

        std::cout << "\tEnd of Row " << j << std::endl;
    }

    std::cout << "End of Table " << i << "\n" << std::endl;
}

Shows how to build a nested table without using a document builder.

void CreateNestedTable()
{
    auto doc = MakeObject<Document>();

    // Create the outer table with three rows and four columns, and then add it to the document.
    SharedPtr<Table> outerTable = CreateTable(doc, 3, 4, u"Outer Table");
    doc->get_FirstSection()->get_Body()->AppendChild(outerTable);

    // Create another table with two rows and two columns and then insert it into the first table's first cell.
    SharedPtr<Table> innerTable = CreateTable(doc, 2, 2, u"Inner Table");
    outerTable->get_FirstRow()->get_FirstCell()->AppendChild(innerTable);

    doc->Save(ArtifactsDir + u"Table.CreateNestedTable.docx");
}

static SharedPtr<Table> CreateTable(SharedPtr<Document> doc, int rowCount, int cellCount, String cellText)
{
    auto table = MakeObject<Table>(doc);

    for (int rowId = 1; rowId <= rowCount; rowId++)
    {
        auto row = MakeObject<Row>(doc);
        table->AppendChild(row);

        for (int cellId = 1; cellId <= cellCount; cellId++)
        {
            auto cell = MakeObject<Cell>(doc);
            cell->AppendChild(MakeObject<Paragraph>(doc));
            cell->get_FirstParagraph()->AppendChild(MakeObject<Run>(doc, cellText));

            row->AppendChild(cell);
        }
    }

    // You can use the "Title" and "Description" properties to add a title and description respectively to your table.
    // The table must have at least one row before we can use these properties.
    // These properties are meaningful for ISO / IEC 29500 compliant .docx documents (see the OoxmlCompliance class).
    // If we save the document to pre-ISO/IEC 29500 formats, Microsoft Word ignores these properties.
    table->set_Title(u"Aspose table title");
    table->set_Description(u"Aspose table description");

    return table;
}

See Also