Number format is not interpreted in the else of if...then...else condition

Hello,

I’m facing an issue that decimal number is not interpreted in the else condition of the if…then…else merged field. Below is code snippet to reproduce this problem. Note: this issue happens on Aspose Word 20.3 (using in production) and 20.11

public class HandleMailMergeNumbers {
    public static void main(String[] args) throws Exception {
        // The path to the documents directory.
        String dataDir = Utils.getSharedDataDir(HandleMailMergeNumbers.class) + "MailMerge/";

        // Open an existing document.
        Document doc = new Document(dataDir + "MailMergeNumbers.docx");
        //Remove merge field with empty value and if the row are empty=> remove paragraph
        doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS
                | MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS
                | MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS
                | MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS);

        doc.getMailMerge().setFieldMergingCallback(new CustomedFieldMergingCallback());

        doc.getMailMerge().setUnconditionalMergeFieldsAndRegions(true);

        // Fill the fields in the document with user data.
        doc.getMailMerge().execute(
                new String[]{"testNumber", "quantite", "sousFamilleProduit", "otherAttr"},
                new Object[]{"123456.9", 0.0, "abc", 11111.0});

        dataDir = dataDir + "MergeNumbers_out.doc";
        doc.save(dataDir);

        System.out.println("\nSimple Mail merge performed with array data successfully.\nFile saved at " + dataDir);
    }
}

public class CustomedFieldMergingCallback implements IFieldMergingCallback {
    @Override
    public void fieldMerging(FieldMergingArgs fma) throws Exception {
        if (fma.getField().getFormat().getNumericFormat() != null && !fma.getField().getFormat().getNumericFormat().isEmpty()) {
            if (fma.getFieldValue() != null && !StringUtils.isBlank(fma.getFieldValue().toString())) {
                DocumentBuilder builder = new DocumentBuilder(fma.getDocument());
                String languageTag = LanguageCode.FRENCH.getCode();
                String formatString = fma.getField().getFormat().getNumericFormat();
                String formatPattern = formatString;
                if (formatString.contains(":")) {
                    String[] formatParts = formatString.split(":");
                    languageTag = formatParts[0];
                    formatPattern = formatParts[1];
                }
                DecimalFormatSymbols formatSymbol = new DecimalFormatSymbols(Locale.forLanguageTag(languageTag));
                if (Locale.forLanguageTag(languageTag).getLanguage().isEmpty()) {
                    formatSymbol = new DecimalFormatSymbols(Locale.forLanguageTag(LanguageCode.FRENCH.getCode()));
                }
                // This is to handle for Swiss French and undefined case
                if ("fr_ch".equalsIgnoreCase(languageTag)) {
                    formatSymbol.setGroupingSeparator('\'');
                    formatSymbol.setDecimalSeparator('.');
                }
                DecimalFormat formatter = new DecimalFormat(formatPattern, formatSymbol);
                Double value = Double.parseDouble(fma.getFieldValue().toString());
                builder.moveToMergeField(fma.getFieldName());
                builder.write(formatter.format(value));
            }
        }
    }
...
}

sample output:
image.png (8.6 KB)

Note:
image.png (27.4 KB)

Test code and sample output:
testCode.zip (22.8 KB)

Thanks

@dat.do,

We have logged this problem in our issue tracking system. Your ticket number is WORDSNET-21679. We will further look into the details of this problem and will keep you updated on the status of the linked issue. We apologize for your inconvenience.

@dat.do,

Regarding WORDSNET-21679, we have completed the work on this issue and concluded to close this issue with “not a bug” status. The template document contains following field structure (simplified):

{ IF { MERGEFIELD quantite } <> 0 "{ MERGEFIELD quantite \# fr_ch:#,##0.00 }" "{ IF 0 = 0 "{ MERGEFIELD quantite \# fr_ch:#,##0.00 }" "" }" }

And you run mail merge with the UnconditionalMergeFieldsAndRegions option (all fields are merged regardless IF conditions) and custom merging callback (simplified):

public void fieldMerging(FieldMergingArgs e) throws Exception {
    if (e.getField().getFormat().getNumericFormat() == "" || e.getField().getFormat().getNumericFormat() == null)
        return;

    String formatPattern = e.getField().getFormat().getNumericFormat();

    if (formatPattern.contains(":")) {
        String[] formatParts = formatPattern.split(":");
        formatPattern = formatParts[1];
    }

    double value = Double.parseDouble(e.getFieldValue().toString());
    DocumentBuilder builder = new DocumentBuilder(e.getDocument());
    builder.moveToMergeField(e.getFieldName());
    builder.write(String.format(formatPattern, value));
}

Merging callback skips the first quantite field (because of no numeric format). So, when the second quantite field is merged the DocumentBuilder.MoveToMergeField method moves cursor to the first field and removes it and so on. Please move document builder to certain field explicitly and remove it:

builder.MoveTo(e.Field.Remove());