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

Free Support Forum - aspose.com

Check box filling based on dynamic value

Hi,

I have 2 check boxes in a paragraph of word document.

Based on merge field value i want to fill the check boxes, like if the value is Yes first check has to fill else value is NO 2 check box has to fill.

PASSPORT: This policy X «Passport» serves, or X «Passport» does not serve, as a master Passport policy.

Based on the passport value check box has to fill.

Suppose passport is Y first check box has to fill else Passport is N second check box has to fill.

Thanks,

Ravi

Hi Ravi,

Thanks for your inquiry. Please find attached sample input/output Word documents and execute the following code to meet this requirement:

Document doc = new Document(getMyDir() + "in.docx");
doc.getMailMerge().setFieldMergingCallback(new HandleMergeField());
doc.getMailMerge().execute(new String[] { "Passport" }, new Object[] { "N" });
doc.save(getMyDir() + "awjava-15.8.0.docx");
static class HandleMergeField implements IFieldMergingCallback
{
    public void fieldMerging(FieldMergingArgs e) throws Exception
    {
        if (e.getFieldName().equals("Passport")) {
            Document doc = (Document) e.getDocument();
            if (e.getFieldValue().equals("Y")) {
                FormField cbx = doc.getRange().getFormFields().get(0);
                cbx.setChecked(true);
            }
            else
            {
                FormField cbx = doc.getRange().getFormFields().get(1);
                cbx.setChecked(true);
            }
        }
    }
    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception
    {
        // Do nothing.
    }
}

Hope, this helps.

Best regards,

Hi Awais,
Thanks for your response.
There is one small change required in that output, I don’t want to show the passport value in the out put document.

Hi Ravi,

Thanks for your inquiry. Please do the following change to meet this requirement.:

static class HandleMergeField implements IFieldMergingCallback
{
    public void fieldMerging(FieldMergingArgs e) throws Exception
    {
        if (e.getFieldName().equals("Passport")) {
            Document doc = (Document) e.getDocument();
            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.moveToMergeField(e.getFieldName());
            if (e.getFieldValue().equals("Y")) {
                FormField cbx = doc.getRange().getFormFields().get(0);
                cbx.setChecked(true);
            }
            else
            {
                FormField cbx = doc.getRange().getFormFields().get(1);
                cbx.setChecked(true);
            }
        }
    }
    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception
    {
        // Do nothing.
    }
}

I hope, this helps.

Best regards,