Work with Text in a Table

As mentioned in previous articles, a table usually contains plain text, although other content such as images or even other tables can be placed in table cells.

Adding text or other content to the table is carried out using the appropriate methods of the DocumentBuilder class and is described in the “Create a Table” article. In this article, we will talk about how to work with text in an already existing table.

Replace Text in a Table

The table, like any other node in Aspose.Words, has access to the Range object. Using the table range object, you can replace text in a table.

The ability to use special characters when replacing is currently supported, so it is possible to replace existing text with multi-paragraph text. To do this, you need to use the special metacharacters described in the corresponding Replace method.

The following code example shows how to replace all instances of a string of text in the cells of an entire table:

Extract Plain Text from a Table

Using the Range object, you can also call methods on the entire table range and extract the table as plain text. To do this, use the Text property.

The following code example shows how to print the text range of a table:

The following code example shows how to print the text range of row and table elements.

Working with Alternative Table Text

Microsoft Word tables have a table title and table description that provide an alternative textual representation of the information contained in the table.

In Aspose.Words, you can also add a table title and description using the Title and Description properties. These properties are meaningful for DOCX documents conforming to the ISO/IEC 29500. When saving in formats earlier than ISO/IEC 29500, these properties are ignored.

The following code example shows how to set the title and description properties of a table: