We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to use IFieldMergingCallback class

Using IFieldMergingCallback class, when mergefield value is null or “”, the entire paragraph is removed.
However if text other than mergefields are included in the same paragraph, we like to avoid removing.
Although the code shown in the following (eventHandler code) was implemented to realize above,
it does not operate, as meant.
Please instruct us the right way.

(Word document template : Template.docx)

Name: {MERGEFIELD "Name"}
Tel: {MERGEFIELD "TelNumber"}
Mail: {MERGEFIELD "MailAddress"}

Name: {NEXT \*MERGEFORMAT}{MERGEFIELD "Name"}
Tel: {MERGEFIELD "TelNumber"}
Mail: {MERGEFIELD "MailAddress"}

-----------------------------------------------------------------------------------------------------------------------------
(Data source : source.csv)

Name,TelNumber,Adress
"Mary Smith","03-1111-xxxx","xxx-oooo@geegle.co.jo"
"Jack Johnson","06-4444-xxxx",""

-----------------------------------------------------------------------------------------------------------------------------
(Word document actual result : Result.docx)

Name: Mary Smith
Tel: 03-1111-xxxx
mail: xxx-oooo@geegle.co.jo

Name: Jack Johnson
Tel: 06-4444-xxxx

*↑Letters ‘mail :’ are removed.

-----------------------------------------------------------------------------------------------------------------------------
(Word document result to mean : Target.docx)

Name: Mary Smith
Tel: 03-1111-xxxx
mail: xxx-oooo@geegle.co.jo

Name: Jack Johnson
Tel: 06-4444-xxxx
mail:

-----------------------------------------------------------------------------------------------------------------------------
(eventHandler code)

class HandleMergeFieldRemoveEmptyLine implements IFieldMergingCallback{

    public void fieldMerging(FieldMergingArgs args) throws Exception {

        String fieldValue = (String)args.getFieldValue();
        Paragraph parent = args.getField().getStart().getParentParagraph();

        // If the field value is null or empty string and the text of this paragraph is null or empty String then remove the entire paragraph.
        if(fieldValue == null || fieldValue.equals("")){

            if(parent.getParentNode() != null) {

                if(parent.getText().equals("") || parent.getText() == null) {
                    parent.remove();
                }
            }
        }
        else if (fieldValue.trim() == null || fieldValue.trim().equals("")) {
            // Otherwise if the field value contains special chars only, then remove all content and leave a blank line
            parent.removeAllChildren();
        }
    }

    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
    }
}

Hi Yuji,

Thanks for your inquiry. I think, you can achieve what you need by using the following code snippet:

class HandleMergeField implements IFieldMergingCallback {

public void fieldMerging(FieldMergingArgs args) throws Exception {
    if (args.getFieldValue().equals("") || args.getFieldValue() == null) {
        DocumentBuilder builder = new DocumentBuilder(args.getDocument());
        builder.moveToMergeField(args.getFieldName());
        Paragraph p = builder.getCurrentParagraph();
        boolean removePara = true;
        for (Run r : p.getRuns()) {
            if (r.getText().trim().length() > 0) {
                removePara = false;
                break;
            }
        }
        if (removePara)
            p.remove();
    }
}
public void imageFieldMerging(ImageFieldMergingArgs e) throws Exception {
    /* DO NOTHING */
}
}

I hope, this will help.

Best Regards,

Hi Awais,

Thank you for your reply. We could make it with your lovely code snippet!

We really appreciate it. Thanks a lot.

Yuji