We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Words for Java incorrectly updating IF condition

With Aspose.Words for Java (13.9.0.0), when a merge field is replaced by Aspose, it is evaluated correctly. But if the merge is manually controlled by using the IFieldMergingCallback interface, then the updated value isn’t surrounded by double quotes, and the value is not converted to uppercase as defined in the field.

Field defined in word document:
{ IF { MERGEFIELD field1 * Upper * MERGEFORMAT } = “STAR WARS” Lucasfilm Dunno }

Expected result when the value Star Wars is passed as field1:
{ IF “STAR WARS” = “STAR WARS” Lucasfilm Dunno }

Actual result:
{ IF Star Wars = “STAR WARS” Lucasfilm Dunno }


------------------ Code sample -----------------

Document doc = new Document(args[0]);

doc.getMailMerge().setFieldMergingCallback(new IFieldMergingCallback() {
@Override
public void fieldMerging(FieldMergingArgs fieldMergingArgs) throws Exception {
if (fieldMergingArgs.getFieldName().equals(“field1”)) {
DocumentBuilder builder = new DocumentBuilder(fieldMergingArgs.getDocument());
builder.moveToMergeField(fieldMergingArgs.getFieldName());
builder.write(“Star Wars”);
fieldMergingArgs.setText("");
}
}

@Override
public void imageFieldMerging(ImageFieldMergingArgs imageFieldMergingArgs) throws Exception {
}

});
doc.getMailMerge().execute(new String[]{“field1”, “field2”},
new Object[]{“Star Wars”, “Star Wars”});
doc.save(“c:\testHtmlOut.doc”, SaveFormat.DOC);

The input file and the output files are attached.

Hi Kasun,


Thanks for your inquiry. This is the expected behavior; the problem occurs because the DocumentBuilder.MoveToMergeField(String) method moves the cursor to a position just beyond the specified merge field and removes the merge field (including the \Upper switch) before writing a plain string using DocumentBuilder.Write method. If we can help you with anything else, please feel free to ask.

Best regards,