BARCODE generation failing if text has DOUBLE QUOTE

While I configure DISPLAYBARCODE over a field, if the arg1, i.e. INPUT text has DOUBLE QUOTE ("), the document fails to parse the arg2.

{ DISPLAYBARCODE "8"bread bar" CODE128 \t }

It results in error ‘Required Parameters are missing or incorrect’.

I understand that the default separator for MERGEFIELD is DOUBLE QUOTE, hence the parsing failing and considering the text beyond 2nd separator as arg2 itself.
But while doing the WordMerge and then integrating the Barcode API, I found no option to either receive actual, whole INPUT text and/or could not find any configuration to change the default separator type.

Whatever happening at Word Doc level, same is happening within the code as well

Need support to get over this problem.

@rootforms Double quotes should be escaped in the MS Word field code to make it to be considered as a simple text. You can use either backslash to escape the double quote:
{ DISPLAYBARCODE "8\"bread bar" CODE128 \t }
Or use { QUOTE 34 } field:
{ DISPLAYBARCODE "8{ QUOTE 34 }bread bar" CODE128 \t }

Thanks @alexey.noskov for your quick response.

Actually the backslash works only at Document level. When it comes to code, here is my finding

  • if the chars are replaced before WordMerge, post merge, the Barcode API (i.e. BarcodeParameters) receives the TEXT as escaped one.
  • and as mentioned in opening remark, the ARG1 (i.e. text) itself isn’t fully available in BarcodeParameters. Such incomplete text is then presented for Barcode Generation.

And the problem continues…
Hence I was checking if some configuration at API level can take care of escaping the DOUBLE QUOTE itself.

@rootforms It is not quite clear how to reproduce the problem on my side. As I can see Aspose.Words properly returns barcode value in BarcodeParameters. Here is simple code I have used for testing:

Document doc = new Document(@"C:\Temp\in.docx");
doc.FieldOptions.BarcodeGenerator = new DummyBarcodeGenerator();
doc.Save(@"C:\Temp\out.pdf");
private class DummyBarcodeGenerator : IBarcodeGenerator
{
    public Image GetBarcodeImage(BarcodeParameters parameters)
    {
        Console.WriteLine(parameters.BarcodeValue);

        throw new NotImplementedException();
    }

    public Image GetOldBarcodeImage(BarcodeParameters parameters)
    {
        Console.WriteLine(parameters.BarcodeValue);

        throw new NotImplementedException();
    }
}

parameters.BarcodeValue returns proper value - "8\"bread bar", when the following filed code is used:
{ DISPLAYBARCODE "8\"bread bar" CODE128 \t }

Thanks @alexey.noskov, for following it up.
Instead of hardcoding, you could set up a MERGEFIELD against some database value as something like 8"bread bar/6" donut.
And then additionally mailMerge over xmldata.

Document doc = new Document(@"C:\Temp\in.docx");
doc.getMailMerge().execute(xmlDataSet);
doc.updateFields();
doc.FieldOptions.BarcodeGenerator = new DummyBarcodeGenerator();
doc.Save(@"C:\Temp\out.pdf");

Hope this helps you reproduce the issue. (just tried correcting the xmldata, but no change)

@rootforms Thank you for additional information. To get the desired output you should also escape backslash otherwise double quote in the merge field value is inserted as not escaped. The following code produces the correct result:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().execute(new String[] { "test"}, new String[] { "8\\\"bread bar" });

Hi @alexey.noskov, adding 3 backslashes works to get the Barcode.
However, it is impacting other MERGEFIELDS since they don’t need the additional escape chars, whereas code level change gets applied to all fields.
I am yet to figure out how I can get type of MERGEFIELD before I replace double quotes only for DISPLAYBARCODE. Do you have any pointers here?

@rootforms You can consider using IFieldMergingCallback and check whether the current merge field is nested into the DISPLAYBARCODE field. Or if you know field name, which is nested into the DISPLAYBARCODE field, you can simply check merge field name and if condition match, escape double quote. For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().setFieldMergingCallback(new BarcodeFieldMergingCallback());
doc.getMailMerge().execute(new String[] { "test"}, new String[] { "8\"bread bar" });
doc.save("C:\\Temp\\out.docx");
private static class BarcodeFieldMergingCallback implements IFieldMergingCallback
{

    @Override
    public void fieldMerging(FieldMergingArgs fieldMergingArgs) throws Exception {

        // Get field start of the merge field.
        FieldStart start = fieldMergingArgs.getField().getStart();

        // Check if merge field is nested into DISPLAYBARCODE
        boolean isBarcode = false;
        Node currentNode = start.getPreviousSibling();
        while (currentNode != null) {
            if(currentNode.getNodeType() == NodeType.FIELD_START)
            {
                isBarcode =  ((FieldStart)currentNode).getFieldType() == FieldType.FIELD_DISPLAY_BARCODE;
                break;
            }
            currentNode = currentNode.getPreviousSibling();
        }

        if(isBarcode) {
            String rawValue = (String) fieldMergingArgs.getFieldValue();
            // Escape double quote.
            fieldMergingArgs.setFieldValue(rawValue.replace("\"", "\\\""));
        }
    }

    @Override
    public void imageFieldMerging(ImageFieldMergingArgs imageFieldMergingArgs) throws Exception {
        // Do nothing
    }
}
1 Like

Thanks Alexey!

1 Like