Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find, Replace & Hide Text, Phrase, Words in Word Documents using C#
// Find text and hide it by drawing rectangle over it using C#
using (Redactor redactor = new Redactor(@"path/document.docx"))
{
redactor.Apply(new ExactPhraseRedaction("John Doe", new ReplacementOptions(System.Drawing.Color.Black)));
redactor.Save();
}
// Find exact phrase and replace it with some other text using C#
using (Redactor redactor = new Redactor(@"path/document.docx"))
{
redactor.Apply(new ExactPhraseRedaction("John Doe", new ReplacementOptions("[censored]")));
redactor.Save();
}
// Find exact phrase (case-sensitive) and replace it with some other text using C#
using (Redactor redactor = new Redactor(@"path/document.docx"))
{
redactor.Apply(new ExactPhraseRedaction("John Doe", true /*isCaseSensitive*/, new ReplacementOptions("[censored]")));
redactor.Save();
}
// Find text using regular expression and replace it with some other text using C#
using (Redactor redactor = new Redactor(@"path/document.docx"))
{
redactor.Apply(new RegexRedaction("\\d{2}\\s*\\d{2}[^\\d]*\\d{6}", new ReplacementOptions("[censored]")));
redactor.Save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment