DEV Community

Usman Aziz
Usman Aziz

Posted on • Edited on • Originally published at blog.groupdocs.com

3 1

Find and Replace Text in Documents using .NET API

One of the popular uses cases related to the documents is finding and replacing the text programmatically. There could be multiple cases when you need to replace a word or phrase in the document. For example, you want to censor the content before sharing the document publically or you want to hide/remove all the email IDs or Social Security Numbers provided in the document before putting it online.

So in this article, I am going to target this use case and will show you how you could easily find and replace your desired text in documents programmatically using C#. For this, I will use GroupDocs.Redaction for .NET API which provides multiple ways to hide or replace text in the popular formats of MS WordExcelPowerPoint, and PDF documents.

So you don’t need MS Office, PDF editor or any other third party software in this scenario. Let’s now begin and have a look at different approaches to deal with finding and replacing text in the documents. The following is the screenshot of a Word document that I have used in the examples for demonstration. The same methods will work for other document formats I have mentioned above without any change in the code.

Alt Text

Find and replace a word or phrase

This is how you can replace a single word or phrase in the document.

using GroupDocs.Redaction;
using GroupDocs.Redaction.Redactions;
...

using (Document doc = Redactor.Load("sample.docx"))
{
    doc.RedactWith(new ExactPhraseRedaction("John Doe", new ReplacementOptions("[censored]")));
    doc.Save();
}
Enter fullscreen mode Exit fullscreen mode

Output

Alt Text

Find and replace case-sensitive word or phrase

In case you want to replace text considering its case sensitivity, use the following code.

using GroupDocs.Redaction;
using GroupDocs.Redaction.Redactions;
...

using (Document doc = Redactor.Load("sample.docx"))
{
    doc.RedactWith(new ExactPhraseRedaction("John Doe", true /*isCaseSensitive*/, new ReplacementOptions("[censored]")));
    doc.Save();
}
Enter fullscreen mode Exit fullscreen mode

Output

Alt Text

Find and replace text using regular expressions (regex)

If you need to replace the text occurrences that are following a particular pattern, for example SS#, you can do it using the regular expressions.

using GroupDocs.Redaction;
using GroupDocs.Redaction.Redactions;
...

using (Document doc = Redactor.Load("sample.docx"))
{
    // Regex to match a number having 2 digits, space, 2 digits, space, 6 digits
    doc.RedactWith(new RegexRedaction(@"\\d{2}\\s*\\d{2}[^\\d]*\\d{6}", new ReplacementOptions("[censored]")));
    doc.Save();
}
Enter fullscreen mode Exit fullscreen mode

Output

Alt Text

Find and replace the text with colored box

You can also replace your desired text with a colored box.

using GroupDocs.Redaction;
using GroupDocs.Redaction.Redactions;
...

using (Document doc = Redactor.Load("sample.docx"))
{
    doc.RedactWith(new ExactPhraseRedaction("John Doe", true, new ReplacementOptions(System.Drawing.Color.Black)));
    doc.Save();
}
Enter fullscreen mode Exit fullscreen mode

Output

Alt Text

Cheers!

See Also

Extract Text from PDF C#
Extract Text from PowerPoint C#
Find and Replace Text in Excel C#
Find and Replace Text in Word C#
Find and Replace Text in PDF C#

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (2)

Collapse
 
umar320 profile image
Muhamid Umar

Great article. Keep up the good work.

Collapse
 
olekria profile image
Olek Ria
Comment hidden by post author

Some comments have been hidden by the post's author - find out more

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay