HTML <UL> tag resets formatting

Pretty simple HTML string:

<UL>
<LI>Bullet 1</LI>
<LI>Bullet 2</LI>
</UL>

The <ul> tag resets the format where the merge tag is placed in the template document. The same behavior if using a <P> tag. With the <P> tag I just wrote a simpe method that removed the opening and closing tag.</P>
However, it seems not that simplw with the <UL> tag.
Any ideas?

Hi James,

Thanks for your inquiry. Please note that, content inserted by DocumentBuilder.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.

I suggest you please use InsertHtmlWithBuilderFormatting instead of the InsertHtml method as shown in following code snippet. I have attached the code related to InsertHtmlWithBuilderFormatting with this post.

// builder is DocumentBuilder object
InsertHtmlWithBuilderFormatting(builder, "<p>some text</p>");

If the problem still remains, please attach your input Word document here along with code for testing. I will investigate the issue on my side and provide you more information.

I tried the InsertHtmlWithBuilderFormatting method and no chages.
I had one of your support people tell me to remove the <p> tag from the html string and that worked. The merge tag format did not get reset.
However, I have not found a good work-around for the <UL> tag. Currently I am removing the <UL> tag and replacing the <LI> tags with a <BR> and the &bull. It’s an ugly fix for the moment.
I have attached the word template, the the word document after the merge and the c# code for the recommended method as well as the code to feed the method.
Thanks

Hi James,

Thanks for sharing the document. I have worked with your documents and have not found any formatting issue with output document. Please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate, how you want your final Word output be generated like. We will then provide you more information on this along with code.

I uploaded three files already.
One was the Word template with the merge fields
One was the Word document after the merge
And the final was the class.
Can you use the Word template document as it has the position of the merge field.
James

Hi James,

Thanks for sharing the detail. I am working over your query and will update you asap.

Thanks for the update.

Hi James,

Thanks for your patience. You can achieve your requirements by using following code snippet. Hope this helps you. Please let us know if you have any more queries.

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
{
    if (e.DocumentFieldName.StartsWith("Answer9"))
    {
        // Insert the text for this merge field as HTML data, using DocumentBuilder.
        DocumentBuilder builder = new DocumentBuilder(e.Document);
        if (e.Field.Start.PreviousSibling != null && e.Field.Start.PreviousSibling.NodeType == NodeType.Run)
            if (((Run) e.Field.Start.PreviousSibling).Text.Contains(ControlChar.LineBreak))
                builder.MoveTo(e.Field.Start.PreviousSibling);
        e.Field.Remove();
        builder.StartBookmark("bm");
        builder.InsertHtml(e.FieldValue.ToString());
        builder.EndBookmark("bm");
        e.Text = "";
        if (e.FieldValue.ToString().ToLower().Contains("ul"))
        {
            builder.MoveToBookmark("bm", true, false);
            Node currentNode = builder.CurrentNode.ParentNode;
            while (currentNode.NodeType != NodeType.BookmarkEnd)
            {
                if (currentNode.NodeType == NodeType.Paragraph)
                {
                    Paragraph para = (Paragraph) currentNode;
                    if (para.ListFormat.IsListItem)
                    {
                        para.ParagraphFormat.LeftIndent = 60;
                    }
                }
                currentNode = currentNode.NextPreOrder(e.Document);
            }
        }
    }
}

I’m not sure where to put the snippet you provided me.
Cam you give me some direction.
Thanks
James

Hi James,
Thanks for your inquiry. You can use the shared code inside else if (rawHTML.ToLower().Contains("ul")).
Please check the following complete example to insert html (the UL tag) at mail merge filed’s position. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "MailMerge-Test.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
 
doc.MailMerge.FieldMergingCallback = new Test30();
doc.MailMerge.Execute(     new string[] { "Answer9" },     new object[] { "<UL><LI>Bullet 1</LI><LI>Bullet 2</LI></UL>" });
 
doc.Save(MyDir + "Out.docx");
private class Test30 : IFieldMergingCallback
{
    /// <summary>
    /// This is called when merge field is actually merged with data in the document.
    /// </summary>
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        // All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'.
        if (e.DocumentFieldName.StartsWith("Answer9"))
        {
            // Insert the text for this merge field as HTML data, using DocumentBuilder.
            DocumentBuilder builder = new DocumentBuilder(e.Document);

            string rawHTML = "";
            if (e.FieldValue != null)
            {
                rawHTML = e.FieldValue.ToString();

                if (rawHTML.ToLower().Contains("<p>"))
                {
                    rawHTML = rawHTML.Replace("<p>", "");
                    rawHTML = rawHTML.Replace("<P>", "");

                    rawHTML = rawHTML.Replace("</p>", "<br /><br />");
                    rawHTML = rawHTML.Replace("</P>", "<br /><br />");
                    builder.InsertHtml(rawHTML);
                }

                else if (rawHTML.ToLower().Contains("<p "))
                {
                    rawHTML = rawHTML.Replace("<p ", ");
                    rawHTML = rawHTML.Replace("<P ", ");
                    rawHTML = rawHTML.Replace("</p>", "<br /><br />");
                    rawHTML = rawHTML.Replace("</P>", "<br /><br />");
                    builder.InsertHtml(rawHTML);
                }
                else if (rawHTML.ToLower().Contains("ul"))
                {
                    //rawHTML = rawHTML.Replace("<UL> ", "");
                    //rawHTML = rawHTML.Replace("<LI>", "&bull;&nbsp;");
                    //rawHTML = rawHTML.Replace("</LI>", "<br />");
                    //rawHTML = rawHTML.Replace("</UL>", "");

                    if (e.Field.Start.PreviousSibling != null && e.Field.Start.PreviousSibling.NodeType == NodeType.Run)
                        if (((Run)e.Field.Start.PreviousSibling).Text.Contains(ControlChar.LineBreak))
                            builder.MoveTo(e.Field.Start.PreviousSibling);

                    e.Field.Remove();
                    builder.StartBookmark("bm");

                    builder.InsertHtml(e.FieldValue.ToString());
                    builder.EndBookmark("bm");
                    e.Text = "";

                    if (e.FieldValue.ToString().ToLower().Contains("ul"))
                    {
                        builder.MoveToBookmark("bm", true, false);
                        Node currentNode = builder.CurrentNode.ParentNode;

                        while (currentNode.NodeType != NodeType.BookmarkEnd)
                        {
                            if (currentNode.NodeType == NodeType.Paragraph)
                            {
                                Paragraph para = (Paragraph)currentNode;
                                if (para.ListFormat.IsListItem)
                                {
                                    para.ParagraphFormat.LeftIndent = 60;
                                }
                            }
                            currentNode = currentNode.NextPreOrder(e.Document);
                        }
                    }
                }

                //InsertHtmlWithBuilderFormatting(builder, rawHTML);

                e.Text = "";
            }
        }
    }

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // Do nothing.
    }
}