How to add styling with mailMerge insertHtml()?

Hi ,

I’m am currently busy with an implementation of the aspose mailmerge combined with the insert HTML method .

Currently when i insert html i get a blank rule/line between different HTML tags .
As an example i insert the following HTML :

<H4>header</H4>
<p>tekst</p>
<ul>
<span class="Apple-tab-span" style="white-space:pre">	</span><li>list item</li>
</ul>

the output is as followed :

header
tekst
-listitem

I would like to remove those empty newlines. How can i manipulate ,HTML styling during mailMerge?
I am already using a .dotx file for fontstyling.

Kind regards,
Eric

Hi

Thanks for your request. I think in your case you can try using FieldMergingCallback, please see the following link to learn more:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/inserthtml/
Best regards,

Hi ,

thanks for your quick reply ,

currently i’m using the movetoMergeField() and then the insertHTML() method
and i don’t use the execute method , so my question is , does this will fire the callback event ?

kind regards ,

Eric

Hi Eric,
Thanks for this additional information.
In this case then, no the FieldMergingCallback will never be called. Could you please post the code you are using here and we will take a closer look.
Thanks,

Hi ,

the method looks as followed

private Document FillSingleDocument(Content content)
{
    Document xdoc = new Document(baseDir + content.template); //creating document with dotx file
    DocumentBuilder db = new DocumentBuilder(xdoc);

    foreach(IField field in content.getFields())
    {
        string name; //fieldname string
        string value; //html string

        field.Merge(out name, out value); //extracting the values from field objects

        db.MoveToMergeField(name);
        db.InsertHtml(value);
    }
    return xdoc;
}

i’ve also included a simple application where you can see the bigger picture of the application .
because i don’t want any space between list items and headerElements and textElements

kind regards,
Eric

Hi Eric,
Thanks for attaching your project here.
I have taken a look and it appears that the spacing right now isthe correct behaviour right now. There are no extra paragraph being inserted between the content. Let me explain:
You are loading a template file and inheriting the styles and settings from this template when creating your new document. When the content is inserted from HTML, the line spacing is inherited from the template, which seems to have very large line spacing set for these styles. You can see from the screenshot below taken using the template that the line spacing is large.
``
Depending upon how you would like to go about this, you could either

  • If you don’t want these settings for any of the styles in the document then you should change the the default settings in the template itself.
  • If you only want the line spacing ignored for inserted content then you use an implementation of the NodeChangedEventHandler similar to this post here. In your case you would want to change the line spacing of any inserted content.

If you have any further queries please feel free to ask.
Thanks,

Hi ,

Thanks i implemented the styling true the templates wich was originally the purpose of using the template and we are testing it now.
I was wondering if i also can apply some List styling through the template?
Or is list styling best practiced with modyfying the HTML.
I have also got a question about .eps rendering but ill start a new forum post for that quality question.

Kind regards ,

Eric

Hi Eric,
Sure, you can customise your list styling in the template the same as other regular styles. This article here might be some help.
If you have any further queries then please feel free to ask.
Thanks,

Hi ,

We still have problems with the space after ,
when we set the spacing from a H1 quickstyle to 0 it works the text gets to be placed directly under , but when set the h4 tag to 0 it wil just add a linebreak wich is not wanted.
I’ve included a simpel console app with the template ,we would like to define all our styling within the template but when we render it always puts an linebreak after the header.

kind regards,
Eric

Hi

Thanks for your inquiry. It is not line break it is spacing. Please try specifying spacing in your HTML, like shown below. This should help you to resolve the problem:

Content c3 = new Content();
c3.template = "textPage.dotx";
c3.addField(new TextField("headerTitle", " "));
c3.addField(new TextField("Title", "Header"));
c3.addField(new TextField("pageContent", "<h4 style='margin:0pt;'>content <p align='left' style='margin:0pt;'>this is some lorem ipsum")); 

Hope this helps.
Best regards,

Hi ,

Many thanks the problem is solved now

Kind regards,
Eric

Hi Andrey ,

Regarding this solution , with adding inline css.
When using html list like

this need to be alligned left and should have no indent .
I’ve tried
however it keeps having a tab ore indent .
What html transformation do i need to apply for list margins ?

Kind regards,
Eric

Hi

Thanks for your inquiry. Please try specifying margins in LI element, like shown below:

<html>
<body>
    <ul>
        <li style='margin-left:18pt;'>test</li>
        <li style='margin-left:18pt;'>test</li>
        <li style='margin-left:18pt;'>test</li>
        <li style='margin-left:18pt;'>test</li>
    </ul>
</body>
</html>

Hope this helps.
Best regards,

Hi ,

this solution ,works however it has two other effects

  1. the tekst stays fixed
  2. The UL type square is beeing ignored so it wil have a normal circle bullet

I’ve added the pdf , on page 4 u see the result.
The code where the input html is transformed looks like:

tf.fieldValue = value.Replace("<ul>", @"<ul type='square'>");
tf.fieldValue = value.Replace("<li>", @"<li style='margin-left:18pt;'>");

I’ve also added a simpel application , the transformation is found in the content class

Kind regards ,
Eric

Hi Eric,

Thanks for your inquiry.

  1. I am not sure I properly got your question. Could you be more specific?
  2. I tested with the following HTML and bullets are properly imported from HTML.
<html>
<body>
    <ul type="square">
        <li>test</li>
        <li>test</li>
        <li>test</li>
    </ul>
</body>
</html>

Best regards.

Hi andrey ,

I’ve added an image and a document with the styling we want .
The problem is that we can’t get the correct styling for our list items:

The requirements are :

  • Bullets needs to be square
  • Bullets are alligend with the text and are not indented
  • The space between the bullets and the text behind it needs to be smaller
  • A second List should have the same styling and indent

To get the result from the current list we use :

kind regards,
Eric

Hi

Thank you for additional information. Maybe in your case, you should consider post-processing the document after inserting HTML. For instance, please see the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
string html = File.ReadAllText(@"Test001\test.html");
// Insert HTML into the document.
builder.InsertHtml(html);
// Reset indents of list items.
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach(Paragraph paragraph in paragraphs)
{
    if (paragraph.IsListItem)
    {
        paragraph.ListFormat.ListLevel.NumberPosition = 0;
        paragraph.ListFormat.ListLevel.TextPosition = 15;
        paragraph.ListFormat.ListLevel.TabPosition = 15;
        // If paragraph is an item of bulleted list, we should specify bullet style square.
        if (paragraph.ListFormat.ListLevel.NumberStyle == NumberStyle.Bullet)
            paragraph.ListFormat.ListLevel.NumberFormat = "\x00A7";
    }
}
doc.Save(@"Test001\out.doc");

Hope this helps.
Best regards,

Hi ,

This solution of post processing works like a charm !
Many thanks for this solution.

Kind regards ,

Eric