‘IF’ condition value ‘True’ and ‘TRUE’ is working fine in aspose word java 15.11 jar.
While upgrade the jar 22.12 then faced following issue
TRUE’ is not working when used inside the ‘IF’ condition in the template document. They have also mentioned that the boolean value ‘true’ is working properly when used inside the ‘IF’ condition.
Looks like boolean value is not case sensitive in 15.11 Jar. Now it is case sensitive 22.12.
@akondewar Could you please attach a sample template where you have experienced a problem? We will check the issue and provide you more information. Do you use condition like this? { IF "{ MERGEFIELD test1 }" = "true" "True value" "False value" }
Condition in such case if case sensitive.
{ IF "true" = "true" "True value" "False value" } - shows True value { IF "True" = "true" "True value" "False value" } - shows False value { IF "TRUE" = "true" "True value" "False value" } - shows False value
Hi @alexey.noskov
Our issues is, if 'Print_Note ’ have only lower case boolean value ‘true’ then data has been print in the template for 22.12 jar. If upper case boolean value ‘TRUE’ then data not print. It was working fine in previous 15.11 jar.
Our field mapping as follows {{IF{MERGEFIELD Print_Note \* MERGEFORMAT} = "true" {MERGEFIELD Line_Notes \* MERGEFORMAT} ""\*MERGEFORMAT}
@akondewar Thank you for additional information. After executing mail merge IF field code looks like this: { IF TRUE = "true" "RSTK notes test" "" \* MERGEFORMAT }
As I have mentioned condition is case sensitive in MS Word as well as in Aspose.Words. So in this case false value is displayed. So the current behavior is expected.
Hi Thanks reply,
Regardless of whether it’s a small letter or capital letter, it works fine in 15.11 jar, so it’s not case-sensitive. Please check with 15.11 jar.
Facing this issue while upgrading the aspose jar 22.12.
@akondewar It looks like a bug in an old version of Aspose.Words, since current Aspose.Words behavior matches MS Word behavior, so it is correct. You can use IFieldMergingCallback to convert the field value to lowercase.
@alexey.noskov , Is there a way to convert a specific IF condition boolean value into lowercase? We don’t want other data to be converted to lowercase.
Thanks for reply, We have multiple word templates for multiple transactions, so we never don’t know which merged filed have boolean value. Even one document have multiple merged filed boolean value.
Can you please let me know if there is an API to handle this scenario. If have then share us code.
@akondewar You can modify the IFieldMergingCallback implementation like this:
private static class ConvertToLowercaseCallback implements IFieldMergingCallback
{
@Override
public void fieldMerging(FieldMergingArgs args) throws Exception {
String val = ((String)args.getFieldValue()).toLowerCase();
if(val.equals("true") || val.equals("false")) {
args.setFieldValue(val);
}
}
@Override
public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
// Do nothing.
}
}
Alternatively, if it is required to make all IF fields conditions case insensitive, you can convert all expressions to lowercase. For example see the following code:
Document doc = new Document("C:\\Temp\\in.docx");
// Execute mail merge here
// ................
for (Field f : doc.getRange().getFields())
{
if (f.getType() == FieldType.FIELD_IF)
{
FieldIf ifField = (FieldIf)f;
// Trim double quotes to avoid their escaping upon setting the expression.
ifField.setLeftExpression(trim(ifField.getLeftExpression().toLowerCase(), '"'));
ifField.setRightExpression(trim(ifField.getRightExpression().toLowerCase(), '"'));
ifField.update();
}
}
doc.save("C:\\Temp\\out.docx");
@alexey.noskov Thanks for sulution, but it dose not work , here I am shareing with you simple word template file, input xml file, java code and output file.
Note :
We have fixed following issues as suggested by aspose, that code change also present in this java file.
@akondewar The code does not work in your case because MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS clean up option is used. In this case upon executing mail merge IF fields that contain merge fields are unlinked, i.e. replaced with simple text. So the code that process IF fields after executing mail merge does not have any effect.
@akondewar You can try avoiding using MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS and modify the workaround for “case insensitive IF field condition” like this:
for (Field f : doc.getRange().getFields())
{
if (f.getType() == FieldType.FIELD_IF)
{
FieldIf ifField = (FieldIf)f;
// Trim double quotes to avoid their escaping upon setting the expression.
ifField.setLeftExpression(trim(ifField.getLeftExpression().toLowerCase(), '"'));
ifField.setRightExpression(trim(ifField.getRightExpression().toLowerCase(), '"'));
ifField.update();
ifField.unlink(); // Replace field with simple text.
}
}
But in general the problem from the mentioned thread occurs because Document.updateFields is called, that is not normally required after executing mail merge.
Hi,
If I commented ‘MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS’ and ifField.unlink(); // and added ifField.unlink(); , then output is generated like ’ Notes: «Line_Notes» ’ and ‘Notes: «Line_Notes1»’
Sample code, word template, input xml and generated pdf has been attached
@akondewar By default Aspose.Words does not merge merge fields in unconditional value of IF fields. So in your case if IF field looks like this: { IF "{ MERGEFIELD test }" = "TRUE" "{ MERGEFIELD trueValue }" "{ MERGEFIELD falseValue }" }
After executing mail merge, if the value of test field is true, the resulting field will look like this: { IF "true" = "TRUE" "{ MERGEFIELD trueValue }" "false value" }
I.e. in the true value of IF field remains untouched. Since after executing mail merge the condition is changed, it is required to have the true value must have an actual value. To achieve this you should set UnconditionalMergeFieldsAndRegions option. Please modify your code like this:
@alexey.noskov
As per your reply ‘It looks like a bug in an old version of Aspose.Words, since current Aspose.Words behavior matches MS Word behavior, so it is correct’
Please let us know release note link and issue fixed api version , so that we can add a reference to our internal document.
@akondewar Unfortunately, currently it is difficult to specify what fix has changed the behavior. There were 80+ released published since 15.11 version and each release contains 50-80 fixes and improvements included.