In Aspose.Words text in a table can be normally changed using the Range.Replace method.
The following code example shows how to change text in a table:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
|
|
Document doc = new Document(MyDir + "Change text in a table.docx");
|
|
|
|
// Get the first table in the document.
|
|
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
|
|
// Replace any instances of our string in the last cell of the table only.
|
|
FindReplaceOptions options = new FindReplaceOptions
|
|
{
|
|
MatchCase = true,
|
|
FindWholeWordsOnly = true
|
|
};
|
|
table.Rows[1].Cells[2].Range.Replace("Mr", "test", options);
|
|
|
|
doc.Save(ArtifactsDir + "Replace text - Aspose.Words.docx"); |
You can also do the same using the Open XML SDK. At the same time, note that it looks somewhat more complicated and more cumbersome.
The following code example shows how to change text in a table:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
|
|
File.Copy(MyDir + "Replace text.docx", ArtifactsDir + "Replace text - OpenXML.docx", true);
|
|
|
|
// Use the file name and path passed in as an argument to open an existing document.
|
|
using WordprocessingDocument doc = WordprocessingDocument.Open(ArtifactsDir + "Replace text - OpenXML.docx", true);
|
|
|
|
// Get the main document part.
|
|
MainDocumentPart mainPart = doc.MainDocumentPart;
|
|
if (mainPart?.Document?.Body == null)
|
|
throw new InvalidOperationException("The document does not contain a valid body.");
|
|
|
|
// Find the first table in the document.
|
|
Table table = mainPart.Document.Body.Elements<Table>().FirstOrDefault();
|
|
// Find the second row in the table.
|
|
TableRow row = table.Elements<TableRow>().ElementAtOrDefault(1);
|
|
// Find the third cell in the row.
|
|
TableCell cell = row.Elements<TableCell>().ElementAtOrDefault(2);
|
|
// Find the first paragraph in the table cell.
|
|
Paragraph paragraph = cell.Elements<Paragraph>().FirstOrDefault();
|
|
// Find the first run in the paragraph.
|
|
Run run = paragraph.Elements<Run>().FirstOrDefault();
|
|
|
|
// Find the first text element in the run.
|
|
Text text = run.Elements<Text>().FirstOrDefault();
|
|
if (text == null)
|
|
{
|
|
// If no text element exists, create one.
|
|
text = new Text();
|
|
run.Append(text);
|
|
}
|
|
|
|
// Set the text for the run.
|
|
text.Text = "The text from the OpenXML API example"; |