In Aspose.Words, use the Replace method to find and replace text.
The following code example shows how to find and replace text from a document part:
public static void SearchAndReplaceTextFeature()
{
Document doc = new Document(MyDir + "Search and replace text.docx");
Regex regex = new Regex("Hello World!", RegexOptions.IgnoreCase);
doc.Range.Replace(regex, "Hi Everyone!");
doc.Save(ArtifactsDir + "Search and 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.
Following are the namespaces we need to add:
using System.IO;
using System.Text.RegularExpressions;
using DocumentFormat.OpenXml.Packaging;
using NUnit.Framework;
The following code example shows how to find and replace text from a document part:
public static void SearchAndReplaceTextFeature()
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(MyDir + "Search and replace text.docx", true))
{
string docText;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
Regex regexText = new Regex("Hello world!");
docText = regexText.Replace(docText, "Hi Everyone!");
using (StreamWriter sw = new StreamWriter(File.Create(ArtifactsDir + "Search and replace text - OpenXML.docx")))
{
sw.Write(docText);
}
}
}