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;
}
}
}