Articles in this section
Category / Section

How to find and replace in Word document with text from database?

4 mins read

Syncfusion Essential DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can find and replace in Word document with text from database in C#.

Steps to find and replace in Word document with text from database:

  1. Create a new C# .NET Core console application project. Create .NET Core console application in Visual Studio in ASP.NET Core Word
  2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from and System.Data.SqlClient Nuget package. Add DocIO.Net.Core NuGet packages of ASP.NET Core Word
  3. Create table in database with columns "TextToFind" and "ReplacementText" as like the screenshot shown below.

Image of the database table

  1. Include the following namespace in the Program.cs file:


using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System.Data;
using System.Data.SqlClient;
  1. Use the following code example to find and replace in Word document with text from database in C#:


using (FileStream inputStream = new FileStream(Path.GetFullPath(@"../../../Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    //Set datasource and database values.
    string datasource = "";
    string database = "";
    //Load the file stream into a Word document.
    using (WordDocument document = new WordDocument(inputStream, FormatType.Automatic))
        //Create sql connection to get data from the database.
        SqlConnection sqlConnection= new SqlConnection("Data Source=" + datasource + ";Initial Catalog=" + database + ";Integrated Security=True");
        //Retrive data from the table 'FindReplace' using SqlCommand.
        SqlCommand sqlCommand = new SqlCommand("Select * from FindReplace");
        sqlCommand.Connection = sqlConnection;
        //Load the data into DataTable using SqlDataAdapter.
        SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCommand);
        dataAdapter.SelectCommand.CommandTimeout = 0;
        DataTable dataTable = new DataTable();
        //Iterate DataRow to find and replace text with other text from the database.
        foreach (DataRow row in dataTable.Rows)
            //First column contains text to find.
            //Second column contains replacement text.
            document.Replace(row[dataTable.Columns[0]] as string, row[dataTable.Columns[1]] as string, false, false);
        //Create a file stream.
        using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite))
            //Save the Word document to the file stream.
            document.Save(outputFileStream, FormatType.Docx);

A complete working sample to find and replace in Word document with text from database in C# can be downloaded from GitHub.

By executing the program, you will get the Output document as follows.

Output document generated in ASP.NET Core Word

Take a moment to peruse the documentation, where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

See Also:

How to replace the particular text with hyperlink in Word document

How to replace text in a Word document with HTML

How to find and replace text inside table in Word document

How to find and replace text in headers and footers of Word document

How to find and replace placeholder with page break in Word document

How to find and replace line break in Word document as paragraph mark?

How to find and replace text with content control in Word document?


Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.


Did you find this information helpful?
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment