We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

HTML <UL> tag resets formatting

Pretty simple HTML string:


  • Bullet 1

  • Bullet 2

The

    tag resets the format where the merge tag is placed in the template document. The same behavior if using a

    tag. With the

    tag I just wrote a simpe method that removed the opening and closing tag.

    However, it seems not that simplw with the

      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, "

some text

");


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

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

    tag. Currently I am removing the
      tag and replacing the
    • tags with a
      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”);<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);

doc.MailMerge.FieldMergingCallback = new Test30();

doc.MailMerge.Execute(

new string[] { "Answer9" },

new object[] { "

  • Bullet 1
  • Bullet 2
" });

doc.Save(MyDir + "Out.docx");

private class Test30 : IFieldMergingCallback

{

///

/// This is called when merge field is actually merged with data in the document.

///

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("

"

))

{

rawHTML = rawHTML.Replace("

"

, "");

rawHTML = rawHTML.Replace("

"

, "");

rawHTML = rawHTML.Replace("

", "

"
);

rawHTML = rawHTML.Replace("

", "

"
);

builder.InsertHtml(rawHTML);

}

else if (rawHTML.ToLower().Contains("<p "))

{

rawHTML = rawHTML.Replace("<p ", "<span ");

rawHTML = rawHTML.Replace("<P ", "<span ");

rawHTML = rawHTML.Replace("

", "

");

rawHTML = rawHTML.Replace("

", "
");

builder.InsertHtml(rawHTML);

}

else if (rawHTML.ToLower().Contains("ul"))

{

//rawHTML = rawHTML.Replace("

    ", "");

//rawHTML = rawHTML.Replace("

  • ", "• ");
  • //rawHTML = rawHTML.Replace("", "
    ");

    //rawHTML = rawHTML.Replace(" ", "");

    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.

    }

    }