Search and Replace

Wordize Replacement for .NET enables search and replace, supports regex, and works with document conversion modules for automated text modifications.
Supported Modules and Document Formats

Wordize supports search and replace operations for documents in any format:

  • to find and replace, use the Wordize Replacement for .NET module
  • to work with documents in the required formats, select the appropriate document conversion module

Wordize provides powerful search and replace functionality, allowing developers to find specific text or patterns in documents and replace them programmatically. This feature is useful for updating templates, correcting errors, and automating content modifications across multiple documents.

With Wordize Replacement for .NET, you can:

  1. Find and replace plain text
  2. Use regular expressions for advanced search operations

Search and Replace Simple Text

The simplest way to replace text in a document is by providing a search string and a replacement string. This method is useful for making direct word or phrase substitutions.

Simple search and replace

The following code example shows how to perform search and replace on a string using the Replace method:

var doc = "Document.docx";
var pattern = "Wordize";
var replacement = "Wordize Pro";

Replacer.Replace(doc, "Document.1.docx", pattern, replacement);

Search and Replace Using Regular Expressions

Wordize also provides a powerful way to search and replace documents using regular expressions (Regex). This is useful when working with dynamic text such as dates, numbers, placeholders, or recurring phrases.

Using Wordize Replacer for .NET, developers can define complex search patterns and replace them with the desired text.

Search and replace with regex

The following code example shows how to perform search and replace through a regular expression using the Replace method:

var doc = "Document.docx";
Regex pattern = new Regex("gr(a|e)y");
var replacement = "lavender";

Replacer.Replace(doc, "Document.4.docx", pattern, replacement);

Customize Search and Replace

You can work with many options during the search and replace process using the following FindReplaceOptions:

  • FindWholeWordsOnly – to specify whether we are searching for a standalone word
  • IgnoreDeleted – to specify whether to ignore text inside delete revisions, defaults to “false”
  • IgnoreInserted – to specify whether to ignore text inside insert revisions, defaults to “false”
  • IgnoreFields – to specify whether to ignore text inside fields, defaults to “false”
  • IgnoreFieldCodes – to specify whether to ignore field codes only, defaults to “false”
  • IgnoreFootnotes – to specify whether to ignore footnotes, defaults to “false”
  • IgnoreStructuredDocumentTags – to specify whether to ignore the content of SDT, defaults to “false”
  • IgnoreShapes – to specify whether to ignore shapes within a text, defaults to “false”
  • MatchCase – “true” if the search is case sensitive, “false” if the search is case insensitive
  • ReplacementFormat – to specify the replacement format, defaults to “Text”
  • UseSubstitutions – to specify whether to recognize and use substitutions within replacement patterns, defaults to “false”

The following code example shows how to perform search and replace with the FindWholeWordsOnly option:

var doc = "Document.doc";
var pattern = "Wordize";
var replacement = "Wordize Pro";

FindReplaceOptions options = new FindReplaceOptions { FindWholeWordsOnly = true };

Replacer.Replace(doc, "Document.7.docx", SaveFormat.Docx, pattern, replacement, options);

The following code example shows how to specify ReplacementFormat to HTML:

var doc = "Document.doc";
var pattern = "run";
var replacement = "<b>Text </b><i>with </i><u>formatting</u>";

FindReplaceOptions options = new FindReplaceOptions() { ReplacementFormat = ReplacementFormat.Html };

Replacer.Replace(doc, "Document.7.docx", SaveFormat.Docx, pattern, replacement, options);

The following code example shows how to specify ReplacementFormat to Markdown:

var doc = "Document.doc";
var pattern = "run";
var replacement = "**Text** *with* `formatting`";

FindReplaceOptions options = new FindReplaceOptions() { ReplacementFormat = ReplacementFormat.Markdown };

Replacer.Replace(doc, "Document.8.docx", SaveFormat.Docx, pattern, replacement, options);

The following code example shows how to replace text only if the found pattern is a separate word:

var doc = "Document.doc";
var pattern = "Wordize";
var replacement = "Wordize Pro";

FindReplaceOptions options = new FindReplaceOptions { FindWholeWordsOnly = true };

Replacer.Replace(doc, "Document.9.docx", SaveFormat.Docx, pattern, replacement, options);

The following code example shows how to ignore text inside fields when replacing:

var doc = "Document.doc";
var pattern = "Wordize";
var replacement = "Wordize Pro";

FindReplaceOptions options = new FindReplaceOptions { IgnoreFields = true };

Replacer.Replace(doc, "Document.10.docx", SaveFormat.Docx, pattern, replacement, options);

The following code example shows how to how to replace all occurrences of a specified character string pattern with a replacement string in an input file and save the result to an image:

Stream[] pages = Replacer.ReplaceToImages("Document1.docx", new ImageSaveOptions(SaveFormat.Png), "Wordize", "(Wordize Pro)");
using var streamIn = File.OpenRead("Document1.docx");

Stream[] pages = Replacer.ReplaceToImages(streamIn, new ImageSaveOptions(SaveFormat.Png), "Wordize", "(Wordize Pro)");

See Also