Mergefield HTML Issue

We are trying to insert HTML during the mailmerge callback event, here is some sample html, this is the field value during mailmerge.
Sample Text
Second Sample Text
Our word template contains a style with the exact same name “My_Custom_Style”,
I was hoping that the Builder.inserthtml function would apply the same style if it exists in the word template, but it seems to be applying the default “Normal” style instead of the custom style.
Please let us know if this is the correct behavior of the inserthtml function, if yes, are there any workarounds to solve this issue, is there a way to get the style name name mentioned in the merge fieldvalue (other than parsing html directly) and applying to the corresponding paragrahs. Please advise.

Hi Umair,

Thanks for your inquiry. DocumentBuilder.InsertHtml method uses the fonts/styles specified in HTML. You can get the document’s style by using following line of code and apply this style to any Paragraphs, Runs etc.

Style style = doc.Styles["My_Custom_Style"];

Please check following code example for your kind reference.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a paragraph style and specify some formatting for it.
Style style = doc.Styles.Add(StyleType.Paragraph, "MyStyle1");
style.Font.Size = 24;
style.Font.Name = "Verdana";
style.ParagraphFormat.SpaceAfter = 12;
// Create a list and make sure the paragraphs that use this style will use this list.
style.ListFormat.List = doc.Lists.Add(ListTemplate.BulletDefault);
style.ListFormat.ListLevelNumber = 0;
// Apply the paragraph style to the current paragraph in the document and add some text.
builder.ParagraphFormat.Style = style;
builder.Writeln("Hello World: MyStyle1, bulleted.");
// Change to a paragraph style that has no list formatting.
builder.ParagraphFormat.Style = doc.Styles["Normal"];
builder.Writeln("Hello World: Normal.");
builder.Document.Save(MyDir + "Lists.ParagraphStyleBulleted Out.doc");

Please let us know if you have any more queries.

Hi,
Yes, the document styles can be accessed via doc.Styles, please look at my sampe html snippet, I am referring to the styles mentioned in the html snippet, how to get them and apply them to the relevant paragraphs / elements in the word document, I’ll post the HTML snippet again.
Sample Text
Second Sample Text
During the mail merge process for this field, I want the paragraph 1 to be using the “My_Custom_Style1” and paragraph 2 to be using “My_Custom_Style2” and so on…
In the mailmerge callback event how do I know that this is paragraph 1 and how do I get the style value of “My_Custom_Style1” from the html snippet ?
Thanks,
Umair

Hi Umair,

Thanks for your inquiry.

Please note that the content inserted by InsertHtml method does not inherit formatting specified in DocumentBuilder options. Whole formatting is taken from HTML snippet. If you insert HTML with no formatting specified then default formatting is used for inserted content, e.g. if font is not specified in your HTML snippet, default font will be applied (Times New Roman).

You can change font of inserted HTML by using following codes snippet. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("some text");
// Set the node changing handler to catch inserted nodes and pass the node range object used to the store
// the nodes are looking for.
doc.NodeChangingCallback = new HandleNodeChanging_FontChanger(doc.Styles["My_Custom_Style"]);
// Insert sample HTML content
builder.InsertHtml("Sample Text HTML"); 
doc.NodeChangingCallback = null;
builder.Writeln("some text");
doc.Save(MyDir + "AsposeOut.docx");
public class HandleNodeChanging_FontChanger: INodeChangingCallback
{
    Style style = null;
    public HandleNodeChanging_FontChanger(Style newStyle)
    {
        style = newStyle;
    }
    // Implement the NodeInserted handler to set default font settings for every Run node inserted into the Document
    void INodeChangingCallback.NodeInserted(NodeChangingArgs args)
    {
        // Change the font of inserted text contained in the Run nodes.
        if (args.Node.NodeType == NodeType.Run)
        {
            Aspose.Words.Font font = ((Run) args.Node).Font;
            font.Size = style.Font.Size;
            font.Name = style.Font.Name;
        }
    }
    void INodeChangingCallback.NodeInserting(NodeChangingArgs args)
    {
        // Do Nothing
    }
    void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)
    {
        // Do Nothing
    }
    void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)
    {
        // Do Nothing
    }
}