Text font changes unexpectedly for part of the text when merging a field with multiline plain input string

Here’s the scenario that causes the issue:
Two merge fields with identical settings get same multiline plain input text.
Sample input text:
Test
Test
Test

Text that’s passed to api is “Test\nTest\nTest”

In the merge output doc one field displays text correctly identical to input. The other field changes font of the text starting the second line. Like so:
Test
Test
Test

See attached images for the actual template, input and output.

If I go into word template and add a character after the problematic mergefield or hit enter and then backspace(effectively changing nothing), the issue goes away.
We have many merge documents with many merge fields in each. Going through each one and typing enter+backspace next to each field is highly undesirable. Would be nice to know source of the problem and possible programmatic work around.
Outlined api use:

Aspose.Words.Document doc;
using(MemoryStream templateStream = new MemoryStream(templateFile))
{
    doc = new Document(templateStream);
}
doc.MailMerge.Execute(documentLevelData.Rows[0]);

Thank you.

Hi Alex,

Thanks for your inquiry. Could you please attach your input Word document here for testing? I will investigate the issue on my side and provide you more information.

Thank you for quick response. I attached input Word document to the original post.

Hi Alex,

Thanks for sharing the document. Perhaps, you are using an older version of Aspose.Words; as with Aspose.Words v13.3.0.1, I am unable to reproduce this problem on my side. I would suggest you please upgrade to the latest version of Aspose.Words i.e. v13.3.0.1 and let us know how it goes on your side. I hope, this will help. I have attached the output Doc/Docx files with this post for your kind reference.

Hello,

We purchased latest version of Aspose.Words and it resolved the issue we were originally having. However it introduced another issue of very similar nature using same template and dll version 13.3.0.1. In the attached image, you can see that in two separate fields all characters after ‘/’ have smaller font. Tests showed that removing ‘/’ results in consistent font. You can find table with problematic fields on top of page two of the template attached in the previous post.

Hi Alex,

Thanks for your inquiry. Please note that Aspose.Words tries to mimic the same behaviour as MS Word do. If you do the mail merge operation for PatientName and PatientBirthDate by using MS Word, you will get the same output. Please see the attached image, the font size is not same for all characters in specific area.

Please set the same font size for following text and check this scenario with latest version of Aspose.Words. Hope this helps you.

MEDICATION ACTION PLAN FOR «PatientName», DOB: «PatientBirthDate»

Thank you for replying.
Running merge using our pre-upgrade 8.2 version of the aspose.words yields consistent font in the field output. That means that behavior was acquired through changes/bugs in newer versions. Our templates were developed with 8.2. Having just paid to upgrade to newest version we expect backward compatibility. Changing scores of templates is hardly an option.

Seems very random that presence of ‘/’ character changes font within a field. Part of text displayed with one font and part with another can’t be an intentional behavior. Would you be able to look into why this happens and offer a possible solution.

Just to make sure we are on the same page, the scenario is that string of ‘Test’ displays correctly with consistent font whereas string of ‘Test/abc’ will display ‘/abc’ part of the string with a smaller font. Observed in 13.3 and 13.5. 8.2 works correctly.

Thank you.

Hi Alex,

Thanks for your inquiry. Please note that the behaviour of output document which you are facing is not a bug. MS Word and Aspose.Words generates the same output.

In your case, I suggest you please implement the IFieldMergingCallback* interface as shown in following code snippet. This will solve your problem. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "SYSTEM_CASE_TEMPLATE_PML_MAP.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
doc.MailMerge.FieldMergingCallback = new FontIssue(builder);
doc.MailMerge.Execute(new string[]
    {
        "PatientName",
        "PatientBirthDate"
    },
    new object[]
    {
        "Wallace Martelmo/abc",
        "12/4/1945"
    });
doc.Save(MyDir + "Out.docx");
public class FontIssue : IFieldMergingCallback
{
    public FontIssue(DocumentBuilder b)
    {
        builder = b;
    }

    DocumentBuilder builder;

    ///
    /// This is called when merge field is actually merged with data in the document.
    ///
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (e.FieldValue != null)
        {
            builder.MoveToMergeField(e.DocumentFieldName);
            builder.Write(e.FieldValue.ToString());
            e.Text = "";
        }
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // Do nothing.
    }
}