IF field with quotes not printing complete data

Hello,

I will preface this by saying that I am not sure if it’s an aspose issue or a word/merge field issue.

My template has the following code:

The output is not printed correctly if the condition is evaluated as false and Description2 has double quotes.

Thank you!
Brunda

@gbrunda

Could you please provide more details about the specific issue you are facing with the merge field and the expected output?

Here you go with an example:
ProdType = XYZ

Description1: Hi, my name is “Description1” Testing with double quotes
Description2: Hi, my name is “Description2” Testing with double quotes

Expected Output if ProdType != XYZ: It displays Description1 Description2

Actual Output if ProdType !=XYZ: It displays Testing (i.e. word right after the double quote)

Note: This is an issue only when an If statement is present, if not it displays the double quotes correctly.

@gbrunda In MS Word IF field a whitespace is used as delimiter between parameters, so if field value of Description2 has a whitespace it is require to wrap it into double quotes. For example in the template you have the following field code:

{ IF { MERGEFIELD ProdType } = "XYZ" { MERGEFIELD Description1 } {  MERGEFIELD Description2 } }

if you fill it with data without whitespaces like this:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().setUnconditionalMergeFieldsAndRegions(true);
doc.getMailMerge().execute(new String[] { "ProdType", "Description1", "Description2" },
        new String[] { "ProdType", "Description1", "Description2" });
doc.save("C:\\temp\\out.docx");

Double quotes are not required and the output will look like this:

{ IF ProdType = "XYZ" Description1 Description2 }

but if there are whitespaces:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().setUnconditionalMergeFieldsAndRegions(true);
doc.getMailMerge().execute(new String[] { "ProdType", "Description1", "Description2" },
        new String[] { "ProdType", "The first description", "The second description" });
doc.save("C:\\temp\\out.docx");

It is required to wrap the values into double quotes to make it possible to distinguish between true and false values in the IF field and the result will look like this:

{ IF ProdType = "XYZ" "The first description" "The second description" }

in.docx (14.0 KB)
out.docx (11.4 KB)

It is normally is not allowed to use double quotes in IF field values. And the only way to have a double quite in the IF field is using an embedded field like this { QUOTE 34 }.

Interesting, is this a word limitation? Given that the field value is dynamic, we cannot use Quote 34.

@gbrunda This is MS Word limitation. You can try using IFieldMergingCallback to resolve the problem. But you should wrap true and false values in your IF field into double quotes like in the attached template:
in.docx (14.0 KB)
out.docx (11.4 KB)

Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().setUnconditionalMergeFieldsAndRegions(true);
doc.getMailMerge().setFieldMergingCallback(new QuoteResolver());
doc.getMailMerge().execute(new String[] { "ProdType", "Description1", "Description2" },
        new String[] { "ProdType", "The \"first\" description", "The \"second\" description" });
doc.save("C:\\temp\\out.docx");
private static class QuoteResolver implements IFieldMergingCallback
{
    @Override
    public void fieldMerging(FieldMergingArgs args) throws Exception {
        String fieldValue = (String) args.getFieldValue();
        if(!fieldValue.contains("\""))
            return;
    
        String[] parts = fieldValue.split("\"");
        DocumentBuilder builder = new DocumentBuilder(args.getDocument());
        builder.moveToField(args.getField(), false);
        for (int i=0; i< parts.length; i++)
        {
            builder.write(parts[i]);
            if(i<parts.length-1)
                builder.insertField("QUOTE 34");
        }
        args.setText("");
    }
    
    @Override
    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
        // Do nothing.
    }
}