We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Syncfusion Feedback


Trusted by the world’s leading companies

Overview

Streamline your Word document editing with the Syncfusion .NET Word Library (DocIO). Find and replace text with dynamic elements, including images, hyperlinks, paragraphs, tables, or even an entire document, in C#. This functionality operates independently of Microsoft Word and interop dependencies. Refine your searches using options for case-sensitive and whole-word matches.


How to find and replace text in a Word document using C#

  1. Install NuGet package: Install the Syncfusion.DocIO.Net.Core NuGet package in your project.
  2. Open Word document: Create a FileStream object to represent the input Word document.
  3. Load in .NET Word Library: Create a WordDocument object by passing the FileStream object.
  4. Find and replace text: Call the Replace method of the WordDocument class to replace all occurrences of the word “Adventure” with “Syncfusion”.
  5. Save the Word document: Save the WordDocument object to the FileStream object.

Here is an example of how to find and replace text in a Word document using C# in the Syncfusion .NET Word Library (DocIO).

//Load an existing Word document.
using FileStream inputDocumentStream = new FileStream("Template.docx", FileMode.Open, FileAccess.ReadWrite);
using WordDocument document = new WordDocument(inputDocumentStream, FormatType.Docx);

//Replace all occurrences of the word "Adventure" with "Syncfusion".
document.Replace("Adventure", "Syncfusion", false, true);

//Save the Word document.
using FileStream outputDocumentStream = new FileStream("Result.docx", FileMode.Create, FileAccess.ReadWrite);
document.Save(outputDocumentStream, FormatType.Docx);

Find options

Find text

Find text in a Word document based on a specific pattern using a regular expression (regex) or normal string. Search for the text in the entire Word document or just within a specific paragraph.

Casing and whole word

Refine the find operation by matching the casing or whole word for more precise results.

Find first

Find only the first occurrence of a piece of text in a Word document.

Find all

Find all the occurrences of a length of text in all parts of a Word document, including the body, header, footer, shapes, comments, footnotes, and endnotes.

Find next

Find the next occurrence of specified text in a Word document.

Find multiline text

Find the first or next occurrence of text that extends across several paragraphs.


Replace options

Replace text

Perform a find and replace operation for text in a Word document and see the total number of replacements made.

Replace first

Replace only the first occurrence of the specified text in a Word document.

Replace with document

Replace searched for text in a Word document with the entire content of another Word document.

Replace with document elements

Replace searched for text with document elements such as paragraphs, tables, and content controls, while maintaining their source formatting.

Replace with selection

Find and retrieve text with formatting (bold, highlight) for replacement in occurrences of other placeholders in a Word document, while preserving that formatting.

Replace multiline text

Replace all occurances of specified text extending across several paragraphs with replacement text or document elements.


Find document elements

Unlock advanced find functionalities in the Syncfusion .NET Word Library (DocIO) to find Word document elements such as images, shapes, and content controls based on their properties.

With this functionality, you can:

  1. Find the first item based on one property.
  2. Find the first item based on multiple properties.
  3. Find all the items based on one property.
  4. Find all the items based on multiple properties.

How to find Word document elements based on properties in C#

  1. Install NuGet package: Install the Syncfusion.DocIO.Net.Core NuGet package in your project.
  2. Open Word document: Create a FileStream object to represent the input Word document.
  3. Load in .NET Word Library: Create a WordDocument object by passing the FileStream object.
  4. Find document elements: Call the FindAllItemsByProperties method of the WordDocument object to find all elements based on properties.
  5. Save the Word document: Save the WordDocument object to the FileStream object.
//Load an existing Word document.
using FileStream inputDocumentStream = new FileStream("Template.docx", FileMode.Open, FileAccess.ReadWrite);
using WordDocument document = new WordDocument(inputDocumentStream, FormatType.Docx);

//Find all the inline content controls by Title and Tag. 
string[] propertyNames = new string[] { "ContentControlProperties.Title", "ContentControlProperties.Tag" };
string[]  propertyValues = new string[] { "Contact", "Contact" };
List<Entity> inlineContentControls = document.FindAllItemsByProperties(EntityType.InlineContentControl,propertyNames,propertyValues);

//Iterate the inline content controls.
for (int i = 0; i < inlineContentControls.Count; i++)
{
    InlineContentControl inlineContentControl = inlineContentControls[i] as InlineContentControl;
    // To do : Modify the retrieved elements.
}

//Save the Word document.
using FileStream outputDocumentStream = new FileStream("Result.docx", FileMode.Create, FileAccess.ReadWrite);
document.Save(outputDocumentStream, FormatType.Docx);




Awards

Greatness—it’s one thing to say you have it, but it means more when others recognize it. Syncfusion is proud to hold the following industry awards.

Scroll up icon