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 CompositeNode

Methods

MethodDescription
Accept(System::SharedPtr<Aspose::Words::DocumentVisitor>) overrideAccepts a visitor.
AcceptEnd(System::SharedPtr<Aspose::Words::DocumentVisitor>) overrideAccepts a visitor for visiting the end of the table.
AcceptStart(System::SharedPtr<Aspose::Words::DocumentVisitor>) overrideAccepts a visitor for visiting the start of the table.
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.
ConvertToHorizontallyMergedCells()Converts cells horizontally merged by width to cells merged by HorizontalMerge.
EnsureMinimum()If the table has no rows, creates and appends one Row.
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.
SetShading(Aspose::Words::TextureIndex, System::Drawing::Color, System::Drawing::Color)Sets shading to the specified values on whole table.
Table(const System::SharedPtr<Aspose::Words::DocumentBase> &)Initializes a new instance of the Table class.
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 or sets 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_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.
get_FirstRow()Returns the first Row node in the table.
get_HorizontalAnchor()Gets the base object from which the horizontal positioning of floating table should be calculated. Default value is Column.
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_NodeType() const overrideReturns Table.
get_PreferredWidth()Gets or sets the table preferred width.
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.
set_AbsoluteHorizontalDistance(double)Sets absolute horizontal floating table position specified by the table properties, in points. Default value is 0.
set_AbsoluteVerticalDistance(double)Sets absolute vertical floating table position specified by the table properties, in points. Default value is 0.
set_Alignment(Aspose::Words::Tables::TableAlignment)Specifies how an inline table is aligned in the document.
set_AllowAutoFit(bool)Allows Microsoft Word and Aspose.Words to automatically resize cells in a table to fit their contents.
set_AllowCellSpacing(bool)Sets the “Allow spacing between cells” option.
set_Bidi(bool)Sets whether this is a right-to-left table.
set_BottomPadding(double)Sets the amount of space (in points) to add below the contents of cells.
set_CellSpacing(double)Sets the amount of space (in points) between the cells.
set_Description(const System::String &)Sets description of this table. It provides an alternative text representation of the information contained in the table.
set_DistanceBottom(double)Sets distance between table bottom and the surrounding text, in points.
set_DistanceLeft(double)Sets distance between table left and the surrounding text, in points.
set_DistanceRight(double)Sets distance between table right and the surrounding text, in points.
set_DistanceTop(double)Sets distance between table top and the surrounding text, in points.
set_HorizontalAnchor(Aspose::Words::Drawing::RelativeHorizontalPosition)Gets the base object from which the horizontal positioning of floating table should be calculated. Default value is Column.
set_LeftIndent(double)Sets the value that represents the left indent of the table.
set_LeftPadding(double)Sets the amount of space (in points) to add to the left of the contents of cells.
set_PreferredWidth(const System::SharedPtr<Aspose::Words::Tables::PreferredWidth> &)Sets the table preferred width.
set_RelativeHorizontalAlignment(Aspose::Words::Drawing::HorizontalAlignment)Sets floating table relative horizontal alignment.
set_RelativeVerticalAlignment(Aspose::Words::Drawing::VerticalAlignment)Sets floating table relative vertical alignment.
set_RightPadding(double)Sets the amount of space (in points) to add to the right of the contents of cells.
set_Style(const System::SharedPtr<Aspose::Words::Style> &)Sets the table style applied to this table.
set_StyleIdentifier(Aspose::Words::StyleIdentifier)Sets the locale independent style identifier of the table style applied to this table.
set_StyleName(const System::String &)Sets the name of the table style applied to this table.
set_StyleOptions(Aspose::Words::Tables::TableStyleOptions)Sets bit flags that specify how a table style is applied to this table.
set_TextWrapping(Aspose::Words::Tables::TextWrapping)Sets TextWrapping for table.
set_Title(const System::String &)Sets title of this table. It provides an alternative text representation of the information contained in the table.
set_TopPadding(double)Sets the amount of space (in points) to add above the contents of cells.
set_VerticalAnchor(Aspose::Words::Drawing::RelativeVerticalPosition)Gets the base object from which the vertical positioning of floating table should be calculated. Default value is Margin.
virtual AcceptEnd(System::SharedPtr<Aspose::Words::DocumentVisitor>)When implemented in a derived class, calls the VisitXXXEnd method of the specified document visitor.
virtual AcceptStart(System::SharedPtr<Aspose::Words::DocumentVisitor>)When implemented in a derived class, calls the VisitXXXStart method of the specified document visitor.
AppendChild(T)Adds the specified node to the end of the list of child nodes for this node.
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.
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> &)Inserts the specified node immediately after the specified reference node.
InsertBefore(T, const System::SharedPtr<Aspose::Words::Node> &)Inserts the specified node immediately before the specified reference node.
PrependChild(T)Adds the specified node to the beginning of the list of child nodes for this node.
RemoveAllChildren()Removes all the child nodes of the current node.
RemoveChild(T)Removes the specified child node.
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.
get_Count()Gets the number of immediate children of this node.
get_FirstChild() constGets the first child of the node.
get_HasChildNodes()Returns true if this node has any child nodes.
get_IsComposite() overrideReturns true as this node can have child nodes.
get_LastChild() constGets the last child of the node.
virtual Accept(System::SharedPtr<Aspose::Words::DocumentVisitor>)Accepts a visitor.
Clone(bool)Creates a duplicate of the node.
GetAncestor(Aspose::Words::NodeType)Gets the first ancestor of the specified NodeType.
virtual GetText()Gets the text of this node and of all its children.
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.
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.
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.
get_CustomNodeId() constSpecifies custom node identifier.
virtual get_Document() constGets the document to which this node belongs.
virtual get_IsComposite()Returns true if this node can contain other nodes.
get_NextSibling()Gets the node immediately following this node.
virtual get_NodeType() constGets the type of this node.
get_ParentNode()Gets the immediate parent of this node.
get_PreviousSibling()Gets the node immediately preceding this node.
get_Range()Returns a Range object that represents the portion of a document that is contained in this node.
set_CustomNodeId(int32_t)Specifies custom node identifier.

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 = System::MakeObject<Aspose::Words::Document>();
auto builder = System::MakeObject<Aspose::Words::DocumentBuilder>(doc);

System::SharedPtr<Aspose::Words::Tables::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(table->get_Rows()->idx_get(0)->get_Cells()->idx_get(0)->get_CellFormat()->get_VerticalAlignment(), CellVerticalAlignment::Center);
ASSERT_EQ(table->get_Rows()->idx_get(0)->get_Cells()->idx_get(1)->get_CellFormat()->get_VerticalAlignment(), CellVerticalAlignment::Center);

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(table->get_Rows()->idx_get(0)->get_RowFormat()->get_Height(), 0);
ASSERT_EQ(table->get_Rows()->idx_get(0)->get_RowFormat()->get_HeightRule(), HeightRule::Auto);
ASPOSE_ASSERT_EQ(table->get_Rows()->idx_get(1)->get_RowFormat()->get_Height(), 100);
ASSERT_EQ(table->get_Rows()->idx_get(1)->get_RowFormat()->get_HeightRule(), HeightRule::Exactly);
ASSERT_EQ(table->get_Rows()->idx_get(1)->get_Cells()->idx_get(0)->get_CellFormat()->get_Orientation(), TextOrientation::Upward);
ASSERT_EQ(table->get_Rows()->idx_get(1)->get_Cells()->idx_get(1)->get_CellFormat()->get_Orientation(), TextOrientation::Downward);

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

Shows how to create a table.

auto doc = System::MakeObject<Aspose::Words::Document>();
auto table = System::MakeObject<Aspose::Words::Tables::Table>(doc);
doc->get_FirstSection()->get_Body()->AppendChild<System::SharedPtr<Aspose::Words::Tables::Table>>(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 = System::MakeObject<Aspose::Words::Tables::Row>(doc);
table->AppendChild<System::SharedPtr<Aspose::Words::Tables::Row>>(firstRow);

auto firstCell = System::MakeObject<Aspose::Words::Tables::Cell>(doc);
firstRow->AppendChild<System::SharedPtr<Aspose::Words::Tables::Cell>>(firstCell);

auto paragraph = System::MakeObject<Aspose::Words::Paragraph>(doc);
firstCell->AppendChild<System::SharedPtr<Aspose::Words::Paragraph>>(paragraph);

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

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

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

auto doc = System::MakeObject<Aspose::Words::Document>(System::String(get_MyDir() + u"Tables.docx"));
System::SharedPtr<Aspose::Words::Tables::TableCollection> tables = doc->get_FirstSection()->get_Body()->get_Tables();

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

for (int32_t i = 0; i < tables->get_Count(); i++)
{
    std::cout << System::String::Format(u"Start of Table {0}", i) << std::endl;

    System::SharedPtr<Aspose::Words::Tables::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->ToArray(), rows);
    ASSERT_FALSE(System::Object::ReferenceEquals(rows->ToArray(), rows));

    for (int32_t j = 0; j < rows->get_Count(); j++)
    {
        std::cout << System::String::Format(u"\tStart of Row {0}", j) << std::endl;

        System::SharedPtr<Aspose::Words::Tables::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->ToArray(), cells);
        ASSERT_FALSE(System::Object::ReferenceEquals(cells->ToArray(), cells));

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

        std::cout << System::String::Format(u"\tEnd of Row {0}", j) << std::endl;
    }

    std::cout << System::String::Format(u"End of Table {0}\n", i) << std::endl;
}

See Also