Find And Replace Text In Word Document Using C#

Introduction

In Microsoft Word, we can find special text strings and replace them with new text strings easily. We can replace all the searched text automatically at one time and we can also only replace some of them from the searched results. This article will mention various approaches to the find and replace feature in the Word document by using a .NET API Spire.Doc independently (without using any third-party code) as below.

  • Find and replace the text string in Word with a new text string
  • Find a specific index text string and only replace it with the new text string
  • Find and Replace Text by Regular Expressions
  • Find and Replace Text with table
  • Find and replace text with image

Find and replace a text string in Word with a new text string.

All the searched results will be detected and replaced at one time by invoking the Replace (string matchString, string newValue, bool caseSensitive, bool wholeWord) method to realize the replace function. We could also set if the replacement is case-sensitive or if the whole Word document will be affected.

Document doc = new Document();  
doc.LoadFromFile("Sample.docx");  
doc.Replace("Document", "Doc", true, true);  
doc.SaveToFile("FindandReplace.docx", FileFormat.Docx2013);

Find a specified index text string and only replace it with the new text string.

That means if the same text string occurs many times in a Word document, we only replace a special one, such as the second one and the others remain the same. By invoking FindAllString (string matchString, bool caseSensitive, bool wholeWord) method, we can find the text strings and then set the new string for the indexed one that we want to replace.

Document doc = new Document();  
doc.LoadFromFile("Sample.docx");  
  
Section sec = doc.Sections[0];  
TextSelection[] finds = doc.FindAllString("John", true, true);  
finds[1].GetAsOneRange().Text = "NewString";  
doc.SaveToFile("Result.docx", FileFormat.Docx2013);

Find the words that match a specific regular expression in a Word document and replace the matched words with a new string.

The following sample will show you how to replace the regular expression for the words that start with”#”.

Document doc = new Document();  
doc.LoadFromFile("example.docx");  

Regex regex = new Regex(@"\#\w+\b");  
doc.Replace(regex, "NewString");  

doc.SaveToFile("Result.docx", FileFormat.Docx2013);

Do a simple text replacement with a table.

The following example shows that we add a table in the middle of the document where a 'Key Text' is found. At the same time, we have removed the paragraph where the 'Key Text' is located.

Document doc = new Document();  
doc.LoadFromFile("Sample.docx");  

Section section = doc.Sections[0];  
TextSelection selection = doc.FindString("Key Text", true, true);  
TextRange range = selection.GetAsOneRange();  
Paragraph paragraph = range.OwnerParagraph;  
Body body = paragraph.OwnerTextBody;  
int index = body.ChildObjects.IndexOf(paragraph);  

Table table = section.AddTable(true);  
table.ResetCells(3, 3);  
body.ChildObjects.Remove(paragraph);  
body.ChildObjects.Insert(index, table);  

doc.SaveToFile("Result2.doc", FileFormat.Docx2013);

Find and replace text in a Word doc with an image. In the following sample, we find the text “South Islands” and replace it with an image.

Document doc = new Document();  
doc.LoadFromFile("Sample.docx");  
Image image = Image.FromFile("logo.jpg");  

TextSelection[] selections = doc.FindAllString("South Islands", true, true);  
int index = 0;  
TextRange range = null;  

foreach (TextSelection selection in selections)  
{  
    DocPicture pic = new DocPicture(doc);  
    pic.LoadImage(image);  

    range = selection.GetAsOneRange();  
    index = range.OwnerParagraph.ChildObjects.IndexOf(range);  
    range.OwnerParagraph.ChildObjects.Insert(index, pic);  
    range.OwnerParagraph.ChildObjects.Remove(range);  
}  

doc.SaveToFile("Result3.docx", FileFormat.Docx2013);

Conclusion

It is quite easy for us to use Spire.Doc to find and replace the text on the Word document with new text strings, tables, image etc. in C#. Thanks for your reading and I hope it helps.


Similar Articles