Extract Text from and Replace Text in a Table

Extract Plain Text from a Table

A Table like any other node in Aspose.Words has access to a Range object. Using this object, you can call methods over the entire table range to extract the table as plain text. The Range.getText() property is used for this purpose. 

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
public static void printTextRangeOfATable() throws Exception {
Document doc = new Document(dataDir + "Table.SimpleTable.doc");
// Get the first table in the document.
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
// The range text will include control characters such as "\a" for a cell.
// You can call ToTxt() on the desired node to find the plain text.
// Print the plain text range of the table to the screen.
System.out.println("Contents of the table: ");
System.out.println(table.getRange().getText());
}

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
public static void printTextRangeOfRowAndTableElements() throws Exception {
Document doc = new Document(dataDir + "Table.SimpleTable.doc");
// Get the first table in the document.
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
// Print the contents of the first row to the screen.
System.out.println("\nContents of the row: ");
System.out.println(table.getFirstRow().getRange().getText());
// Print the contents of the last cell in the table to the screen.
System.out.println("\nContents of the cell: ");
System.out.println(table.getLastRow().getLastCell().getRange().getText());
}

Replace Text in a Table

Using a table’s range object you can replace text within the table. However, there are currently restrictions which prevent any replacement with special characters being made so care must be taken to ensure that the replacement string does not carry over more than one paragraph or cell. If such a replacement is made which spans across multiple nodes, such as paragraphs or cells, then an exception is thrown.

Normally the replacement of text should be done at the cell level (per cell) or at the paragraph level.

Shows how to replace all instances of a string of text in a table and cell.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "Table.SimpleTable.doc");
// Get the first table in the document.
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
FindReplaceOptions opts = new FindReplaceOptions();
opts.setMatchCase(true);
opts.setFindWholeWordsOnly(true);
// Replace any instances of our string in the entire table.
table.getRange().replace("Carrots", "Eggs", opts);
// Replace any instances of our string in the last cell of the table only.
table.getLastRow().getLastCell().getRange().replace("50", "20", opts);
doc.save(dataDir + "Table.ReplaceCellText Out.docx");