Merge field format lost when using FieldMergingCallback

I have a template that have some merge fields that are in side and out side a table. The values for these fields are queried and cached to map then set to the field by a custom FieldMergingCallback, here is the java code:

// doc is a Document object
doc.getMailMerge().setFieldMergingCallback(new IFieldMergingCallback() {
    public void fieldMerging(FieldMergingArgs arg0) throws Exception {
        if (fieldCache.containsKey(arg0.getFieldName())) {

   public void imageFieldMerging(ImageFieldMergingArgs arg0) throws Exception {

The problem is I lost all field format (\* Caps, \* Upper, etc…) when using setText method, I have tried to call doc.updateFields() after the call to executeWithRegions() but the format still lost.

How can I reserve format in the call back?

This is a legacy system so we are stuck in aspose word 14.5.0


To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a simple Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Here is the sample project: (37.2 KB)

The input document is the file “test.docx” in resources folder of the java project.
The actual output is file “result-not-expected.docx”
The expected output is file “result-expected.docx”

When you run the project, it will produce a file “result.docx”

What I want is both fileds “GlobalField1” and “GlobalField2” are formatted in the table.

After hours of searching the internet, I end up with a very ugly hack, the idea is in every call back, I create a fake document and clone the current field to this fake document, excute it to get the final result, then set it back to the actual field with setText function:

doc.getMailMerge().setFieldMergingCallback(new IFieldMergingCallback(){
    public void fieldMerging(FieldMergingArgs e)throws Exception{
            Document fakeDoc=new Document();
            DocumentBuilder builder=new DocumentBuilder(fakeDoc);
            fakeDoc.getMailMerge().setFieldMergingCallback(new IFieldMergingCallback(){
                public void fieldMerging(FieldMergingArgs arg)throws Exception{
                    DocumentBuilder innerBuilder=new DocumentBuilder(arg.getDocument());

                public void imageFieldMerging(ImageFieldMergingArgs arg){

            fakeDoc.getMailMerge().execute(new String[]{e.getFieldName()},new Object[]{cache.get(e.getFieldName())});
           // Set the formatted text to the actual field

    public void imageFieldMerging(ImageFieldMergingArgs args){

I hope there is an built-in way to do this task.


You are facing the expected behavior of Aspose.Words. When you implement IFieldMergingCallback interface, its means that you want to control how data is inserted into merge fields during a mail merge operation. You need to write your own code to format the field value.

However, we have added new feature to support field formats when FieldMergingArgs.Text is used. The feature ID is WORDSNET-19750. We will check the possibility of implementation of this feature and update you about it via this forum thread. We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-19750) have been fixed in this Aspose.Words for .NET 20.2 update and this Aspose.Words for Java 20.2 update.

1 Like