insertHtml resets font style to Times new roman 12pt

Hello,

We tried to update Aspose in our application to version 14.6.0 and 14.7.0 but we have an issue with insertHtml. We wanted to update to the new version to fix the problem:

WORDSNET-10163 Normal style’s font size is not applied to text inside Table when importing HTML using InsertHtml method

However, what we are seeing now is that the normal text outside of tables keeps getting reset to Times new roman 12pt. After some research I found that the following issue is the cause:

WORDSNET-9421 InsertHtml method resets paragraph’s style to 'Normal’

see: Inserting a text to a paragraph does not retain its style anymore in version 13.11.0

The issue WORDSNET-9421 does not make sense as you seem to have fixed two other issues which do the exact opposite of issue WORDSNET-9421. They both apply the style from the word document to the inserted html. See:

WORDSNET-10163 Normal style’s font size is not applied to text inside Table when importing HTML using InsertHtml method

WORDSNET-9571 InsertHtml method changes formatting of Heading1 paragraph

Even if we apply the MERGEFORMAT to the mergefields that get html inserted into them the style keeps resetting to Times new roman 12pt.

As our customers create their own templates this is unacceptable. The font and height of the inserted html should match the normal style in their templates and not get reset.

Please undo the issue WORDSNET-9421 or provide a reasonable workaround.

Hi Bart,

Thanks for your inquiry. 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.ParagraphFormat.LeftIndent = 72;
builder.Font.Name = "Arial";
builder.Font.Size = 24;

bool 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.

You can try using this method and in case the problem still remains, please attach your input/output documents and complete source code here for testing. We will investigate the issue on our end and provide you more information.

Best regards,

Hello,

Thank you for your reply. Where can I find the api changes for new releases? Shouldn’t this be in the release notes?

The usage you provided as options does not cover our scenario. We want to insertHtml with the default style from the word document where the html is getting merged into. We don’t want to use the builder’s or the html’s formatting. The client creates his own word document with his own style and layout and our application merges the html into mergefields where he wants with the default word style he formatted in the word document. The only thing that was taken from the HTML is whether or not the text should be bold, underlined, lists, tables, … etc. But the font and height is always taken from the word document’s default style.

This worked fine up to 14.6.0. Can you provide me with a way to solve my issue?

Hi Bart,

Thanks for your inquiry. For API changes, please refer to the forllowing page:
https://docs.aspose.com/words/java/

Bart:
The usage you provided as options does not cover our scenario. We want to insertHtml with the default style from the word document where the html is getting merged into. We don’t want to use the builder’s or the html’s formatting. The client creates his own word document with his own style and layout and our application merges the html into mergefields where he wants with the default word style he formatted in the word document. The only thing that was taken from the HTML is whether or not the text should be bold, underlined, lists, tables, … etc. But the font and height is always taken from the word document’s default style.
Please attach the following resources here for testing:

Your input Word document.
Your HTML file
Aspose.Words generated output Word document showing the undesired behavior
Your expected Word document which shows the correct final output. Please create this document using Microsoft Word.
Please create a standalone (runnable) console application that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information.

Best regards,

Hello,

I created a project which shows what I explained above.

Thanks.

Hi Bart,

Thanks for the additional information. I tested the scenario and have managed to reproduce the same problem on my side. For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-10643. Our development team will further look into the details of this problem and we will keep you updated on the status of correction. We apologize for your inconvenience.

Best regards,

Hi Bart,

Thanks for being patient. Regarding WORDSNET-10643, our development team has completed the analysis of your issue and has come to a conclusion that this issue and the undesired behaviour you’re observing is actually not a bug. So, we will close this issue as ‘Not a Bug’.

You should use InsertHtml with useBuilderFormatting=true you want to insert HTML fragments with document’s default styles. Please see attached document generated using your code with useBuilderFormatting=true.

In this document, all the text inserted with InsertHtml is in Arial 14pt (taken from the template document’s Normal style). The resulting document looks even better than “out-awjava-14.5.0.doc”, in which the size of inserted text is incorrectly set to 12pt.

Best regards,

Hello,

This does indeed work but it seems to work different than before. It copies more formatting than it used to. Now if I use a label before a mergefield and put the label in bold the merged text will also be in bold.

Another issue is when using an html unordered list or ordered list the bullets and numbers’ font is wrong. It always resets to 12pt.

See attached example:

With the first mergefield I just merge text as normal where you can see the bullets and numbers have the wrong font height.
With the second mergefield I use a before switch on the mergefield with a label which I put in bold. All the html gets merged in bold. This used to work as expected. Only the label would be bold and the merged html would maintain the normal style.
EDIT
It seems the bullets and numbers font height is solved in Aspose 14.7.0, I was using 14.6.0. The bold before however is still a problem.

In my main project I am using the following snippet to insert the labels before these mergefields:

if (args.getField().getFieldCode().contains("\b"))
{
    String textBefore = args.getField().getResult();
    textBefore = textBefore.replace(args.getFieldName(), "");
    textBefore = textBefore.replace("«»", "");
    AsposeDocumentBuilderHelper.InsertTextWithStyleFormatting(builder, textBefore, "Tekstvaklabel");
    builder.insertHtml((String)args.getFieldValue(), true);
}
else
{
    builder.insertHtml((String)args.getFieldValue(), true);
}
/**
* Inserts an HTML snippet into document using a combination of the current formatting from the supplied DocumentBuilder object
* and the input HTML.
* @param html
*/
public void InsertTextWithStyleFormatting(String text, String style)
{
    try
    {
        Document doc = documentBuilder.getDocument();
        StyleCollection styles = doc.getStyles();
        Style foundStyle = styles.get(style);
        if (foundStyle != null)
        {
            Font currentFont = documentBuilder.getFont();
            Font styleFont = foundStyle.getFont();
            currentFont.setBold(styleFont.getBold());
            currentFont.setColor(styleFont.getColor());
            currentFont.setItalic(styleFont.getItalic());
            currentFont.setName(styleFont.getName());
            currentFont.setSize(styleFont.getSize());
            currentFont.setUnderline(styleFont.getUnderline());
            documentBuilder.getParagraphFormat().setStyle(foundStyle);
            documentBuilder.writeln(text);
        }
        else
        {
            documentBuilder.writeln(text);
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

Thanks,

Bart

Hi Bart,

Thanks
for your inquiry. We are working over your query and will update you asap.

Hi Bart,

Thanks
for your patience.

bvr:

With the first mergefield I just merge text as normal where you can see the bullets and numbers have the wrong font height.
It seems the bullets and numbers font height is solved in Aspose 14.7.0, I was using 14.6.0. The bold before however is still a problem.

I have tested the scenario using latest version of Aspose.Words for Java 14.7.0 and have not found this issue.

bvr:

With the second mergefield I use a before switch on the mergefield with a label which I put in bold. All the html gets merged in bold. This used to work as expected. Only the label would be bold and the merged html would maintain the normal style.

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

In this case, the behaviour of Aspose.Words is correct. When you move the cursor to the mail merge field { MERGEFIELD htmlvalue \b “I am a label” * MERGEFORMAT }, the DocumentBuilder formatting for bold is true. This is the reason you are getting bold formatting in output document. To avoid this, you can set the DocumentBuilder’s font formatting before using insertHtml method.

I have modified your code. Please check the following highlighted code snippet and let us know if you have any more queries.

public void fieldMerging(FieldMergingArgs args) throws Exception
{
    if (args.getFieldName().equals("htmlvalue"))
    {
        // Insert the text for this merge field as HTML data, using DocumentBuilder.
        DocumentBuilder builder = new DocumentBuilder(args.getDocument());
        System.*out*.println("Before moving the cursor : " + builder.getFont().getBold());
        builder.moveToMergeField(args.getDocumentFieldName(), true, false);
        if (args.getFieldValue() != null)
        {
            System.*out*.println("After moving the cursor : " + builder.getFont().getBold());
            if(args.getField().getFieldCode().contains("\\b"))
            {
                builder.getFont().setBold(false);
                builder.getParagraphFormat().getStyle().getFont().setBold(false);
            }
            builder.insertHtml((String) args.getFieldValue(), true);
            // The HTML text itself should not be inserted.
            // We have already inserted it as an HTML.
            args.setText("");
        }
    }
}

Hello,

Sorry for the late reply and thank you for your suggestion but that does not cover our scenario. We are inserting the text before the mergefield with a predefined Word style which our customers can format themselves. So we don’t know what the formatting for this style will be up front. Removing it hardcoded seems like a very ineffecient solution to fix this. It will also cause all the html formatted in bold to be removed. The html merged into the document is also dynamic and gets inputted by customers in a WYSIWYG editor in our application.

My question is, how come this works just fine in our application with Aspose version 13.12.0 and with the upgrade this breaks? Is it not possible anymore to apply a style to specific text in a paragraph? Adding an extra paragraph to insert the before text into and applying the style to that paragraph isn’t the solution we want because not all of our customers want the text before the html to be in a seperate paragraph.

Thanks,

Bart

Hi Bart,

Thanks
for your inquiry.

bvr:

We are inserting the text before the mergefield with a predefined Word style which our customers can format themselves. So we don’t know what the formatting for this style will be up front.

In this case, you need to modify your code according to formatting of mail merge field in your template documents. Could you please share your template documents here for our reference? We will then provide you more information about your query along with code.

bvr:

My question is, how come this works just fine in our application with Aspose version 13.12.0 and with the upgrade this breaks?

The older version also does not produce the correct output. The output is : I am a labelMy value

bvr:

Is it not possible anymore to apply a style to specific text in a paragraph? Adding an extra paragraph to insert the before text into and applying the style to that paragraph isn’t the solution we want because not all of our customers want the text before the html to be in a separate paragraph.

Yes, you can achieve this by implementing IFieldMergingCallback. Please set the Paragraph Formatting inside fieldMerging method. Please move the cursor to the mail merge field and insert the contents according to your requirements.

If you still face problem, please share you input document and expected output documents 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.

Hello,

See the attached project:
As you can see 13.12.0 does produce the correct output (See data/Output-13.12.0.doc)
As you can see 14.6.0 with useBuilderFormatting false does not produce the correct output. The label style is correct but the html inserted is in the incorrect font etc. (See data/Output-14.6.0-useBuilderFormatting-False.doc)
As you can see 14.6.0 with useBuilderFormatting true does not produce the correct output. It applies the style that should only be applied to the Label to all the inserted html. (See data/Output-14.6.0-useBuilderFormatting-True.doc)
Considering we have over 60 customers using our application with 5-10 templates for each customer we want to reduce the amount of changes required to the templates as much as possible.

Thanks,

Bart

Hi Bart,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. If you perform a simple mail merge with your input document using MS Word, you will get the same output. If you want to control how data is inserted into merge fields during a mail merge operation, you need to implement IFieldMergingCallback interface. As you are inserting HTML into your document, you need to implement this interface.

In your case, I suggest please clear the font formatting using Font.clearFormatting and ParagraphFormat.ClearFormatting methods inside fieldMerging. After clearing the font formatting, set the font formatting of DocumentBuilder according to your requirements.

The DocumentBuilder.insertHtml method works as expected with useBuilderFormatting parameter either it is false/true. If useBuilderFormatting is true, you need to set font formatting of DocumentBuilder before calling insertHtml method.

Hope this answers your query. Please let us know if you have any more queries.