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

Free Support Forum - aspose.com

Mail merge label / value table and row removeal

I have the requirement to use mail merge in a label / field table setup where the contents of the "fields" is inserted by mail merge. This is how the template looks:

Label 1 $field_1$
Label 2 $field_2$
Label 3 $field_3$

If there is no value for one of the fields I would like to delete the entire row from the table. How can this be done?

Best regards,
Jethro

Hi Jethro,

Thanks for your inquiry. Please use the following code example to achieve your requirements. Hope this helps you. I have attached the input and output documents with this post for your kind reference.


Document doc = new Document(MyDir + "Test02.docx");

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

String[] input_names = new String[] { "Label1", "Label2", "Label3" };

Object[] input_data = new Object[] { "Label1", "", "Label3" };

doc.getMailMerge().execute(input_names, input_data);

doc.save(MyDir + "Out.docx");

public class MailMergeRemoveRow implements IFieldMergingCallback {

public void fieldMerging(FieldMergingArgs e) throws Exception {

if (e.getFieldValue().toString().trim().equals(""))

{

Row row = (Row)e.getField().getStart().getAncestor(NodeType.ROW);

if(row != null)

row.remove();

}

}

public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {

}

}

Thank you for your extensive and quick reply, this works great.

Hi Jethro,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.