MailMerge fails when using version > 17.5 (test included)

I’m trying to upgrade our current project to the latest version of Aspose.Words for Java, but there seems to be an issue when using a Aspose.Words version greater than 17.5 with our mailmerge. I however don’t seem to find anything in the release notes that could explain this behavior.

We are inserting a barcode using a barcode font (not an image) when we find a field “DISPLAYBARCODE” (not shown below). All our templates are using this field, so it’s not really an option to update the this field with something else.

Below I wrote a simple test to show the behavior. When running the test using Aspose.Words 17.5 Words or lower the test succeeds, using 17.6 and up the test fails…

import com.aspose.words.*;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;


public class MergeCallbackTest {


@Test
public void test() {

    try {

        Document document = createDocument();

        MyFieldMergingCallback callback = new MyFieldMergingCallback();

        document.getMailMerge().setFieldMergingCallback(callback);
        document.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS);
        document.getMailMerge().execute(new MyIMailMergeDataSource());

        Assert.assertTrue("field merge not called",callback.isFieldMergingCalled);

    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    }
}

/**
 * Create document with 1 line :   {DISPLAYBARCODE"{MERGEFIELD RRRN}" CODE 128}
 * @return
 * @throws Exception
 */
private Document createDocument() throws Exception {

    Document document = new Document();
    DocumentBuilder builder = new DocumentBuilder(document);

    Field f1 = builder.insertField("DISPLAYBARCODE", "");
    builder.moveTo(f1.getStart().getNextSibling().getNextSibling());
    builder.write("\"");
    builder.insertField("MERGEFIELD RRNR");
    builder.write("\" CODE 128");
    return document;
}


public class MyFieldMergingCallback implements IFieldMergingCallback {

    boolean isFieldMergingCalled = false;
    boolean isImageFieldMerginCalled = false;

    @Override
    public void fieldMerging(FieldMergingArgs fieldMergingArgs) throws Exception {
        isFieldMergingCalled = true;

    }

    @Override
    public void imageFieldMerging(ImageFieldMergingArgs imageFieldMergingArgs) throws Exception {
        isImageFieldMerginCalled = true;
    }


}

private class MyIMailMergeDataSource implements IMailMergeDataSource {

    private final Map<String, String> myMap = createMap();

    private Map<String, String> createMap()
    {
        Map<String,String> myMap = new HashMap<>();
        myMap.put("RRNR", "1");
        return myMap;
    }

    boolean moveNext = true;
    @Override
    public String getTableName() throws Exception {
        return "MyIMailMergeDataSource";
    }

    @Override
    public boolean moveNext() throws Exception {
            if (moveNext) {
                moveNext = false;
                return true;
            } return false;

    }

    @Override
    public boolean getValue(String s, Object[] fieldValue) throws Exception {
        if (s != null) {
            fieldValue[0] = myMap.get(s);
            return true;
        }
        return false;
    }

    @Override
    public IMailMergeDataSource getChildDataSource(String s) throws Exception {
        return null;
        }
    }
}

@Cuball,

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-16024. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

@Cuball,

The issues you have found earlier have been fixed in this Aspose.Words for .NET 17.11 update and this Aspose.Words for Java 17.11 update.

Please also check the following articles: