Syncfusion Feedback

Find and Replace in Word Documents Using the Syncfusion .NET Word Library

The Syncfusion® .NET Word Library provides find and replace text with dynamic elements, including images, hyperlinks, paragraphs, tables, or even an entire document. This functionality operates independently of Microsoft Word and interop dependencies.

Watch this video to learn how to find and replace in a Word document using the Syncfusion .NET Word Library.

Watch the video

Find and replace text in Word document using C#

Learn how to find and replace text in Word documents programmatically using C# with the Syncfusion .NET Word Library. This guide demonstrates replacing text with content elements like text and other dynamic content.

Step 1: Create a new project

Start by creating a new C# Console Application project.

Step 2: Install the NuGet package

Add the Syncfusion.DocIO.Net.Core package to your project from NuGet.org.

Step 3: Add required namespaces to find and replace text

Add the following namespaces to your Program.cs file:

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System.IO;

Step 4: Open the Word document

Open an existing Word document that contains the text to be replaced.

FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Open an existing Word document
WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx);

Step 5: Perform find and replace text in the document

Find all occurrences of the specified text and replaces them with new text.

Run

//Find all occurrences of a misspelled word and replaces with properly spelled word
document.Replace("Cyles", "Cycles", true, true);

Step 6: Save the Word document

Save the Word document to a file stream in DOCX format.

//Create file stream
FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite);
//Save the Word document to file stream
document.Save(outputFileStream, FormatType.Docx);
document.Close();
fileStreamPath.Close();
outputFileStream.Close();

NuGet installation

Nuget Installation image Syncfusion.DocIO.Net.Core Copy Icon image

Get started quickly by downloading the installer and checking license information on the Downloads page.

Syncfusion .NET Word Library Resources

Explore these resources for comprehensive guides, knowledge base articles, insightful blogs, and ebooks.