Do not display MERGEBARCODE for empty value

Hi!

How can I achieve this?
I tried many things with {IF…} but cound not get the desired result.

Thank You!

@EPluribusUnum You can remove DISPLAYBARCODE fields with empty values after executing mail merge:

Document doc = new Document("C:\\Temp\\in.docx");

doc.getMailMerge().execute(new String[] { "test" }, new String[] { "" });

// Remove DISPLAYBARCODE fields with empty values.
for (Field f : doc.getRange().getFields())
{
    if(f.getType() == FieldType.FIELD_DISPLAY_BARCODE)
    {
        FieldDisplayBarcode fdb = (FieldDisplayBarcode)f;
        if(fdb.getBarcodeValue().equals(""))
            fdb.remove();
    }
}

doc.save("C:\\Temp\\out.docx");
2 Likes

@alexey.noskov, this works, thank you!

1 Like

A little addition was needed to be a complete solution:

for (Field f : dotx.getRange().getFields())
{
    switch (f.getType())
    {
        case FIELD_DISPLAY_BARCODE:
            {
                FieldDisplayBarcode fdb = (FieldDisplayBarcode)f;
                if (isEmpty(fdb.getBarcodeValue()))
                {
                    fdb.remove();
                }
                break;
            }
        case FIELD_MERGE_BARCODE:
            {
                FieldMergeBarcode fmb = (FieldMergeBarcode)f;
                if (fmb.getDisplayResult().contains(fmb.getBarcodeValue()))
                {
                    fmb.remove();
                }
                break;
            }
        default:
            {
                assert true;//nothing to do
            }
    }
}
1 Like