Merged Text Prints in Different Font During Mail Merge

Hello

I am upgrading aspose.word for java to the latest version.

I get a problem about font.

I have a lot of existing word template. And after the templates are merged, doc files are generated, I find that the first symble of a lot of words looks smaller than the symber behind it.
For example, for '06/01/2011’ which is a merged value, font size of ‘06’ looks smaller than ‘06/01/2011’. Font size of ‘06’ is 8, font style is ‘Arial’, but font size of ‘06/01/2011’ is bigger and font style is ‘Times New Roman’.

I search this problem in internet. An articl in support center of Microsoft describes this problem. This is the link: https://support.microsoft.com/en-us/topic/wd2000-merged-text-prints-in-different-font-during-mail-merge-0b8f02d8-76bd-26a9-54dd-95de2be50b8a

This article gives three workarounds.
Method 1: Add the Charformat Switch. Edit the merge field and add the Charformat switch.
Method 2: Change the Normal Style. You can modify the built-in Normal style to match the font that you want.
Method 3: Merge to a New Document and Make Formatting Changes

These workarounds only for word. I have a lot of templates to handle. It’s impossible for me to do this mannually. Could you please tell me to change the normal style in runtime? I prefer method 2. Or you have other way to solve this problems?
Maybe you can read this article in support center of Microsoft.

Thank you very much

Hi Shaohui,

Thanks for your inquiry.

To be able to apply custom formatting during Mail Merge, you should handle IFieldMergingCallback.FieldMerging event. For more information, please see the following link:
https://docs.aspose.com/words/java/mail-merge-and-reporting/

Moreover, to change merge field style and formatting, please see the following sample implementation of IFieldMergingCallback.FieldMerging event:

private class HandleMergeFieldAlternatingRows : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (mBuilder == null)
            mBuilder = new DocumentBuilder(e.Document);
        if (e.FieldName.Equals("yourDateFieldName"))
        {
            mBuilder.MoveToMergeField(e.FieldName);
            mBuilder.Font.StyleIdentifier = StyleIdentifier.DefaultParagraphFont;
            Run run = new Run(mBuilder.Document);
            run.Text = e.FieldValue.ToString();
            // You can set font here
            Aspose.Words.Font font = run.Font;
            font.Bold = true;
            font.Color = Color.Red;
            mBuilder.InsertNode(run);
        }
    }
    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        throw new NotImplementedException();
    }
    private DocumentBuilder mBuilder;
}

I hope, this will help.

Best Regards,

Hi,

In addition to my previous reply, please note that the formatting of merge fields, upon Mail Merge execution, should be preserved and that is not happening in your case. Could you please attach your template document here for testing? We will investigate and try to reproduce this issue on on our side and then provide you more information.

Best Regards,

Hi awais.hafeez:

Thanks for your answer. But I think you misunderstood me.
The style is
defined in word file. For the same field, in different file, its style
is different. The style is defined by user, but not set in runtime.

Here is my template file and the result file.

I am looking forward to your response.

Thank you very much!

Hello
Thank you for additional information. Please try using the following code to avoid this behavior:

Document doc = new Document("C:\\Temp\\DocTemplate.Invoice.doc");
DataTable dt = new DataTable("InvoiceItem");
dt.Columns.Add("InvoiceItem.SubscriptionNumber");
dt.Columns.Add("InvoiceItem.RatePlanName");
dt.Columns.Add("InvoiceItem.Name");
dt.Columns.Add("InvoiceItem.Quantity");
dt.Columns.Add("InvoiceItem.UnitPrice");
dt.Columns.Add("InvoiceItem.ServicePeriod");
dt.Columns.Add("InvoiceItem.ExtendedPrice");
//Add some data
dt.Rows.Add(new object[]
{
    "SubscriptionNumber1", "RatePlanName1", "Name1", "Quantity1", "UnitPrice1",
    "07/01/2011-07/31/2011", "ExtendedPrice1"
});
dt.Rows.Add(new object[]
{
    "SubscriptionNumber2", "RatePlanName2", "Name2", "Quantity2", "UnitPrice2",
    "07/01/2011-07/31/2011", "ExtendedPrice2"
});
doc.MailMerge.FieldMergingCallback = new HandleMergeFieldAlternatingRows();
doc.MailMerge.ExecuteWithRegions(dt);
doc.Save("C:\\Temp\\out.doc");
private class HandleMergeFieldAlternatingRows : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (mBuilder == null)
            mBuilder = new DocumentBuilder(e.Document);
        if (e.FieldName.Equals("InvoiceItem.ServicePeriod"))
        {
            mBuilder.MoveToMergeField(e.FieldName);
            Run run = new Run(mBuilder.Document);
            run.Font.Name = mBuilder.Font.Name;
            run.Font.Size = mBuilder.Font.Size;
            run.Text = e.FieldValue.ToString();
            mBuilder.InsertNode(run);
        }
    }
    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        throw new NotImplementedException();
    }
    private DocumentBuilder mBuilder;
}

Best regards,

Thanks you, AndreyN!

Your code works very well.

I have one question for you.
What the meanning of ‘mBuilder.Font.Name’?
Does it stand for the global default font style?

If the global style is different field style, how it works?

Thanks again!

Hello
Thanks for your inquiry. ‘mBuilder.Font.Name’ it is font of FieldName. Please press Alt-F9 to see the field code. As you can see FieldName->Arial, FieldCode and FieldSeparator->TimesNewRoman. Actually it is the reason of the problem.
Best regards,

Hi,
I run into this issue again when I am working on the same project of evaluating the upgrading Aspose word for Java from 2.4.1 to 11.5.0 that Shaohui worked on before.
I have two questions:
Q1: Does this is a regression? For the same template, it can be generated correct word in version 2.4.1 but this issue will happen in v11.5.0.
Q2: Is there some global setting to control this behavior instead of using FieldMergeCallback?
In our template, there are hundreds merge field and the leading words in those merge fields are smaller then the following words just as the previous description Shaohui wrote. So it is hard to use the if-else statement to adjust all merge fields Font. I am concerning about this approachs performance. If the output word has more then 50 pages, the performance may have serious degradation with this field by field font adjustment.
I am looking forward to your reply since this is very important for me to decide when and how to upgrade our Aspose component to latest version.

Any feedback?

The problem is part of the merged field using the font of "FieldName"
and part of the merged field using the font of Normal style.

For example:
“United States” is the value of Country with font Arial 9. You can see the “United” is rendered correctly but the “States” is wrong, the font is “Times New Roman 12”.
Hope anyone from Aspose can check if this is regression defect.

Hi Cosmo,

Thanks for your inquiry and sorry for the delayed response. I am working over your query and will get back to you as soon as possible.

Best Regards,

Hi Cosmo,

Thanks for your patience.

While using the latest version of Aspose.Words i.e. 11.5.0, I managed to reproduce this issue on my side. I have logged this issue in our bug tracking system as a regression. The issue ID is WORDSNET-6674. Your request has been linked to this issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Best Regards,

Thanks for your reply. I am glad to see your guys take action to fix it. Hope it can be released with 11.6.0 so that I have enough time to retest it.

Hi Cosmo,

Thanks for your inquiry. Unfortunately, your issue is not resolved yet. Our development team is currently doing analysis of this issue. We will inform you as soon as your issue is resolved.

Best Regards,

The issues you have found earlier (filed as WORDSNET-6674) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Could you confirm that this issue has been fixed and no regression on 13.2.0?
From our testing result, we found that this issue still exist in Aspose.Word 13.2.0
And I know new Run set Font can resolve this issue, but it will cost a lot of memory from our observation since we have large number of records during mailmerge. And moveToMergeField and new Font will cost a lot of memory consumption.

Hi Guilin,
Thanks for your inquiry. Yes, the fix to WORDSNET-6674 was integrated in Aspose.Words 11.7.0. However, to ensure a timely and accurate response please supply us with the following information, if you cannot supply us with this information we will not be able to investigate your issue and raise a ticket.
What version of Aspose.Words for Java are you using?
What environment are you running on?
OS (Windows Version or Linux Version)
Architecture (32 / 64 bit)
MS Office Version
Any Third Party Libraries / SDK in addition to Aspose Products
Please supply us with the code from your application that is causing the issue
Please supply us with the input document that is causing the issue
Please supply us with the output document showing the undesired behavior
As soon as you get these pieces of information to us we’ll start our investigation into your issue.
Many thanks,

@AndreyN can you send me same code for java, because I tried it’s not worked for java

@kalpeshAspose1997 You can find a similar Java code example in our documentation:
https://docs.aspose.com/words/java/how-to-apply-custom-formatting-during-mail-merge/

And there are more code example in our GitHub:
https://github.com/aspose-words/Aspose.Words-for-Java