Font style is not taken from template word document

Hi,

We create a word document from html file using Aspose.Word(17.2). When processing (append the Html to Word document) the font style is obtained from html document. But the font style should obtain from template word file. I have attached the files.

Could you please suggest us?

Thanks.

Hi Mani,

Thanks for your inquiry. Please note that formatting is applied on a few different levels. For example, let’s consider formatting of simple text. Text in documents is represented by Run element and a Run can only be a child of a Paragraph. You can apply formatting

  1. to Run nodes by using Character Styles e.g. a Glyph Style,
  2. to the parent of those Run nodes i.e. a Paragraph node (possibly via paragraph Styles)
  3. you can also apply direct formatting to Run nodes by using Run attributes (Font). In this case the Run will inherit formatting of Paragraph Style, a Glyph Style and then direct formatting.

In your case we suggest you please remove the formatting applied to Run nodes and use ImportFormatMode.UseDestinationStyles in Document.AppendDocument method. Please read the following article:
Differences between ImportFormat Modes

Document document = new Document(strTemplateFile);
Document docHtml = new Document(strHtmlFile);
document.AppendDocument(docHtml, ImportFormatMode.UseDestinationStyles);
document.UpdateFields();
document.UpdateTableLayout();
foreach (Paragraph paragraph in document.GetChildNodes(NodeType.Paragraph, true))
{
    foreach (Run run in paragraph.Runs)
    {
        run.Font.ClearFormatting();
    }
}
document.Save(strResultFile, SaveFormat.Docx);

Hi Tahir Manzoor,

Thanks for your quick response. I have tried your solution. Now I have get the template font in merged word document But in my html file, I have some special symbol(arrows) with “Wingdings” font. Those arrows are missing now.
How to retain special symbol in merged document?

Thanks.

Hi Mani,

Thanks for your inquiry. In this case, we suggest you please do not clear the font formatting of special symbol (arrows) with “Wingdings” font. Please check the highlighted code snippet below. Hope this helps you.

string strTemplateFile = "C:\Temp\Template(Title).doc";
string strHtmlFile = "C:\\Temp\\Html1.html";
string strResultFile = "C:\\Temp\\output.doc";
Document document = new Document(strTemplateFile);
Document docHtml = new Document(strHtmlFile);
document.AppendDocument(docHtml, ImportFormatMode.KeepSourceFormatting);
document.UpdateFields();
document.UpdateTableLayout();
foreach (Paragraph paragraph in document.GetChildNodes(NodeType.Paragraph, true))
{
    foreach (Run run in paragraph.Runs)
    {
        if (run.Font.Name != "Wingdings")
            run.Font.ClearFormatting();
    }
}
document.Save(strResultFile, SaveFormat.Docx);

Hi Tahir Manzoor,

Thanks for your response.

Thanks.

Hi Tahir Manzoor,

I have an another question, I need to update style class while converting Html to Word using Aspose word.

Hi Mani,

Thanks for your inquiry. Please use ParagraphFormat.StyleName property to set the name of the paragraph style applied to this formatting. We suggest you please reset to default paragraph formatting by using ParagraphFormat.ClearFormatting method. Hope this helps you.

Paragraph paragraph = (Paragraph)doc.GetChild(NodeType.Paragraph, 0, true);
paragraph.ParagraphFormat.ClearFormatting();
paragraph.ParagraphBreakFont.ClearFormatting();
paragraph.ParagraphFormat.StyleName = "StyleName";

Hi Tahir Manzoor,

Thanks for your response.

Thanks.

Hi Mani,

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.