DEV Community

Jeremy K.
Jeremy K.

Posted on

How to Replace Word Text in C# – Free API

Why Replace Word Text Programmatically?

In many enterprise applications, programmatically modifying Word documents is a frequent requirement. Common use cases include:

  • Batch updating placeholders in contract templates.
  • Dynamically generating personalized reports and quotes.
  • Standardizing terminology across hundreds of documents.

Free Spire.Doc for .NET is a free .NET API that works without Microsoft Office installed. It provides robust methods like Replace and FindAllString to replace Word text in C# using plain strings or regular expressions.


Environment Setup

The fastest way to add Free Spire.Doc to your project is via NuGet.

Option 1 – NuGet Package Manager:
Search for FreeSpire.Doc in Visual Studio’s “Manage NuGet Packages” and install.

Option 2 – Package Manager Console:

Install-Package FreeSpire.Doc
Enter fullscreen mode Exit fullscreen mode

After installation, include these namespaces:

using Spire.Doc;
using Spire.Doc.Documents;
using System.Text.RegularExpressions;
Enter fullscreen mode Exit fullscreen mode

Example 1: Replace Specific Text in Word Using C

The core method for C# replace text in Word is Document.Replace(string matchString, string newValue, bool caseSensitive, bool wholeWord).

Parameter meaning:

Parameter Description
matchString The text to find.
newValue The replacement text.
caseSensitive true = case‑sensitive, false = ignore case.
wholeWord true = replace only whole words, false = replace any substring.

Example: Replace an old company name with a new one.

public static void BasicReplace(string inputPath, string outputPath)
{
    Document document = new Document();
    document.LoadFromFile(inputPath);

    // Case‑insensitive, whole‑word replacement
    document.Replace("old_company_name", "NewTech Solutions Inc.", false, true);

    document.SaveToFile(outputPath, FileFormat.Docx);
    document.Close();
}
Enter fullscreen mode Exit fullscreen mode

✅ This preserves original formatting – images, tables, headers, and footers remain intact.


Example 2: Replace Only the First Occurrence

Sometimes you only want to replace the first matched instance. Set Document.ReplaceFirst = true before calling Replace.

public static void ReplaceFirstInstance(string inputPath, string outputPath)
{
    Document document = new Document();
    document.LoadFromFile(inputPath);

    document.ReplaceFirst = true;
    document.Replace("placeholder", "actual value", false, true);

    document.SaveToFile(outputPath, FileFormat.Docx);
    document.Close();
}
Enter fullscreen mode Exit fullscreen mode

Example 3: Regex Replace Word Text in C

For dynamic patterns – like {{placeholder}}, invoice numbers, or email addresses – use Document.Replace(Regex regex, string newValue).

Example: Replace all curly‑brace placeholders with a fixed value.

using System.Text.RegularExpressions;

public static void RegexReplace(string inputPath, string outputPath)
{
    Document document = new Document();
    document.LoadFromFile(inputPath);

    Regex placeholderRegex = new Regex(@"\{\{.*?\}\}");
    document.Replace(placeholderRegex, "replacement text");

    document.SaveToFile(outputPath, FileFormat.Docx);
    document.Close();
}
Enter fullscreen mode Exit fullscreen mode

For even more flexibility, use the Replace(MatchEvaluator) overload to generate replacement values dynamically (e.g., sequential IDs, database lookups).


Summary & Best Practices

You’ve learned how to replace Word text in C# using Free Spire.Doc:

Scenario Method
Basic string replace Document.Replace
Replace only first match Document.ReplaceFirst = true
Regex pattern replace Document.Replace(Regex, …)

For large documents or complex formatting needs, consider other overloads or encapsulate the logic into reusable helpers. The free library handles most real‑world scenarios without costing a cent.

Top comments (0)