This article covers various approaches to find and replace text in Word documents using C# in .NET or .NET Core applications.
Find and Replace is quite a useful feature of MS Word to quickly locate and replace the desired text in the documents. It becomes more handy and time-saving when you are working with longer documents. In cases when you need to find and replace text in hundreds of Word documents or you need to automate this process, you’ll definitely opt to do it programmatically. So in this article, I’ll show you how to find and replace text in Word documents programmatically in different scenarios using C#. Once you have read this article, you will be able to:
- find and replace text in Word DOC/DOCX using C#
- find and replace similar words in Word document
- find and replace text using Regex
- find and replace text in the header/footer of Word document
- find and replace text with meta-characters in Word document
TIP: You may be interested in a free Text to GIF Converter that allows you to generate animations from texts.
C# Library to Find and Replace Text in a Word Document
First of all, create a new C# project (Console, ASP.NET, etc.) in Visual Studio and install Aspose.Words for .NET via NuGet Package Manager or Package Manager Console.
Installing via NuGet Package Manager
Installing via Package Manager Console
PM> Install-Package Aspose.Words
After we have installed Aspose.Words for .NET, let’s now begin finding and replacing text in the following Word document.
Find and Replace Text in Word Documents in C#
The following are the steps to find and replace a particular text in a Word document using Aspose.Words for .NET.
- Create an instance of Document class and initialize with Word document’s path.
- Find and replace text using Document.Range.Replace(string, string, FindReplaceOptions) method.
- Save the document using Document.Save(string) method.
The FindReplaceOptions class provides various options to customize the find/replace operations. The following code sample shows how to find and replace a particular word or string in a Word document using C#.
Find and Replace Similar Words in Word Documents using C#
You can also customize the API to find similar words and replace them with a particular word. For example, you can find the words “sad” and “mad” and replace them with a single word. The following code sample shows how to find and replace similar words in Word document using C#.
Find and Replace Text using Regex in C#
There might be the case when you want to find and replace text that appears in a particular pattern. For example, you need to hide/replace all the email IDs in a Word document. In such cases, you can create a regular expression for email IDs and pass it to Document.Range.Replace(Regex, string, FindReplaceOptions) method.
The following code sample shows how to find and replace text based on a pattern in Word document.
Find and Replace Text in Header/Footer of Word Document using C#
You can also find and replace text in the header or footer sections of a Word document using the HeaderFooter class. The HeaderFooter.Range.Replace(string, string, FindReplaceOptions) method is used for this purpose. The following code sample shows how to replace text in the header/footer of Word document in C#.
Find and Replace Text with Meta-Characters in Word Documents using C#
There could be a scenario where a particular text or phrase is composed of multiple paragraphs, sections, or pages. In such cases, the simple find and replace method wouldn’t work effectively and we’ll have to handle the paragraphs breaks, section breaks, or page breaks. For this, Aspose.Words allows you to use the following meta-characters in the search string or the replacement string:
- &p: paragraph break
- &b: section break
- &m: page break
- &l: line break
The following code sample shows how to find and replace the text with a paragraph break in a Word document.
This article covers some useful ways to find and replace text in a Word document based on matched or similar words, phrases, and regex patterns programmatically. These features not only automate the text replacement process but save you a lot of time and effort required in manual find and replace operation in Word documents. You can learn more about Aspose’s Word Library using documentation.