Setting font style when inserting HTML to Word document

Dear friends at Syncfusion,
in our use case, we get input from users using Syncfusion Rich Text Editor. We save the xhtml in db and later want to insert it into a word document, where it replaces specific tags. We accomplish it using following code:

      public void SetHTMLByTag(WordDocument templateForInstering, String Tag)
        {
            using (WordDocument replaceDoc = new WordDocument())
            {
                IWSection htmlsection = replaceDoc.AddSection();
                string htmlstring = this.GetStringValueByTag(Tag);
                htmlstring = Tools.ReplaceBrs(htmlstring);
                htmlsection.Body.InsertXHTML(htmlstring);
                templateForInstering.Replace(Tag, replaceDoc, false, false);
                replaceDoc.Close();
            }
        }

This works great. 
However, we cannot find a way to set a font style for the inserted html (we would like to use calibri).
Is it possible to accomplish this somehow? 

Any help would be greatly appreciated.
Thank you in advance,
Petr

2 Replies 1 reply marked as answer

MR Manikandan Ravichandran Syncfusion Team August 28, 2020 05:55 PM UTC

Hi Petr,

Thank you for contacting Syncfusion support.

For applying font name to the text, after appending the HTML to Word document, we suggest you to iterate each body item from the textbody part and then applies the font properties to the text ranges. We have prepared the sample application to meet your end requirement. It can be downloaded from the following link:
https://www.syncfusion.com/downloads/support/forum/157365/ze/FormatHTML702922820

We have done the following things in the sample:
  1. Creates a new Word document instance.
  2. Add new section to the Word document.
  4. Appends HTML string into newly created body of section.
  5. Iterates each body item from the textbody part and sets formatting to the text range.
  6. Saves the Word document.

Please let us know if you have any other questions.


Regards,
Manikandan Ravichandran 


Marked as answer

PE Petr September 11, 2020 08:10 AM UTC

Dear Manikandan,
Thank you very much for your help, this works very well!

Best regards,
Petr



Loader.
Up arrow icon