Aspose::Words::Tables::Table Class Reference

Detailed Description

Represents a table in a Word document.

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;
}

#include <Aspose.Words.Cpp/Tables/Table.h>

+ Inheritance diagram for Aspose::Words::Tables::Table:

Public Member Functions

 Table (const SharedPtr< DocumentBase > &doc)
 Initializes a new instance of the Table class. More...
 
bool Accept (SharedPtr< DocumentVisitor > visitor) override
 Accepts a visitor. More...
 
void AutoFit (AutoFitBehavior behavior)
 Resizes the table and cells according to the specified auto fit behavior. More...
 
void ClearBorders ()
 Removes all table and cell borders on this table. More...
 
void ClearShading ()
 Removes all shading on the table. More...
 
void ConvertToHorizontallyMergedCells ()
 Converts cells horizontally merged by width to cells merged by HorizontalMerge. More...
 
void EnsureMinimum ()
 If the table has no rows, creates and appends one Row. More...
 
double get_AbsoluteHorizontalDistance ()
 Gets or sets absolute horizontal floating table position specified by the table properties, in points. Default value is 0. More...
 
double get_AbsoluteVerticalDistance ()
 Gets or sets absolute vertical floating table position specified by the table properties, in points. Default value is 0. More...
 
TableAlignment get_Alignment ()
 Specifies how an inline table is aligned in the document. More...
 
bool get_AllowAutoFit ()
 Allows Microsoft Word and Aspose.Words to automatically resize cells in a table to fit their contents. More...
 
bool get_AllowCellSpacing ()
 Gets or sets the "Allow spacing between cells" option. More...
 
bool 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. More...
 
bool get_Bidi ()
 Gets or sets whether this is a right-to-left table. More...
 
double get_BottomPadding ()
 Gets or sets the amount of space (in points) to add below the contents of cells. More...
 
double get_CellSpacing ()
 Gets or sets the amount of space (in points) between the cells. More...
 
String get_Description ()
 Gets or sets description of this table. It provides an alternative text representation of the information contained in the table. More...
 
double get_DistanceBottom ()
 Gets distance between table bottom and the surrounding text, in points. More...
 
double get_DistanceLeft ()
 Gets distance between table left and the surrounding text, in points. More...
 
double get_DistanceRight ()
 Gets distance between table right and the surrounding text, in points. More...
 
double get_DistanceTop ()
 Gets distance between table top and the surrounding text, in points. More...
 
SharedPtr< Rowget_FirstRow ()
 Returns the first Row node in the table. More...
 
RelativeHorizontalPosition get_HorizontalAnchor ()
 Gets the base object from which the horizontal positioning of floating table should be calculated. Default value is Column. More...
 
SharedPtr< Rowget_LastRow ()
 Returns the last Row node in the table. More...
 
double get_LeftIndent ()
 Gets or sets the value that represents the left indent of the table. More...
 
double get_LeftPadding ()
 Gets or sets the amount of space (in points) to add to the left of the contents of cells. More...
 
NodeType get_NodeType () const override
 Returns NodeType.Table. More...
 
SharedPtr< PreferredWidthget_PreferredWidth ()
 Gets or sets the table preferred width. More...
 
HorizontalAlignment get_RelativeHorizontalAlignment ()
 Gets or sets floating table relative horizontal alignment. More...
 
VerticalAlignment get_RelativeVerticalAlignment ()
 Gets or sets floating table relative vertical alignment. More...
 
double get_RightPadding ()
 Gets or sets the amount of space (in points) to add to the right of the contents of cells. More...
 
SharedPtr< RowCollectionget_Rows ()
 Provides typed access to the rows of the table. More...
 
SharedPtr< Styleget_Style ()
 Gets or sets the table style applied to this table. More...
 
StyleIdentifier get_StyleIdentifier ()
 Gets or sets the locale independent style identifier of the table style applied to this table. More...
 
String get_StyleName ()
 Gets or sets the name of the table style applied to this table. More...
 
TableStyleOptions get_StyleOptions ()
 Gets or sets bit flags that specify how a table style is applied to this table. More...
 
TextWrapping get_TextWrapping ()
 Gets or sets TextWrapping for table. More...
 
String get_Title ()
 Gets or sets title of this table. It provides an alternative text representation of the information contained in the table. More...
 
double get_TopPadding ()
 Gets or sets the amount of space (in points) to add above the contents of cells. More...
 
RelativeVerticalPosition get_VerticalAnchor ()
 Gets the base object from which the vertical positioning of floating table should be calculated. Default value is Margin. More...
 
virtual const TypeInfoGetType () const override
 
virtual bool Is (const TypeInfo &target) const override
 
void set_AbsoluteHorizontalDistance (double value)
 Setter for get_AbsoluteHorizontalDistance. More...
 
void set_AbsoluteVerticalDistance (double value)
 Setter for get_AbsoluteVerticalDistance. More...
 
void set_Alignment (TableAlignment value)
 Setter for get_Alignment. More...
 
void set_AllowAutoFit (bool value)
 Setter for get_AllowAutoFit. More...
 
void set_AllowCellSpacing (bool value)
 Setter for get_AllowCellSpacing. More...
 
void set_Bidi (bool value)
 Setter for get_Bidi. More...
 
void set_BottomPadding (double value)
 Setter for get_BottomPadding. More...
 
void set_CellSpacing (double value)
 Setter for get_CellSpacing. More...
 
void set_Description (const String &value)
 Setter for get_Description. More...
 
void set_HorizontalAnchor (RelativeHorizontalPosition value)
 Setter for get_HorizontalAnchor. More...
 
void set_LeftIndent (double value)
 Setter for get_LeftIndent. More...
 
void set_LeftPadding (double value)
 Setter for get_LeftPadding. More...
 
void set_PreferredWidth (const SharedPtr< PreferredWidth > &value)
 Setter for get_PreferredWidth. More...
 
void set_RelativeHorizontalAlignment (HorizontalAlignment value)
 Setter for get_RelativeHorizontalAlignment. More...
 
void set_RelativeVerticalAlignment (VerticalAlignment value)
 Setter for get_RelativeVerticalAlignment. More...
 
void set_RightPadding (double value)
 Setter for get_RightPadding. More...
 
void set_Style (const SharedPtr< Style > &value)
 Setter for get_Style. More...
 
void set_StyleIdentifier (StyleIdentifier value)
 Setter for get_StyleIdentifier. More...
 
void set_StyleName (const String &value)
 Setter for get_StyleName. More...
 
void set_StyleOptions (TableStyleOptions value)
 Setter for get_StyleOptions. More...
 
void set_TextWrapping (TextWrapping value)
 Setter for get_TextWrapping. More...
 
void set_Title (const String &value)
 Setter for get_Title. More...
 
void set_TopPadding (double value)
 Setter for get_TopPadding. More...
 
void set_VerticalAnchor (RelativeVerticalPosition value)
 Setter for get_VerticalAnchor. More...
 
void SetBorder (BorderType borderType, LineStyle lineStyle, double lineWidth, Color color, bool isOverrideCellBorders)
 Sets the specified table border to the specified line style, width and color. More...
 
void SetBorders (LineStyle lineStyle, double lineWidth, Color color)
 Sets all table borders to the specified line style, width and color. More...
 
void SetShading (TextureIndex texture, Color foregroundColor, Color backgroundColor)
 Sets shading to the specified values on all cells in the table. More...
 
- Public Member Functions inherited from CompositeNode
SharedPtr< NodeAppendChild (const SharedPtr< Node > &newChild)
 Adds the specified node to the end of the list of child nodes for this node. More...
 
SharedPtr< NodeCollectionget_ChildNodes ()
 Gets all immediate child nodes of this node. More...
 
SharedPtr< CompositeNodeget_Container () override
 
int32_t get_Count ()
 Gets the number of immediate children of this node. More...
 
SharedPtr< Nodeget_FirstChild () const
 Gets the first child of the node. More...
 
bool get_HasChildNodes ()
 Returns true if this node has any child nodes. More...
 
bool get_IsComposite () override
 Returns true as this node can have child nodes. More...
 
SharedPtr< Nodeget_LastChild () const
 Gets the last child of the node. More...
 
SharedPtr< NodeGetChild (NodeType nodeType, int32_t index, bool isDeep)
 Returns an Nth child node that matches the specified type. More...
 
SharedPtr< NodeCollectionGetChildNodes (NodeType nodeType, bool isDeep)
 Returns a live collection of child nodes that match the specified type. More...
 
SharedPtr< NodeGetCurrentNode () override
 
SharedPtr< IEnumerator< SharedPtr< Node > > > GetEnumerator () override
 Provides support for the for each style iteration over the child nodes of this node. More...
 
SharedPtr< NodeGetNextMatchingNode (SharedPtr< Node > curNode) override
 
String GetText () override
 Gets the text of this node and of all its children. More...
 
int32_t IndexOf (const SharedPtr< Node > &child)
 Returns the index of the specified child node in the child node array. More...
 
SharedPtr< NodeInsertAfter (const SharedPtr< Node > &newChild, const SharedPtr< Node > &refChild)
 Inserts the specified node immediately after the specified reference node. More...
 
SharedPtr< NodeInsertBefore (const SharedPtr< Node > &newChild, const SharedPtr< Node > &refChild)
 Inserts the specified node immediately before the specified reference node. More...
 
SharedPtr< NodePrependChild (const SharedPtr< Node > &newChild)
 Adds the specified node to the beginning of the list of child nodes for this node. More...
 
void RemoveAllChildren ()
 Removes all the child nodes of the current node. More...
 
SharedPtr< NodeRemoveChild (const SharedPtr< Node > &oldChild)
 Removes the specified child node. More...
 
void RemoveSmartTags ()
 Removes all SmartTag descendant nodes of the current node. More...
 
SharedPtr< NodeListSelectNodes (const String &xpath)
 Selects a list of nodes matching the XPath expression. More...
 
SharedPtr< NodeSelectSingleNode (const String &xpath)
 Selects the first Node that matches the XPath expression. More...
 
- Public Member Functions inherited from Node
SharedPtr< NodeClone (bool isCloneChildren)
 Creates a duplicate of the node. More...
 
int32_t get_CustomNodeId () const
 Specifies custom node identifier. More...
 
virtual SharedPtr< DocumentBaseget_Document () const
 Gets the document to which this node belongs. More...
 
SharedPtr< Nodeget_NextSibling ()
 Gets the node immediately following this node. More...
 
SharedPtr< CompositeNodeget_ParentNode ()
 Gets the immediate parent of this node. More...
 
SharedPtr< Nodeget_PreviousSibling ()
 Gets the node immediately preceding this node. More...
 
SharedPtr< Rangeget_Range ()
 Returns a Range object that represents the portion of a document that is contained in this node. More...
 
SharedPtr< CompositeNodeGetAncestor (NodeType ancestorType)
 Gets the first ancestor of the specified NodeType. More...
 
template<typename T >
GetAncestorOf ()
 
SharedPtr< NodeNextPreOrder (const SharedPtr< Node > &rootNode)
 Gets next node according to the pre-order tree traversal algorithm. More...
 
SharedPtr< NodePreviousPreOrder (const SharedPtr< Node > &rootNode)
 Gets the previous node according to the pre-order tree traversal algorithm. More...
 
void Remove ()
 Removes itself from the parent. More...
 
void set_CustomNodeId (int32_t value)
 Setter for get_CustomNodeId. More...
 
String ToString (SaveFormat saveFormat)
 Exports the content of the node into a string in the specified format. More...
 
String ToString (const SharedPtr< SaveOptions > &saveOptions)
 Exports the content of the node into a string using the specified save options. More...
 

Static Public Member Functions

static const TypeInfoType ()
 
- Static Public Member Functions inherited from CompositeNode
static const TypeInfoType ()
 
- Static Public Member Functions inherited from Node
static String NodeTypeToString (NodeType nodeType)
 A utility method that converts a node type enum value into a user friendly string. More...
 
static const TypeInfoType ()
 

Constructor & Destructor Documentation

◆ Table()

Aspose::Words::Tables::Table::Table ( const System::SharedPtr< Aspose::Words::DocumentBase > &  doc)

Initializes a new instance of the Table class.

When Table is created, it belongs to the specified document, but is not yet part of the document and ParentNode is null.

To append Table to the document use InsertAfter or InsertBefore on the story where you want the table inserted.

Parameters
docThe owner document.
Examples

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 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;
}

Member Function Documentation

◆ Accept()

bool Aspose::Words::Tables::Table::Accept ( System::SharedPtr< Aspose::Words::DocumentVisitor visitor)
overridevirtual

Accepts a visitor.

Enumerates over this node and all of its children. Each node calls a corresponding method on DocumentVisitor.

For more info see the Visitor design pattern.

Parameters
visitorThe visitor that will visit the nodes.
Returns
True if all nodes were visited; false if DocumentVisitor stopped the operation before visiting all nodes.
Examples

Shows how to use a DocumentVisitor implementation to remove all hidden content from a document.

void RemoveHiddenContentFromDocument()
{
auto doc = MakeObject<Document>(MyDir + u"Hidden content.docx");
auto hiddenContentRemover = MakeObject<ExFont::RemoveHiddenContentVisitor>();
// Below are three types of fields which can accept a document visitor,
// which will allow it to visit the accepting node, and then traverse its child nodes in a depth-first manner.
// 1 - Paragraph node:
auto para = System::DynamicCast<Paragraph>(doc->GetChild(NodeType::Paragraph, 4, true));
para->Accept(hiddenContentRemover);
// 2 - Table node:
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
table->Accept(hiddenContentRemover);
// 3 - Document node:
doc->Accept(hiddenContentRemover);
doc->Save(ArtifactsDir + u"Font.RemoveHiddenContentFromDocument.docx");
}
class RemoveHiddenContentVisitor : public DocumentVisitor
{
public:
VisitorAction VisitFieldStart(SharedPtr<FieldStart> fieldStart) override
{
if (fieldStart->get_Font()->get_Hidden())
{
fieldStart->Remove();
}
}
VisitorAction VisitFieldEnd(SharedPtr<FieldEnd> fieldEnd) override
{
if (fieldEnd->get_Font()->get_Hidden())
{
fieldEnd->Remove();
}
}
VisitorAction VisitFieldSeparator(SharedPtr<FieldSeparator> fieldSeparator) override
{
if (fieldSeparator->get_Font()->get_Hidden())
{
fieldSeparator->Remove();
}
}
VisitorAction VisitRun(SharedPtr<Run> run) override
{
if (run->get_Font()->get_Hidden())
{
run->Remove();
}
}
VisitorAction VisitParagraphStart(SharedPtr<Paragraph> paragraph) override
{
if (paragraph->get_ParagraphBreakFont()->get_Hidden())
{
paragraph->Remove();
}
}
VisitorAction VisitFormField(SharedPtr<FormField> formField) override
{
if (formField->get_Font()->get_Hidden())
{
formField->Remove();
}
}
VisitorAction VisitGroupShapeStart(SharedPtr<GroupShape> groupShape) override
{
if (groupShape->get_Font()->get_Hidden())
{
groupShape->Remove();
}
}
VisitorAction VisitShapeStart(SharedPtr<Shape> shape) override
{
if (shape->get_Font()->get_Hidden())
{
shape->Remove();
}
}
VisitorAction VisitCommentStart(SharedPtr<Comment> comment) override
{
if (comment->get_Font()->get_Hidden())
{
comment->Remove();
}
}
VisitorAction VisitFootnoteStart(SharedPtr<Footnote> footnote) override
{
if (footnote->get_Font()->get_Hidden())
{
footnote->Remove();
}
}
VisitorAction VisitSpecialChar(SharedPtr<SpecialChar> specialChar) override
{
if (specialChar->get_Font()->get_Hidden())
{
specialChar->Remove();
}
}
VisitorAction VisitTableEnd(SharedPtr<Table> table) override
{
// The content inside table cells may have the hidden content flag, but the tables themselves cannot.
// If this table had nothing but hidden content, this visitor would have removed all of it,
// and there would be no child nodes left.
// Thus, we can also treat the table itself as hidden content and remove it.
// Tables which are empty but do not have hidden content will have cells with empty paragraphs inside,
// which this visitor will not remove.
if (!table->get_HasChildNodes())
{
table->Remove();
}
}
VisitorAction VisitCellEnd(SharedPtr<Cell> cell) override
{
if (!cell->get_HasChildNodes() && cell->get_ParentNode() != nullptr)
{
cell->Remove();
}
}
VisitorAction VisitRowEnd(SharedPtr<Row> row) override
{
if (!row->get_HasChildNodes() && row->get_ParentNode() != nullptr)
{
row->Remove();
}
}
};

Implements Aspose::Words::Node.

◆ AutoFit()

void Aspose::Words::Tables::Table::AutoFit ( Aspose::Words::Tables::AutoFitBehavior  behavior)

Resizes the table and cells according to the specified auto fit behavior.

This method mimics the commands available in the Auto Fit menu for a table in Microsoft Word. The commands available are "Auto Fit to Contents", "Auto Fit to Window" and "Fixed Column Width". In Microsoft Word these commands set relevant table properties and then update the table layout and Aspose.Words does the same for you.

Parameters
behaviorSpecifies how to auto fit the table.
Examples

Shows how to build a new table while applying a style.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
// We must insert at least one row before setting any table formatting.
builder->InsertCell();
// Set the table style used based on the style identifier.
// Note that not all table styles are available when saving to .doc format.
table->set_StyleIdentifier(StyleIdentifier::MediumShading1Accent1);
// Partially apply the style to features of the table based on predicates, then build the table.
builder->Writeln(u"Item");
builder->get_CellFormat()->set_RightPadding(40);
builder->InsertCell();
builder->Writeln(u"Quantity (kg)");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Apples");
builder->InsertCell();
builder->Writeln(u"20");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Bananas");
builder->InsertCell();
builder->Writeln(u"40");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Carrots");
builder->InsertCell();
builder->Writeln(u"50");
builder->EndRow();
doc->Save(ArtifactsDir + u"DocumentBuilder.InsertTableWithStyle.docx");

◆ ClearBorders()

void Aspose::Words::Tables::Table::ClearBorders ( )

Removes all table and cell borders on this table.

Examples

Shows how to apply an outline border to a table.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
// Align the table to the center of the page.
table->set_Alignment(TableAlignment::Center);
// Clear any existing borders and shading from the table.
table->ClearBorders();
table->ClearShading();
// Add green borders to the outline of the table.
// Fill the cells with a light green solid color.
doc->Save(ArtifactsDir + u"Table.SetOutlineBorders.docx");

Shows how to remove all borders from a table.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Hello world!");
builder->EndTable();
// Modify the color and thickness of the top border.
SharedPtr<Border> topBorder = table->get_FirstRow()->get_RowFormat()->get_Borders()->idx_get(BorderType::Top);
ASPOSE_ASSERT_EQ(1.5, topBorder->get_LineWidth());
ASSERT_EQ(System::Drawing::Color::get_Red().ToArgb(), topBorder->get_Color().ToArgb());
ASSERT_EQ(LineStyle::Double, topBorder->get_LineStyle());
// Clear the borders of all cells in the table, and then save the document.
table->ClearBorders();
doc->Save(ArtifactsDir + u"Table.ClearBorders.docx");
// Verify the values of the table's properties after re-opening the document.
doc = MakeObject<Document>(ArtifactsDir + u"Table.ClearBorders.docx");
table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
topBorder = table->get_FirstRow()->get_RowFormat()->get_Borders()->idx_get(BorderType::Top);
ASPOSE_ASSERT_EQ(0.0, topBorder->get_LineWidth());
ASSERT_EQ(System::Drawing::Color::Empty.ToArgb(), topBorder->get_Color().ToArgb());
ASSERT_EQ(LineStyle::None, topBorder->get_LineStyle());

◆ ClearShading()

void Aspose::Words::Tables::Table::ClearShading ( )

Removes all shading on the table.

Examples

Shows how to apply an outline border to a table.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
// Align the table to the center of the page.
table->set_Alignment(TableAlignment::Center);
// Clear any existing borders and shading from the table.
table->ClearBorders();
table->ClearShading();
// Add green borders to the outline of the table.
// Fill the cells with a light green solid color.
doc->Save(ArtifactsDir + u"Table.SetOutlineBorders.docx");

◆ ConvertToHorizontallyMergedCells()

void Aspose::Words::Tables::Table::ConvertToHorizontallyMergedCells ( )

Converts cells horizontally merged by width to cells merged by HorizontalMerge.

Examples

Shows how to convert cells horizontally merged by width to cells merged by CellFormat.HorizontalMerge.

auto doc = MakeObject<Document>(MyDir + u"Table with merged cells.docx");
// Microsoft Word does not write merge flags anymore, defining merged cells by width instead.
// Aspose.Words by default define only 5 cells in a row, and none of them have the horizontal merge flag,
// even though there were 7 cells in the row before the horizontal merging took place.
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
SharedPtr<Row> row = table->get_Rows()->idx_get(0);
ASSERT_EQ(5, row->get_Cells()->get_Count());
auto horizontalMergeIsNone = [](SharedPtr<Node> c)
{
return (System::DynamicCast<Cell>(c))->get_CellFormat()->get_HorizontalMerge() == CellMerge::None;
};
ASSERT_TRUE(row->get_Cells()->LINQ_All(horizontalMergeIsNone));
// Use the "ConvertToHorizontallyMergedCells" method to convert cells horizontally merged
// by its width to the cell horizontally merged by flags.
// Now, we have 7 cells, and some of them have horizontal merge values.
table->ConvertToHorizontallyMergedCells();
row = table->get_Rows()->idx_get(0);
ASSERT_EQ(7, row->get_Cells()->get_Count());
ASSERT_EQ(CellMerge::None, row->get_Cells()->idx_get(0)->get_CellFormat()->get_HorizontalMerge());
ASSERT_EQ(CellMerge::First, row->get_Cells()->idx_get(1)->get_CellFormat()->get_HorizontalMerge());
ASSERT_EQ(CellMerge::Previous, row->get_Cells()->idx_get(2)->get_CellFormat()->get_HorizontalMerge());
ASSERT_EQ(CellMerge::None, row->get_Cells()->idx_get(3)->get_CellFormat()->get_HorizontalMerge());
ASSERT_EQ(CellMerge::First, row->get_Cells()->idx_get(4)->get_CellFormat()->get_HorizontalMerge());
ASSERT_EQ(CellMerge::Previous, row->get_Cells()->idx_get(5)->get_CellFormat()->get_HorizontalMerge());
ASSERT_EQ(CellMerge::None, row->get_Cells()->idx_get(6)->get_CellFormat()->get_HorizontalMerge());

◆ EnsureMinimum()

void Aspose::Words::Tables::Table::EnsureMinimum ( )

If the table has no rows, creates and appends one Row.

Examples

Shows how to ensure that a table node contains the nodes we need to add content.

auto doc = MakeObject<Document>();
auto table = MakeObject<Table>(doc);
doc->get_FirstSection()->get_Body()->AppendChild(table);
// Tables contain rows, which contain cells, which may contain paragraphs
// with typical elements such as runs, shapes, and even other tables.
// Our new table has none of these nodes, and we cannot add contents to it until it does.
ASSERT_EQ(0, table->GetChildNodes(NodeType::Any, true)->get_Count());
// Calling the "EnsureMinimum" method on a table will ensure that
// the table has at least one row and one cell with an empty paragraph.
table->EnsureMinimum();
table->get_FirstRow()->get_FirstCell()->get_FirstParagraph()->AppendChild(MakeObject<Run>(doc, u"Hello world!"));

◆ get_AbsoluteHorizontalDistance()

double Aspose::Words::Tables::Table::get_AbsoluteHorizontalDistance ( )

Gets or sets absolute horizontal floating table position specified by the table properties, in points. Default value is 0.

Examples

Shows how set the location of floating tables.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Table 1, cell 1");
builder->EndTable();
table->set_PreferredWidth(PreferredWidth::FromPoints(300));
// Set the table's location to a place on the page, such as, in this case, the bottom right corner.
table->set_RelativeVerticalAlignment(VerticalAlignment::Bottom);
table->set_RelativeHorizontalAlignment(HorizontalAlignment::Right);
table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Table 2, cell 1");
builder->EndTable();
table->set_PreferredWidth(PreferredWidth::FromPoints(300));
// We can also set a horizontal and vertical offset in points from the paragraph's location where we inserted the table.
table->set_AbsoluteVerticalDistance(50);
table->set_AbsoluteHorizontalDistance(100);
doc->Save(ArtifactsDir + u"Table.ChangeFloatingTableProperties.docx");

◆ get_AbsoluteVerticalDistance()

double Aspose::Words::Tables::Table::get_AbsoluteVerticalDistance ( )

Gets or sets absolute vertical floating table position specified by the table properties, in points. Default value is 0.

Examples

Shows how set the location of floating tables.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Table 1, cell 1");
builder->EndTable();
table->set_PreferredWidth(PreferredWidth::FromPoints(300));
// Set the table's location to a place on the page, such as, in this case, the bottom right corner.
table->set_RelativeVerticalAlignment(VerticalAlignment::Bottom);
table->set_RelativeHorizontalAlignment(HorizontalAlignment::Right);
table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Table 2, cell 1");
builder->EndTable();
table->set_PreferredWidth(PreferredWidth::FromPoints(300));
// We can also set a horizontal and vertical offset in points from the paragraph's location where we inserted the table.
table->set_AbsoluteVerticalDistance(50);
table->set_AbsoluteHorizontalDistance(100);
doc->Save(ArtifactsDir + u"Table.ChangeFloatingTableProperties.docx");

◆ get_Alignment()

Aspose::Words::Tables::TableAlignment Aspose::Words::Tables::Table::get_Alignment ( )

Specifies how an inline table is aligned in the document.

The default value is Left.

Examples

Shows how to apply an outline border to a table.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
// Align the table to the center of the page.
table->set_Alignment(TableAlignment::Center);
// Clear any existing borders and shading from the table.
table->ClearBorders();
table->ClearShading();
// Add green borders to the outline of the table.
// Fill the cells with a light green solid color.
doc->Save(ArtifactsDir + u"Table.SetOutlineBorders.docx");

◆ get_AllowAutoFit()

bool Aspose::Words::Tables::Table::get_AllowAutoFit ( )

Allows Microsoft Word and Aspose.Words to automatically resize cells in a table to fit their contents.

The default value is true.

See also
Aspose::Words::Tables::Table::AutoFit(Aspose::Words::Tables::AutoFitBehavior)
Examples

Shows how to enable/disable automatic table cell resizing.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->get_CellFormat()->set_PreferredWidth(PreferredWidth::FromPoints(100));
builder->Write(String(u"Lorem ipsum dolor sit amet, consectetur adipiscing elit, ") +
u"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
builder->InsertCell();
builder->get_CellFormat()->set_PreferredWidth(PreferredWidth::Auto());
builder->Write(String(u"Lorem ipsum dolor sit amet, consectetur adipiscing elit, ") +
u"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
builder->EndRow();
builder->EndTable();
// Set the "AllowAutoFit" property to "false" to get the table to maintain the dimensions
// of all its rows and cells, and truncate contents if they get too large to fit.
// Set the "AllowAutoFit" property to "true" to allow the table to change its cells' width and height
// to accommodate their contents.
table->set_AllowAutoFit(allowAutoFit);
doc->Save(ArtifactsDir + u"Table.AllowAutoFitOnTable.html");

◆ get_AllowCellSpacing()

bool Aspose::Words::Tables::Table::get_AllowCellSpacing ( )

Gets or sets the "Allow spacing between cells" option.

Examples

Shows how to enable spacing between individual cells in a table.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Animal");
builder->InsertCell();
builder->Write(u"Class");
builder->EndRow();
builder->InsertCell();
builder->Write(u"Dog");
builder->InsertCell();
builder->Write(u"Mammal");
builder->EndTable();
table->set_CellSpacing(3);
// Set the "AllowCellSpacing" property to "true" to enable spacing between cells
// with a magnitude equal to the value of the "CellSpacing" property, in points.
// Set the "AllowCellSpacing" property to "false" to disable cell spacing
// and ignore the value of the "CellSpacing" property.
table->set_AllowCellSpacing(allowCellSpacing);
doc->Save(ArtifactsDir + u"Table.AllowCellSpacing.html");
// Adjusting the "CellSpacing" property will automatically enable cell spacing.
table->set_CellSpacing(5);
ASSERT_TRUE(table->get_AllowCellSpacing());

◆ get_AllowOverlap()

bool Aspose::Words::Tables::Table::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.

Examples

Shows how to work with floating tables properties.

auto doc = MakeObject<Document>(MyDir + u"Table wrapped by text.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
if (table->get_TextWrapping() == TextWrapping::Around)
{
ASSERT_EQ(RelativeHorizontalPosition::Margin, table->get_HorizontalAnchor());
ASSERT_EQ(RelativeVerticalPosition::Paragraph, table->get_VerticalAnchor());
ASPOSE_ASSERT_EQ(false, table->get_AllowOverlap());
// Only Margin, Page, Column available in RelativeHorizontalPosition for HorizontalAnchor setter.
// The ArgumentException will be thrown for any other values.
table->set_HorizontalAnchor(RelativeHorizontalPosition::Column);
// Only Margin, Page, Paragraph available in RelativeVerticalPosition for VerticalAnchor setter.
// The ArgumentException will be thrown for any other values.
table->set_VerticalAnchor(RelativeVerticalPosition::Page);
}

◆ get_Bidi()

bool Aspose::Words::Tables::Table::get_Bidi ( )

Gets or sets whether this is a right-to-left table.

When true, the cells in this row are laid out right to left.

The default value is false.

Examples

Shows how to create custom style settings for the table.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Name");
builder->InsertCell();
builder->Write(u"مرحبًا");
builder->EndRow();
builder->InsertCell();
builder->InsertCell();
builder->EndTable();
auto tableStyle = System::DynamicCast<TableStyle>(doc->get_Styles()->Add(StyleType::Table, u"MyTableStyle1"));
tableStyle->set_AllowBreakAcrossPages(true);
tableStyle->set_Bidi(true);
tableStyle->set_CellSpacing(5);
tableStyle->set_BottomPadding(20);
tableStyle->set_LeftPadding(5);
tableStyle->set_RightPadding(10);
tableStyle->set_TopPadding(20);
tableStyle->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_AntiqueWhite());
tableStyle->get_Borders()->set_Color(System::Drawing::Color::get_Blue());
tableStyle->get_Borders()->set_LineStyle(LineStyle::DotDash);
tableStyle->set_VerticalAlignment(CellVerticalAlignment::Center);
table->set_Style(tableStyle);
// Setting the style properties of a table may affect the properties of the table itself.
ASSERT_TRUE(table->get_Bidi());
ASPOSE_ASSERT_EQ(5.0, table->get_CellSpacing());
ASSERT_EQ(u"MyTableStyle1", table->get_StyleName());
doc->Save(ArtifactsDir + u"Table.TableStyleCreation.docx");

◆ get_BottomPadding()

double Aspose::Words::Tables::Table::get_BottomPadding ( )

Gets or sets the amount of space (in points) to add below the contents of cells.

Examples

Shows how to configure content padding in a table.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Row 1, cell 1.");
builder->InsertCell();
builder->Write(u"Row 1, cell 2.");
builder->EndTable();
// For every cell in the table, set the distance between its contents and each of its borders.
// This table will maintain the minimum padding distance by wrapping text.
table->set_LeftPadding(30);
table->set_RightPadding(60);
table->set_TopPadding(10);
table->set_BottomPadding(90);
table->set_PreferredWidth(PreferredWidth::FromPoints(250));
doc->Save(ArtifactsDir + u"DocumentBuilder.SetRowFormatting.docx");

◆ get_CellSpacing()

double Aspose::Words::Tables::Table::get_CellSpacing ( )

Gets or sets the amount of space (in points) between the cells.

Examples

Shows how to enable spacing between individual cells in a table.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Animal");
builder->InsertCell();
builder->Write(u"Class");
builder->EndRow();
builder->InsertCell();
builder->Write(u"Dog");
builder->InsertCell();
builder->Write(u"Mammal");
builder->EndTable();
table->set_CellSpacing(3);
// Set the "AllowCellSpacing" property to "true" to enable spacing between cells
// with a magnitude equal to the value of the "CellSpacing" property, in points.
// Set the "AllowCellSpacing" property to "false" to disable cell spacing
// and ignore the value of the "CellSpacing" property.
table->set_AllowCellSpacing(allowCellSpacing);
doc->Save(ArtifactsDir + u"Table.AllowCellSpacing.html");
// Adjusting the "CellSpacing" property will automatically enable cell spacing.
table->set_CellSpacing(5);
ASSERT_TRUE(table->get_AllowCellSpacing());

Shows how to create custom style settings for the table.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Name");
builder->InsertCell();
builder->Write(u"مرحبًا");
builder->EndRow();
builder->InsertCell();
builder->InsertCell();
builder->EndTable();
auto tableStyle = System::DynamicCast<TableStyle>(doc->get_Styles()->Add(StyleType::Table, u"MyTableStyle1"));
tableStyle->set_AllowBreakAcrossPages(true);
tableStyle->set_Bidi(true);
tableStyle->set_CellSpacing(5);
tableStyle->set_BottomPadding(20);
tableStyle->set_LeftPadding(5);
tableStyle->set_RightPadding(10);
tableStyle->set_TopPadding(20);
tableStyle->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_AntiqueWhite());
tableStyle->get_Borders()->set_Color(System::Drawing::Color::get_Blue());
tableStyle->get_Borders()->set_LineStyle(LineStyle::DotDash);
tableStyle->set_VerticalAlignment(CellVerticalAlignment::Center);
table->set_Style(tableStyle);
// Setting the style properties of a table may affect the properties of the table itself.
ASSERT_TRUE(table->get_Bidi());
ASPOSE_ASSERT_EQ(5.0, table->get_CellSpacing());
ASSERT_EQ(u"MyTableStyle1", table->get_StyleName());
doc->Save(ArtifactsDir + u"Table.TableStyleCreation.docx");

◆ get_Description()

System::String Aspose::Words::Tables::Table::get_Description ( )

Gets or sets description of this table. It provides an alternative text representation of the information contained in the table.

The default value is an empty string.

This property is meaningful for ISO/IEC 29500 compliant DOCX documents (OoxmlCompliance). When saved to pre-ISO/IEC 29500 formats, the property is ignored.

Examples

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;
}

◆ get_DistanceBottom()

double Aspose::Words::Tables::Table::get_DistanceBottom ( )

Gets distance between table bottom and the surrounding text, in points.

Examples

Shows the minimum distance operations between table boundaries and text.

auto doc = MakeObject<Document>(MyDir + u"Table wrapped by text.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
ASPOSE_ASSERT_EQ(25.9, table->get_DistanceTop());
ASPOSE_ASSERT_EQ(25.9, table->get_DistanceBottom());
ASPOSE_ASSERT_EQ(17.3, table->get_DistanceLeft());
ASPOSE_ASSERT_EQ(17.3, table->get_DistanceRight());

◆ get_DistanceLeft()

double Aspose::Words::Tables::Table::get_DistanceLeft ( )

Gets distance between table left and the surrounding text, in points.

Examples

Shows the minimum distance operations between table boundaries and text.

auto doc = MakeObject<Document>(MyDir + u"Table wrapped by text.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
ASPOSE_ASSERT_EQ(25.9, table->get_DistanceTop());
ASPOSE_ASSERT_EQ(25.9, table->get_DistanceBottom());
ASPOSE_ASSERT_EQ(17.3, table->get_DistanceLeft());
ASPOSE_ASSERT_EQ(17.3, table->get_DistanceRight());

◆ get_DistanceRight()

double Aspose::Words::Tables::Table::get_DistanceRight ( )

Gets distance between table right and the surrounding text, in points.

Examples

Shows the minimum distance operations between table boundaries and text.

auto doc = MakeObject<Document>(MyDir + u"Table wrapped by text.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
ASPOSE_ASSERT_EQ(25.9, table->get_DistanceTop());
ASPOSE_ASSERT_EQ(25.9, table->get_DistanceBottom());
ASPOSE_ASSERT_EQ(17.3, table->get_DistanceLeft());
ASPOSE_ASSERT_EQ(17.3, table->get_DistanceRight());

◆ get_DistanceTop()

double Aspose::Words::Tables::Table::get_DistanceTop ( )

Gets distance between table top and the surrounding text, in points.

Examples

Shows the minimum distance operations between table boundaries and text.

auto doc = MakeObject<Document>(MyDir + u"Table wrapped by text.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
ASPOSE_ASSERT_EQ(25.9, table->get_DistanceTop());
ASPOSE_ASSERT_EQ(25.9, table->get_DistanceBottom());
ASPOSE_ASSERT_EQ(17.3, table->get_DistanceLeft());
ASPOSE_ASSERT_EQ(17.3, table->get_DistanceRight());

◆ get_FirstRow()

System::SharedPtr<Aspose::Words::Tables::Row> Aspose::Words::Tables::Table::get_FirstRow ( )

Returns the first Row node in the table.

Examples

Shows how to remove the first and last rows of all tables in a document.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
SharedPtr<TableCollection> tables = doc->get_FirstSection()->get_Body()->get_Tables();
ASSERT_EQ(5, tables->idx_get(0)->get_Rows()->get_Count());
ASSERT_EQ(4, tables->idx_get(1)->get_Rows()->get_Count());
for (const auto& table : System::IterateOver(tables->LINQ_OfType<SharedPtr<Table>>()))
{
if (table->get_FirstRow() != nullptr)
{
table->get_FirstRow()->Remove();
}
if (table->get_LastRow() != nullptr)
{
table->get_LastRow()->Remove();
}
}
ASSERT_EQ(3, tables->idx_get(0)->get_Rows()->get_Count());
ASSERT_EQ(2, tables->idx_get(1)->get_Rows()->get_Count());

Shows how to combine the rows from two tables into one.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
// Below are two ways of getting a table from a document.
// 1 - From the "Tables" collection of a Body node:
SharedPtr<Table> firstTable = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
// 2 - Using the "GetChild" method:
auto secondTable = System::DynamicCast<Table>(doc->GetChild(NodeType::Table, 1, true));
// Append all rows from the current table to the next.
while (secondTable->get_HasChildNodes())
{
firstTable->get_Rows()->Add(secondTable->get_FirstRow());
}
// Remove the empty table container.
secondTable->Remove();
doc->Save(ArtifactsDir + u"Table.CombineTables.docx");

◆ get_HorizontalAnchor()

Aspose::Words::Drawing::RelativeHorizontalPosition Aspose::Words::Tables::Table::get_HorizontalAnchor ( )

Gets the base object from which the horizontal positioning of floating table should be calculated. Default value is Column.

Examples

Shows how to work with floating tables properties.

auto doc = MakeObject<Document>(MyDir + u"Table wrapped by text.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
if (table->get_TextWrapping() == TextWrapping::Around)
{
ASSERT_EQ(RelativeHorizontalPosition::Margin, table->get_HorizontalAnchor());
ASSERT_EQ(RelativeVerticalPosition::Paragraph, table->get_VerticalAnchor());
ASPOSE_ASSERT_EQ(false, table->get_AllowOverlap());
// Only Margin, Page, Column available in RelativeHorizontalPosition for HorizontalAnchor setter.
// The ArgumentException will be thrown for any other values.
table->set_HorizontalAnchor(RelativeHorizontalPosition::Column);
// Only Margin, Page, Paragraph available in RelativeVerticalPosition for VerticalAnchor setter.
// The ArgumentException will be thrown for any other values.
table->set_VerticalAnchor(RelativeVerticalPosition::Page);
}

◆ get_LastRow()

System::SharedPtr<Aspose::Words::Tables::Row> Aspose::Words::Tables::Table::get_LastRow ( )

Returns the last Row node in the table.

Examples

Shows how to remove the first and last rows of all tables in a document.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
SharedPtr<TableCollection> tables = doc->get_FirstSection()->get_Body()->get_Tables();
ASSERT_EQ(5, tables->idx_get(0)->get_Rows()->get_Count());
ASSERT_EQ(4, tables->idx_get(1)->get_Rows()->get_Count());
for (const auto& table : System::IterateOver(tables->LINQ_OfType<SharedPtr<Table>>()))
{
if (table->get_FirstRow() != nullptr)
{
table->get_FirstRow()->Remove();
}
if (table->get_LastRow() != nullptr)
{
table->get_LastRow()->Remove();
}
}
ASSERT_EQ(3, tables->idx_get(0)->get_Rows()->get_Count());
ASSERT_EQ(2, tables->idx_get(1)->get_Rows()->get_Count());

◆ get_LeftIndent()

double Aspose::Words::Tables::Table::get_LeftIndent ( )

Gets or sets the value that represents the left indent of the table.

Examples

Shows how to create a formatted table using DocumentBuilder.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
table->set_LeftIndent(20);
// Set some formatting options for text and table appearance.
builder->get_RowFormat()->set_Height(40);
builder->get_RowFormat()->set_HeightRule(HeightRule::AtLeast);
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::FromArgb(198, 217, 241));
builder->get_ParagraphFormat()->set_Alignment(ParagraphAlignment::Center);
builder->get_Font()->set_Size(16);
builder->get_Font()->set_Name(u"Arial");
builder->get_Font()->set_Bold(true);
// Configuring the formatting options in a document builder will apply them
// to the current cell/row its cursor is in,
// as well as any new cells and rows created using that builder.
builder->Write(u"Header Row,\n Cell 1");
builder->InsertCell();
builder->Write(u"Header Row,\n Cell 2");
builder->InsertCell();
builder->Write(u"Header Row,\n Cell 3");
builder->EndRow();
// Reconfigure the builder's formatting objects for new rows and cells that we are about to make.
// The builder will not apply these to the first row already created so that it will stand out as a header row.
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_White());
builder->get_CellFormat()->set_VerticalAlignment(CellVerticalAlignment::Center);
builder->get_RowFormat()->set_Height(30);
builder->get_RowFormat()->set_HeightRule(HeightRule::Auto);
builder->InsertCell();
builder->get_Font()->set_Size(12);
builder->get_Font()->set_Bold(false);
builder->Write(u"Row 1, Cell 1.");
builder->InsertCell();
builder->Write(u"Row 1, Cell 2.");
builder->InsertCell();
builder->Write(u"Row 1, Cell 3.");
builder->EndRow();
builder->InsertCell();
builder->Write(u"Row 2, Cell 1.");
builder->InsertCell();
builder->Write(u"Row 2, Cell 2.");
builder->InsertCell();
builder->Write(u"Row 2, Cell 3.");
builder->EndRow();
builder->EndTable();
doc->Save(ArtifactsDir + u"DocumentBuilder.CreateFormattedTable.docx");

◆ get_LeftPadding()

double Aspose::Words::Tables::Table::get_LeftPadding ( )

Gets or sets the amount of space (in points) to add to the left of the contents of cells.

Examples

Shows how to configure content padding in a table.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Row 1, cell 1.");
builder->InsertCell();
builder->Write(u"Row 1, cell 2.");
builder->EndTable();
// For every cell in the table, set the distance between its contents and each of its borders.
// This table will maintain the minimum padding distance by wrapping text.
table->set_LeftPadding(30);
table->set_RightPadding(60);
table->set_TopPadding(10);
table->set_BottomPadding(90);
table->set_PreferredWidth(PreferredWidth::FromPoints(250));
doc->Save(ArtifactsDir + u"DocumentBuilder.SetRowFormatting.docx");

◆ get_NodeType()

Aspose::Words::NodeType Aspose::Words::Tables::Table::get_NodeType ( ) const
overridevirtual

Returns NodeType.Table.

Examples

Shows how to traverse a composite node's tree of child nodes.

void RecurseChildren()
{
auto doc = MakeObject<Document>(MyDir + u"Paragraphs.docx");
// Any node that can contain child nodes, such as the document itself, is composite.
ASSERT_TRUE(doc->get_IsComposite());
// Invoke the recursive function that will go through and print all the child nodes of a composite node.
TraverseAllNodes(doc, 0);
}
void TraverseAllNodes(SharedPtr<CompositeNode> parentNode, int depth)
{
for (SharedPtr<Node> childNode = parentNode->get_FirstChild(); childNode != nullptr; childNode = childNode->get_NextSibling())
{
std::cout << (String(u'\t', depth)) << Node::NodeTypeToString(childNode->get_NodeType());
// Recurse into the node if it is a composite node. Otherwise, print its contents if it is an inline node.
if (childNode->get_IsComposite())
{
std::cout << std::endl;
TraverseAllNodes(System::DynamicCast<CompositeNode>(childNode), depth + 1);
}
else if (System::ObjectExt::Is<Inline>(childNode))
{
std::cout << " - \"" << childNode->GetText().Trim() << "\"" << std::endl;
}
else
{
std::cout << std::endl;
}
}
}

Shows how to find out if a tables are nested.

void CalculateDepthOfNestedTables()
{
auto doc = MakeObject<Document>(MyDir + u"Nested tables.docx");
SharedPtr<NodeCollection> tables = doc->GetChildNodes(NodeType::Table, true);
for (int i = 0; i < tables->get_Count(); i++)
{
auto table = System::DynamicCast<Table>(tables->idx_get(i));
// Find out if any cells in the table have other tables as children.
int count = GetChildTableCount(table);
std::cout << "Table #" << i << " has " << count << " tables directly within its cells" << std::endl;
// Find out if the table is nested inside another table, and, if so, at what depth.
int tableDepth = GetNestedDepthOfTable(table);
if (tableDepth > 0)
{
std::cout << "Table #" << i << " is nested inside another table at depth of " << tableDepth << std::endl;
}
else
{
std::cout << "Table #" << i << " is a non nested table (is not a child of another table)" << std::endl;
}
}
}
static int GetNestedDepthOfTable(SharedPtr<Table> table)
{
int depth = 0;
SharedPtr<Node> parent = table->GetAncestor(table->get_NodeType());
while (parent != nullptr)
{
depth++;
parent = parent->GetAncestorOf<SharedPtr<Table>>();
}
return depth;
}
static int GetChildTableCount(SharedPtr<Table> table)
{
int childTableCount = 0;
for (const auto& row : System::IterateOver(table->get_Rows()->LINQ_OfType<SharedPtr<Row>>()))
{
for (const auto& Cell : System::IterateOver(row->get_Cells()->LINQ_OfType<SharedPtr<Cell>>()))
{
SharedPtr<TableCollection> childTables = Cell->get_Tables();
if (childTables->get_Count() > 0)
{
childTableCount++;
}
}
}
return childTableCount;
}

Implements Aspose::Words::Node.

◆ get_PreferredWidth()

System::SharedPtr<Aspose::Words::Tables::PreferredWidth> Aspose::Words::Tables::Table::get_PreferredWidth ( )

Gets or sets the table preferred width.

The default value is Auto.

Examples

Shows how to set a table to auto fit to 50% of the width of the page.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Cell #1");
builder->InsertCell();
builder->Write(u"Cell #2");
builder->InsertCell();
builder->Write(u"Cell #3");
table->set_PreferredWidth(PreferredWidth::FromPercent(50));
doc->Save(ArtifactsDir + u"DocumentBuilder.InsertTableWithPreferredWidth.docx");

◆ get_RelativeHorizontalAlignment()

Aspose::Words::Drawing::HorizontalAlignment Aspose::Words::Tables::Table::get_RelativeHorizontalAlignment ( )

Gets or sets floating table relative horizontal alignment.

Examples

Shows how set the location of floating tables.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Table 1, cell 1");
builder->EndTable();
table->set_PreferredWidth(PreferredWidth::FromPoints(300));
// Set the table's location to a place on the page, such as, in this case, the bottom right corner.
table->set_RelativeVerticalAlignment(VerticalAlignment::Bottom);
table->set_RelativeHorizontalAlignment(HorizontalAlignment::Right);
table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Table 2, cell 1");
builder->EndTable();
table->set_PreferredWidth(PreferredWidth::FromPoints(300));
// We can also set a horizontal and vertical offset in points from the paragraph's location where we inserted the table.
table->set_AbsoluteVerticalDistance(50);
table->set_AbsoluteHorizontalDistance(100);
doc->Save(ArtifactsDir + u"Table.ChangeFloatingTableProperties.docx");

◆ get_RelativeVerticalAlignment()

Aspose::Words::Drawing::VerticalAlignment Aspose::Words::Tables::Table::get_RelativeVerticalAlignment ( )

Gets or sets floating table relative vertical alignment.

Examples

Shows how set the location of floating tables.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Table 1, cell 1");
builder->EndTable();
table->set_PreferredWidth(PreferredWidth::FromPoints(300));
// Set the table's location to a place on the page, such as, in this case, the bottom right corner.
table->set_RelativeVerticalAlignment(VerticalAlignment::Bottom);
table->set_RelativeHorizontalAlignment(HorizontalAlignment::Right);
table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Table 2, cell 1");
builder->EndTable();
table->set_PreferredWidth(PreferredWidth::FromPoints(300));
// We can also set a horizontal and vertical offset in points from the paragraph's location where we inserted the table.
table->set_AbsoluteVerticalDistance(50);
table->set_AbsoluteHorizontalDistance(100);
doc->Save(ArtifactsDir + u"Table.ChangeFloatingTableProperties.docx");

◆ get_RightPadding()

double Aspose::Words::Tables::Table::get_RightPadding ( )

Gets or sets the amount of space (in points) to add to the right of the contents of cells.

Examples

Shows how to configure content padding in a table.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Row 1, cell 1.");
builder->InsertCell();
builder->Write(u"Row 1, cell 2.");
builder->EndTable();
// For every cell in the table, set the distance between its contents and each of its borders.
// This table will maintain the minimum padding distance by wrapping text.
table->set_LeftPadding(30);
table->set_RightPadding(60);
table->set_TopPadding(10);
table->set_BottomPadding(90);
table->set_PreferredWidth(PreferredWidth::FromPoints(250));
doc->Save(ArtifactsDir + u"DocumentBuilder.SetRowFormatting.docx");

◆ get_Rows()

System::SharedPtr<Aspose::Words::Tables::RowCollection> Aspose::Words::Tables::Table::get_Rows ( )

Provides typed access to the rows of the table.

Examples

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 combine the rows from two tables into one.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
// Below are two ways of getting a table from a document.
// 1 - From the "Tables" collection of a Body node:
SharedPtr<Table> firstTable = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
// 2 - Using the "GetChild" method:
auto secondTable = System::DynamicCast<Table>(doc->GetChild(NodeType::Table, 1, true));
// Append all rows from the current table to the next.
while (secondTable->get_HasChildNodes())
{
firstTable->get_Rows()->Add(secondTable->get_FirstRow());
}
// Remove the empty table container.
secondTable->Remove();
doc->Save(ArtifactsDir + u"Table.CombineTables.docx");

◆ get_Style()

System::SharedPtr<Aspose::Words::Style> Aspose::Words::Tables::Table::get_Style ( )

Gets or sets the table style applied to this table.

Examples

Shows how to create custom style settings for the table.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Name");
builder->InsertCell();
builder->Write(u"مرحبًا");
builder->EndRow();
builder->InsertCell();
builder->InsertCell();
builder->EndTable();
auto tableStyle = System::DynamicCast<TableStyle>(doc->get_Styles()->Add(StyleType::Table, u"MyTableStyle1"));
tableStyle->set_AllowBreakAcrossPages(true);
tableStyle->set_Bidi(true);
tableStyle->set_CellSpacing(5);
tableStyle->set_BottomPadding(20);
tableStyle->set_LeftPadding(5);
tableStyle->set_RightPadding(10);
tableStyle->set_TopPadding(20);
tableStyle->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_AntiqueWhite());
tableStyle->get_Borders()->set_Color(System::Drawing::Color::get_Blue());
tableStyle->get_Borders()->set_LineStyle(LineStyle::DotDash);
tableStyle->set_VerticalAlignment(CellVerticalAlignment::Center);
table->set_Style(tableStyle);
// Setting the style properties of a table may affect the properties of the table itself.
ASSERT_TRUE(table->get_Bidi());
ASPOSE_ASSERT_EQ(5.0, table->get_CellSpacing());
ASSERT_EQ(u"MyTableStyle1", table->get_StyleName());
doc->Save(ArtifactsDir + u"Table.TableStyleCreation.docx");

◆ get_StyleIdentifier()

Aspose::Words::StyleIdentifier Aspose::Words::Tables::Table::get_StyleIdentifier ( )

Gets or sets the locale independent style identifier of the table style applied to this table.

Examples

Shows how to build a new table while applying a style.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
// We must insert at least one row before setting any table formatting.
builder->InsertCell();
// Set the table style used based on the style identifier.
// Note that not all table styles are available when saving to .doc format.
table->set_StyleIdentifier(StyleIdentifier::MediumShading1Accent1);
// Partially apply the style to features of the table based on predicates, then build the table.
builder->Writeln(u"Item");
builder->get_CellFormat()->set_RightPadding(40);
builder->InsertCell();
builder->Writeln(u"Quantity (kg)");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Apples");
builder->InsertCell();
builder->Writeln(u"20");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Bananas");
builder->InsertCell();
builder->Writeln(u"40");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Carrots");
builder->InsertCell();
builder->Writeln(u"50");
builder->EndRow();
doc->Save(ArtifactsDir + u"DocumentBuilder.InsertTableWithStyle.docx");

◆ get_StyleName()

System::String Aspose::Words::Tables::Table::get_StyleName ( )

Gets or sets the name of the table style applied to this table.

Examples

Shows how to create custom style settings for the table.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Name");
builder->InsertCell();
builder->Write(u"مرحبًا");
builder->EndRow();
builder->InsertCell();
builder->InsertCell();
builder->EndTable();
auto tableStyle = System::DynamicCast<TableStyle>(doc->get_Styles()->Add(StyleType::Table, u"MyTableStyle1"));
tableStyle->set_AllowBreakAcrossPages(true);
tableStyle->set_Bidi(true);
tableStyle->set_CellSpacing(5);
tableStyle->set_BottomPadding(20);
tableStyle->set_LeftPadding(5);
tableStyle->set_RightPadding(10);
tableStyle->set_TopPadding(20);
tableStyle->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_AntiqueWhite());
tableStyle->get_Borders()->set_Color(System::Drawing::Color::get_Blue());
tableStyle->get_Borders()->set_LineStyle(LineStyle::DotDash);
tableStyle->set_VerticalAlignment(CellVerticalAlignment::Center);
table->set_Style(tableStyle);
// Setting the style properties of a table may affect the properties of the table itself.
ASSERT_TRUE(table->get_Bidi());
ASPOSE_ASSERT_EQ(5.0, table->get_CellSpacing());
ASSERT_EQ(u"MyTableStyle1", table->get_StyleName());
doc->Save(ArtifactsDir + u"Table.TableStyleCreation.docx");

◆ get_StyleOptions()

Aspose::Words::Tables::TableStyleOptions Aspose::Words::Tables::Table::get_StyleOptions ( )

Gets or sets bit flags that specify how a table style is applied to this table.

Examples

Shows how to build a new table while applying a style.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
// We must insert at least one row before setting any table formatting.
builder->InsertCell();
// Set the table style used based on the style identifier.
// Note that not all table styles are available when saving to .doc format.
table->set_StyleIdentifier(StyleIdentifier::MediumShading1Accent1);
// Partially apply the style to features of the table based on predicates, then build the table.
builder->Writeln(u"Item");
builder->get_CellFormat()->set_RightPadding(40);
builder->InsertCell();
builder->Writeln(u"Quantity (kg)");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Apples");
builder->InsertCell();
builder->Writeln(u"20");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Bananas");
builder->InsertCell();
builder->Writeln(u"40");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Carrots");
builder->InsertCell();
builder->Writeln(u"50");
builder->EndRow();
doc->Save(ArtifactsDir + u"DocumentBuilder.InsertTableWithStyle.docx");

◆ get_TextWrapping()

Aspose::Words::Tables::TextWrapping Aspose::Words::Tables::Table::get_TextWrapping ( )

Gets or sets TextWrapping for table.

Examples

Shows how to work with table text wrapping.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Cell 1");
builder->InsertCell();
builder->Write(u"Cell 2");
builder->EndTable();
table->set_PreferredWidth(PreferredWidth::FromPoints(300));
builder->get_Font()->set_Size(16);
builder->Writeln(u"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
// Set the "TextWrapping" property to "TextWrapping.Around" to get the table to wrap text around it,
// and push it down into the paragraph below by setting the position.
table->set_TextWrapping(TextWrapping::Around);
table->set_AbsoluteHorizontalDistance(100);
table->set_AbsoluteVerticalDistance(20);
doc->Save(ArtifactsDir + u"Table.WrapText.docx");

◆ get_Title()

System::String Aspose::Words::Tables::Table::get_Title ( )

Gets or sets title of this table. It provides an alternative text representation of the information contained in the table.

The default value is an empty string.

This property is meaningful for ISO/IEC 29500 compliant DOCX documents (OoxmlCompliance). When saved to pre-ISO/IEC 29500 formats, the property is ignored.

Examples

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;
}

◆ get_TopPadding()

double Aspose::Words::Tables::Table::get_TopPadding ( )

Gets or sets the amount of space (in points) to add above the contents of cells.

Examples

Shows how to configure content padding in a table.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Row 1, cell 1.");
builder->InsertCell();
builder->Write(u"Row 1, cell 2.");
builder->EndTable();
// For every cell in the table, set the distance between its contents and each of its borders.
// This table will maintain the minimum padding distance by wrapping text.
table->set_LeftPadding(30);
table->set_RightPadding(60);
table->set_TopPadding(10);
table->set_BottomPadding(90);
table->set_PreferredWidth(PreferredWidth::FromPoints(250));
doc->Save(ArtifactsDir + u"DocumentBuilder.SetRowFormatting.docx");

◆ get_VerticalAnchor()

Aspose::Words::Drawing::RelativeVerticalPosition Aspose::Words::Tables::Table::get_VerticalAnchor ( )

Gets the base object from which the vertical positioning of floating table should be calculated. Default value is Margin.

Examples

Shows how to work with floating tables properties.

auto doc = MakeObject<Document>(MyDir + u"Table wrapped by text.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
if (table->get_TextWrapping() == TextWrapping::Around)
{
ASSERT_EQ(RelativeHorizontalPosition::Margin, table->get_HorizontalAnchor());
ASSERT_EQ(RelativeVerticalPosition::Paragraph, table->get_VerticalAnchor());
ASPOSE_ASSERT_EQ(false, table->get_AllowOverlap());
// Only Margin, Page, Column available in RelativeHorizontalPosition for HorizontalAnchor setter.
// The ArgumentException will be thrown for any other values.
table->set_HorizontalAnchor(RelativeHorizontalPosition::Column);
// Only Margin, Page, Paragraph available in RelativeVerticalPosition for VerticalAnchor setter.
// The ArgumentException will be thrown for any other values.
table->set_VerticalAnchor(RelativeVerticalPosition::Page);
}

◆ GetType()

virtual const System::TypeInfo& Aspose::Words::Tables::Table::GetType ( ) const
overridevirtual

Reimplemented from Aspose::Words::CompositeNode.

◆ Is()

virtual bool Aspose::Words::Tables::Table::Is ( const System::TypeInfo target) const
overridevirtual

Reimplemented from Aspose::Words::CompositeNode.

◆ set_AbsoluteHorizontalDistance()

void Aspose::Words::Tables::Table::set_AbsoluteHorizontalDistance ( double  value)

◆ set_AbsoluteVerticalDistance()

void Aspose::Words::Tables::Table::set_AbsoluteVerticalDistance ( double  value)

◆ set_Alignment()

void Aspose::Words::Tables::Table::set_Alignment ( Aspose::Words::Tables::TableAlignment  value)

◆ set_AllowAutoFit()

void Aspose::Words::Tables::Table::set_AllowAutoFit ( bool  value)

◆ set_AllowCellSpacing()

void Aspose::Words::Tables::Table::set_AllowCellSpacing ( bool  value)

◆ set_Bidi()

void Aspose::Words::Tables::Table::set_Bidi ( bool  value)

◆ set_BottomPadding()

void Aspose::Words::Tables::Table::set_BottomPadding ( double  value)

◆ set_CellSpacing()

void Aspose::Words::Tables::Table::set_CellSpacing ( double  value)

◆ set_Description()

void Aspose::Words::Tables::Table::set_Description ( const System::String value)

◆ set_HorizontalAnchor()

void Aspose::Words::Tables::Table::set_HorizontalAnchor ( Aspose::Words::Drawing::RelativeHorizontalPosition  value)

◆ set_LeftIndent()

void Aspose::Words::Tables::Table::set_LeftIndent ( double  value)

◆ set_LeftPadding()

void Aspose::Words::Tables::Table::set_LeftPadding ( double  value)

◆ set_PreferredWidth()

void Aspose::Words::Tables::Table::set_PreferredWidth ( const System::SharedPtr< Aspose::Words::Tables::PreferredWidth > &  value)

◆ set_RelativeHorizontalAlignment()

void Aspose::Words::Tables::Table::set_RelativeHorizontalAlignment ( Aspose::Words::Drawing::HorizontalAlignment  value)

◆ set_RelativeVerticalAlignment()

void Aspose::Words::Tables::Table::set_RelativeVerticalAlignment ( Aspose::Words::Drawing::VerticalAlignment  value)

◆ set_RightPadding()

void Aspose::Words::Tables::Table::set_RightPadding ( double  value)

◆ set_Style()

void Aspose::Words::Tables::Table::set_Style ( const System::SharedPtr< Aspose::Words::Style > &  value)

◆ set_StyleIdentifier()

void Aspose::Words::Tables::Table::set_StyleIdentifier ( Aspose::Words::StyleIdentifier  value)

◆ set_StyleName()

void Aspose::Words::Tables::Table::set_StyleName ( const System::String value)

◆ set_StyleOptions()

void Aspose::Words::Tables::Table::set_StyleOptions ( Aspose::Words::Tables::TableStyleOptions  value)

◆ set_TextWrapping()

void Aspose::Words::Tables::Table::set_TextWrapping ( Aspose::Words::Tables::TextWrapping  value)

◆ set_Title()

void Aspose::Words::Tables::Table::set_Title ( const System::String value)

◆ set_TopPadding()

void Aspose::Words::Tables::Table::set_TopPadding ( double  value)

◆ set_VerticalAnchor()

void Aspose::Words::Tables::Table::set_VerticalAnchor ( Aspose::Words::Drawing::RelativeVerticalPosition  value)

◆ SetBorder()

void Aspose::Words::Tables::Table::SetBorder ( Aspose::Words::BorderType  borderType,
Aspose::Words::LineStyle  lineStyle,
double  lineWidth,
System::Drawing::Color  color,
bool  isOverrideCellBorders 
)

Sets the specified table border to the specified line style, width and color.

Parameters
borderTypeThe table border to change.
lineStyleThe line style to apply.
lineWidthThe line width to set (in points).
colorThe color to use for the border.
isOverrideCellBordersWhen true, causes all existing explicit cell borders to be removed.
Examples

Shows how to apply an outline border to a table.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
// Align the table to the center of the page.
table->set_Alignment(TableAlignment::Center);
// Clear any existing borders and shading from the table.
table->ClearBorders();
table->ClearShading();
// Add green borders to the outline of the table.
// Fill the cells with a light green solid color.
doc->Save(ArtifactsDir + u"Table.SetOutlineBorders.docx");

◆ SetBorders()

void Aspose::Words::Tables::Table::SetBorders ( Aspose::Words::LineStyle  lineStyle,
double  lineWidth,
System::Drawing::Color  color 
)

Sets all table borders to the specified line style, width and color.

Parameters
lineStyleThe line style to apply.
lineWidthThe line width to set (in points).
colorThe color to use for the border.
Examples

Shows how to apply border and shading color while building a table.

auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
// Start a table and set a default color/thickness for its borders.
SharedPtr<Table> table = builder->StartTable();
// Create a row with two cells with different background colors.
builder->InsertCell();
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_LightSkyBlue());
builder->Writeln(u"Row 1, Cell 1.");
builder->InsertCell();
builder->get_CellFormat()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_Orange());
builder->Writeln(u"Row 1, Cell 2.");
builder->EndRow();
// Reset cell formatting to disable the background colors
// set a custom border thickness for all new cells created by the builder,
// then build a second row.
builder->get_CellFormat()->ClearFormatting();
builder->get_CellFormat()->get_Borders()->get_Left()->set_LineWidth(4.0);
builder->get_CellFormat()->get_Borders()->get_Right()->set_LineWidth(4.0);
builder->get_CellFormat()->get_Borders()->get_Top()->set_LineWidth(4.0);
builder->get_CellFormat()->get_Borders()->get_Bottom()->set_LineWidth(4.0);
builder->InsertCell();
builder->Writeln(u"Row 2, Cell 1.");
builder->InsertCell();
builder->Writeln(u"Row 2, Cell 2.");
doc->Save(ArtifactsDir + u"DocumentBuilder.TableBordersAndShading.docx");

Shows how to format of all of a table's borders at once.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
// Clear all existing borders from the table.
table->ClearBorders();
// Set a single green line to serve as every outer and inner border of this table.
doc->Save(ArtifactsDir + u"Table.SetBorders.docx");

◆ SetShading()

void Aspose::Words::Tables::Table::SetShading ( Aspose::Words::TextureIndex  texture,
System::Drawing::Color  foregroundColor,
System::Drawing::Color  backgroundColor 
)

Sets shading to the specified values on all cells in the table.

Parameters
textureThe texture to apply.
foregroundColorThe color of the texture.
backgroundColorThe color of the background fill.
Examples

Shows how to apply an outline border to a table.

auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
SharedPtr<Table> table = doc->get_FirstSection()->get_Body()->get_Tables()->idx_get(0);
// Align the table to the center of the page.
table->set_Alignment(TableAlignment::Center);
// Clear any existing borders and shading from the table.
table->ClearBorders();
table->ClearShading();
// Add green borders to the outline of the table.
// Fill the cells with a light green solid color.
doc->Save(ArtifactsDir + u"Table.SetOutlineBorders.docx");

◆ Type()

static const System::TypeInfo& Aspose::Words::Tables::Table::Type ( )
static