How to replace text in a Word document with HTML in ASP.NET Core?
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#:
- Create a new C# console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Framework applications from NuGet.org.
- Include the following namespace in the Program.cs file:
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS; using System.IO; using System.Text.RegularExpressions;
- Use the following code example to replace text in a Word document with HTML:
- Use the following helper method to replace text in the Word document.
C#
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"../../../Data/Sample.docx"), FileMode.Open, FileAccess.ReadWrite)){//Open an existing Word document.using (WordDocument document = new WordDocument(fileStream, FormatType.Automatic)){//Replace the text by finding the word using ReGex.ReplaceTextUsingRegex(document);//Replace the text by finding the word using string.ReplaceTextUsingString(document);//Create file stream.using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Result.docx"), FileMode.Create, FileAccess.ReadWrite)){//Save the Word document to file stream.document.Save(outputFileStream, FormatType.Docx);}}}
C#
private static void ReplaceTextUsingString(WordDocument document){//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(Path.GetFullPath(@"../../../Data/Keyword.html"));//Validate the HTML string.bool isValidHtml = htmlsection.Body.IsValidXHTML(htmlString, XHTMLValidationType.None);//When the HTML string passes validation, it is inserted to 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("<Keyword>", replaceDoc, true, true);}}private static void ReplaceTextUsingRegex(WordDocument document){//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(Path.GetFullPath(@"../../../Data/File.html"));//Validate the HTML string.bool isValidHtml = htmlsection.Body.IsValidXHTML(htmlString, XHTMLValidationType.None);//When the HTML string passes validation, it is inserted to 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);}}
By executing the application, you will get the output Word document as follows:
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?
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.
Conclusion
I hope you enjoyed learning about how to replace text in a Word document with HTML in AP.NET Core.
You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!