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

Free Support Forum - aspose.com

HTML mail merge changes font

Hi,

I have noticed a problem in mail merge of html content on a .doc template

I have attached two files:

  • definizioni_it.doc, which is the base template
  • report_mod_1_2_1.doc, which is the compiled result

as you can notice, the template is all in “Arial” format, which is kept in the mail merge of the two regions and also of the title, but when merging the two html_* fields, the font is not preserved.

Here is the code I use to perform html mail merge:

public void fieldMerging(FieldMergingArgs fma) throws Exception {
    if (fma.getDocumentFieldName().startsWith("html_") && fma.getFieldValue() != null) {
        DocumentBuilder builder = new DocumentBuilder(fma.getDocument());
        builder.moveToMergeField(fma.getDocumentFieldName());
        builder.insertHtml(fma.getFieldValue().toString());
        fma.setText("");
    }
}

Am I doing something wrong? I am currently using Aspose.Words for Java version 14.5

Hi Matteo,

Thanks for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v14.6.0) from here and let us know how it goes on your side.

If the problem still remains, Please attach your input html for html_testo_definizioni and html_riferimenti_normativi fields here for testing. I will investigate the issue on my side and provide you more information.

Hi,

as I said in my previous post, I am currently using version 14.5, and currently I cannot update since our subscription expired.

Here is the content for html_testo_definizioni:

Ai fini e agli effetti delle disposizioni di cui al presente decreto legislativo si intende per:

and here is html_riferimenti_normativi:

Vengono sotto riportate in modo non esaustivo le principali norme prese in considerazione nella redazione del presente documento.

Tali normative sono state considerate come riferimenti guida per il controllo delle condizioni necessarie e sufficienti a garantire la protezione dei lavoratori e la loro tutela dai rischi potenziali ed effettivi presenti nei luoghi di lavoro.

Would you be so kind to make the test for us, with our current version and with the latest?

Quick update: I have verified using version 14.6 and the problem is there too.

Can this be solved in our Java code or template? Or do we need to wait for a fix in the library in a next release?

Hi Matteo,

Thanks for sharing the detail.

Please note that the 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.

You may use InsertHtmlWithBuilderFormatting instead of the InsertHtml method as shown in following code example. I have attached the code related to InsertHtmlWithBuilderFormatting with this post. Hope this helps you.

Document doc = new Document(MyDir + "definizioni_it.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToMergeField("html_testo_definizioni");
DocumentBuilderHelper helper = new DocumentBuilderHelper(builder);
helper.insertHtmlWithBuilderFormatting("Ai fini e agli <b>effetti delle</b> disposizioni di cui al presente decreto legislativo si intende per:");
doc.save(MyDir + "Out.docx");

Hi,

thanks for the code snippet, I integrated it in our code, and it worked perfectly.

To bring this to the next level, would there be a way to have it merge html using the document font only if no font is specified in the html snippet itself? Or would this be to difficult to determine?

Thanks again.

Hi Matteo,

Thanks for your suggestions.

This seems possible now. In recent version of Aspose.Words (14.6.0), we have introduced a new overload of DocumentBuilder.InsertHtml method which allows you to choose what formatting will be used as a base for inserted HTML fragments.

The new overload has an argument useBuilderFormatting which when is false, formatting specified in DocumentBuilder is ignored, and formatting of inserted text is based on default HTML formatting. In this case, inserted text looks as in browsers.

When useBuilderFormatting is true, formatting of inserted text is based on formatting specified in DocumentBuilder. Note that useBuilderFormatting chooses only base formatting of inserted text, and do not affect formatting directly specified in the HTML fragment.

The following example illustrates the difference between the two modes:

DocumentBuilder builder = new DocumentBuilder();
builder.getParagraphFormat().setLeftIndent(72);
builder.getFont().setName("Arial");
builder.getFont().setSize(24);
boolean useBuilderFormatting = ...;
builder.insertHtml("<b>Text</b>", useBuilderFormatting);

In this example, if useBuilderFormatting is false, the inserted paragraph will have no left indent and will use the ‘Times New Roman’ 12pt font, which is the default HTML font and indent. If useBuilderFormatting is true, the inserted paragraph will be indented by 1 inch (72 points) and will use the ‘Arial’ 24pt font, as specified in DocumentBuilder. However, in both cases the inserted text will be bold and red, as specified in the HTML fragment.

Best regards,

Hi,

that is a nice feature indeed!

What would happen if useBuilderFormatting was true but at the same time the HTML snippet specified a font in its style? Who would “win” in such case?

For example, you could have this HTML snippet:

Some text here …

Hi Matteo,

Thanks for your inquiry.

When useBuilderFormatting is false, DocumentBuilder formating is ignored and formatting of inserted text is based on default HTML formatting. As a result, the text looks as it is rendered in browsers.

When useBuilderFormatting is true, formatting of inserted text is based on DocumentBuilder formatting, and the text looks as if it were inserted with Write.