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

How to render text with HTML tags to Formatted text in a Word document

Hello,


I have a string text = "<b>Hello</b>". 

How is it possible to insert it into a Word document using DocIO to display as Hello instead? 


I have a string from the database, which contains various HTML tags such as <p>, <strong>, <b> and so on which I need to keep formatted in my Word document file.


I am inserting a new Paragraph into the document and setting the .Text property to the value of my string text, but the HTML tags won't get formatted.


Thank you.


2 Replies 1 reply marked as answer

SB Suriya Balamurugan Syncfusion Team October 31, 2022 01:52 PM

Hi John,

From the given details, we have found that your requirement is to insert an HTML string with formatting in a Word document using DocIO.

To achieve your requirement, we suggest you to use InsertXHTML or AppendHTML method to insert or add the HTML string into a Word document as per your requirement. Please refer the below code snippet to achieve your requirement,

//Create new Word document.

WordDocument document = new WordDocument();

//Add a section and a paragraph in new Word document.

document.EnsureMinimal();

//Html string to be inserted.

string htmlstring = "<b>Hello</b>";

//Appends the Html string to first paragraph in the document.

document.Sections[0].Body.Paragraphs[0].AppendHTML(htmlstring);

//Save the Word document.

FileStream outputStream = new FileStream(@"Output.docx", FileMode.Create);

document.Save(outputStream, FormatType.Docx);

//Close the instance.

outputStream.Close();

document.Close();


Please refer our UG documentation link to know how to insert/add HTML string into a Word document,
https://help.syncfusion.com/file-formats/docio/html#customizing-the-html-to-word-conversion

Regards,
Suriya Balamurugan.

If this post is helpful, please consider accepting it as the solution so that other members can locate it more quickly.


Marked as answer

JJ John Johnson November 1, 2022 07:51 AM

Thanks. That worked!


Loader.
Live Chat Icon For mobile
Up arrow icon