Articles in this section
Category / Section

How to replace text in a Word document with HTML?

3 mins read

Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can replace text in a Word document with HTML.

Steps to replace text in Word document with HTML using C#:

  1. Create a new C# console application project.

Create Console application in Visual Studio

  1. Install the Syncfusion.DocIO.WinForms NuGet package as a reference to your .NET Framework applications from NuGet.org.

Add DocIO NuGet package reference to the project

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

C#

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

VB

Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS
Imports System.IO
Imports System.Text.RegularExpressions
  1. Use the following code example to replace text in a Word document with HTML:

C#

//Open an existing word document 
using (WordDocument document = new WordDocument(@"Sample.docx"))
{
    //Create the temporary word document for HTML.
    using (WordDocument replaceDoc = new WordDocument())
    {
         //Add section for HTML document. 
         IWSection htmlsection = replaceDoc.AddSection();
         //Read HTML string from the file.
         string htmlString = File.ReadAllText(@"File.html");
         //Validate the HTML string.
         bool isValidHtml = htmlsection.Body.IsValidXHTML(htmlString, XHTMLValidationType.Transitional);
         //When the HTML string passes validation, it is inserted into the document.
         if (isValidHtml)
         {
            //Append HTML string in the temporary word document.
            htmlsection.Body.InsertXHTML(htmlString);
         }
         //Replace the placeholder text with content of HTML document.
         document.Replace(new Regex("«([a-zA-Z0-9 ]*:*[a-zA-Z0-9 ]+)»"), replaceDoc, true);
    }
    //Save the Word document.
    document.Save("Result.docx", FormatType.Docx);
}

VB

'Open an existing word document 
Using document As WordDocument = New WordDocument("Sample.docx")
    'Create the temporary word document for HTML.
    Using replaceDoc As WordDocument = New WordDocument()
        'Add section for HTML document. 
        Dim htmlsection As IWSection = replaceDoc.AddSection()
        'Read HTML string from the file.
        Dim htmlString = File.ReadAllText("File.html")
        'Validate the HTML string.
        Dim isValidHtml As Boolean = htmlsection.Body.IsValidXHTML(htmlString, XHTMLValidationType.Transitional)
        'When the HTML string passes validation, it is inserted into the document.
        If isValidHtml Then
            'Append HTML string in the temporary word document.
            htmlsection.Body.InsertXHTML(htmlString)
        End If
        'Replace the placeholder text with content of HTML document.
        document.Replace(New Regex("«([([a-zA-Z0-9 ]*:*[a-zA-Z0-9 ]+)»"), replaceDoc, True)
    End Using
    'Save the Word document.
    document.Save("Result.docx", FormatType.Docx)
End Using

A complete working example to replace text in a Word document with HTML using C# can be downloaded from GitHub.

By executing the application, you will get the output Word document as follows:

Output Word document generated in WinForms

Take a moment to peruse this documentation. You can find basic Word document processing options along with features like mail merge, merge, and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, 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 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?

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

Note:

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

 

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