Hi,
I’m using Aspose words for java, and I have an example mailmerge document making use of conditional text which is behaving incorrectly after a recent Aspose upgrade. This example works as I expect in Aspose Words 18.7, but has changed somewhere between then and Aspose Words 19.9 (I also tried the latest versions 19.10 and 19.11).
I’ve attached a minimal test case with an application, the template Word document and the expected and actual results. Repeating here for visibility:
My template looks like this:
Value of FIELD1 field: "{ MERGEFIELD "FIELD1" }"
Value of FIELD2 field: "{ MERGEFIELD "FIELD2" }"
Example 1.
{IF MERGEFIELD "FIELD1" } <> "" "Text text" "empty" }
Example 2.
{IF MERGEFIELD "FIELD1" } <> "" "{ MERGEFIELD "FIELD2" }" "empty" }
Example 3.
{IF MERGEFIELD "FIELD1" } <> "" "Text { MERGEFIELD "FIELD2" } text" "empty" }
Example 4.
{IF MERGEFIELD "FIELD1" } <> "" "Text { IF 1 = 1 "Yes and { MERGEFIELD "FIELD2" }." "No" } text" "empty" }
I’m running it with a blank value for FIELD1 and the value “ABC” for FIELD2. What I expect is that all 4 of the example fields resolve to “empty”, because FIELD1 is blank. This makes sense and is also the behaviour that I see when performing the mailmerge MS Word.
Expected result and result using Word or Aspose 18.7:
Value of FIELD1 field: ""
Value of FIELD2 field: "ABC"
Example 1.
empty
Example 2.
empty
Example 3.
empty
Example 4.
empty
The attached example application that runs this same mailmerge using the latest version of Aspose. It uses the methods MailMerge.execute() followed by Field.unlink() using a document visitor to unlink all of the fields. What I actually see when running this mailmerge with the application is that examples 2 and 3 both somehow resolve to “FIELD2”, which seems very wrong.
Actual result from Aspose 19.10:
Value of FIELD1 field: ""
Value of FIELD2 field: "ABC"
Example 1.
empty
Example 2.
FIELD2
Example 3.
FIELD2
Example 4.
empty
If I put a filter on the document vistor to only unlink the conditional IF fields and not the mergefields, then it fixes examples 2 and 3 but breaks example 4. (this is what my real application actually does currently)
I’ve discovered that there is another workaround which is that if I add this line:
mailMerge.setUnconditionalMergeFieldsAndRegions(true);
then this fixes all 4 of my examples. However I’m reluctant to use this in my application after reading the documentation:
and section “Option to Choose Between Old and New Mail Merge Behaviors Added” near the end of here: Aspose.Words for Java 18.9 Release Notes
The documents seem to say that setUnconditionalMergeFieldsAndRegions(true) reverts to an older Aspose behaviour that is inconsistent with MS Word, but in my case I’m seeing the opposite: I want the behaviour to be consistent with Word and in my case using this option seems to make it more consistent. Also I understand this option was added in response to the problem WORDSNET-17313 which was raised in Aspose 18.7 (and allows the behaviour to be reverted to some version before that), whereas my example works as expected in Aspose 18.7, so I don’t think my problem is the same as the one this option was meant to solve.
I’d appreciate any help that could be offered with this problem.
aspose-testcase.zip (104.7 KB)