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.
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
to Run nodes by using Character Styles e.g. a Glyph Style,
to the parent of those Run nodes i.e. a Paragraph node (possibly via paragraph Styles)
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);
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 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);
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.