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 of 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 text string in Word with 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 replace function. We could also set if the replacement is case-sensitive or the whole word document will be affected.
  1. Document doc = new Document();  
  2. doc.LoadFromFile("Sample.docx");  
  3. doc.Replace("Document""Doc"truetrue);        
  4. 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 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.
  1. Document doc = new Document();  
  2. doc.LoadFromFile("Sample.docx");  
  3.   
  4. Section sec = doc.Sections[0];  
  5. TextSelection[] finds = doc.FindAllString("John"truetrue);  
  6. finds[1].GetAsOneRange().Text = "NewString";  
  7. 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”#”.
  1. Document doc = new Document();  
  2. doc.LoadFromFile("example.docx");  
  3. Regex regex = new Regex(@"\#\w+\b");  
  4. doc.Replace(regex, "NewString");  
  5. doc.SaveToFile("Result.docx", FileFormat.Docx2013);  

Do a simple text replacement with table.

The following example shows that we add a table in the middle of the document where a 'Key Text' is found. And at the same time, we have removed the paragraph where the 'Key Text' located.
  1. Document doc = new Document();  
  2. doc.LoadFromFile("Sample.docx");  
  3. Section section = doc.Sections[0];  
  4. TextSelection selection = doc.FindString("Key Text"truetrue);  
  5. TextRange range = selection.GetAsOneRange();  
  6. Paragraph paragraph = range.OwnerParagraph;  
  7. Body body = paragraph.OwnerTextBody;  
  8. int index = body.ChildObjects.IndexOf(paragraph);  
  9.   
  10. Table table = section.AddTable(true);  
  11. table.ResetCells(3, 3);  
  12. body.ChildObjects.Remove(paragraph);  
  13. body.ChildObjects.Insert(index, table);  
  14.   
  15. doc.SaveToFile("Result2.doc", FileFormat.Docx2013); 

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

  1. Document doc = new Document();  
  2. doc.LoadFromFile("Sample.docx");  
  3. Image image = Image.FromFile("logo.jpg");  
  4.   
  5. TextSelection[] selections = doc.FindAllString("South Islands"truetrue);  
  6. int index = 0;  
  7. TextRange range = null;  
  8.   
  9. foreach (TextSelection selection in selections)  
  10. {  
  11.     DocPicture pic = new DocPicture(doc);  
  12.     pic.LoadImage(image);  
  13.   
  14.     range = selection.GetAsOneRange();  
  15.     index = range.OwnerParagraph.ChildObjects.IndexOf(range);  
  16.     range.OwnerParagraph.ChildObjects.Insert(index, pic);  
  17.     range.OwnerParagraph.ChildObjects.Remove(range);  
  18.   
  19. }  
  20.   
  21. 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, table, and image etc. in C#. Thanks for your reading and I hope it helps.