IF condition value is case sensitive post aspose word 15.11 jar

‘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.

Upgraded jar is not backward compatible.

Kindly let us know how to handle this scenario.

@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

MS Word behaves the same way.

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}

Sample template and input xml also attached.

SampleTemplate.zip (27.2 KB)

@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.

Sample code has been attached

ToForum.zip (105.3 KB)

@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.

Kindly share a code, it will more helpful for us.

@akondewar You can use the following code:

DataSet ds = new DataSet();
ds.readXml("C:\\Temp\\data.xml");
        
Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().setFieldMergingCallback(new ConvertToLowercaseCallback());
doc.getMailMerge().setUnconditionalMergeFieldsAndRegions(true);
doc.getMailMerge().execute(ds.getTables().get(0));
doc.save("C:\\Temp\\out.docx");
private static class ConvertToLowercaseCallback implements IFieldMergingCallback
{
    
    @Override
    public void fieldMerging(FieldMergingArgs args) throws Exception {
    
        if(args.getFieldName().equals("Print_Notes")) {
            args.setFieldValue(((String)args.getFieldValue()).toLowerCase());
        }
    }
    
    @Override
    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
        // Do nothing.
    }
}

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");
private static String trim(String str, char charToRemove)
{
    while (str.charAt(0) == charToRemove)
        str = str.substring(1);

    while (str.charAt(str.length() - 1) == charToRemove)
        str = str.substring(0, str.length() - 1);

    return str;
}

@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.

  1. https://forum.aspose.com/t/some-of-the-texts-has-the-last-character-followed-by-a-space-for-japanes-font-msgothic-using-aspose-java-word-on-linux/267071

  2. https://forum.aspose.com/t/getting-error-java-lang-nullpointerexception-page-mergeformat-numpages-mergeformat-tag-in-word-file/265588/3

  3. https://forum.aspose.com/t/when-using-if-condition-in-a-word-template-characters-starting-with-a-double-quote-do-not-appear-in-the-pdf/268188

  4. https://forum.aspose.com/t/how-to-convert-word-to-pdf-without-compressing-images-using-java-aspose-words-document/258334/4

  5. https://forum.aspose.com/t/when-using-if-condition-in-a-word-template-characters-starting-with-a-double-quote-do-not-appear-in-the-pdf/268188/6

ToForum.zip (112.5 KB)

@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.

Hi @alexey.noskov
Then how to handle this scenario, because we have fixed following issue as suggested by support.

https://forum.aspose.com/t/when-using-if-condition-in-a-word-template-characters-starting-with-a-double-quote-do-not-appear-in-the-pdf/268188/4?u=akondewar

@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

We need following fix also needs, it’s look like aspose is not backward compatable.
https://forum.aspose.com/t/when-using-if-condition-in-a-word-template-characters-starting-with-a-double-quote-do-not-appear-in-the-pdf/268188/4?u=akondewar

15-Dec-2023.zip (113.6 KB)

@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:

doc.getMailMerge().setCleanupOptions(cleanupOptions);
doc.getMailMerge().setUnconditionalMergeFieldsAndRegions(true);
doc.getMailMerge().executeWithRegions(dataSet);

@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.