Remove label text when no data in mail merge field

Hi,
I have the mail merge regions document like this example:

<<TableStart:Item>>
      Item Name:                 <<ItemName>
      Item Number:              <<ItemNumber>>
      Item Information:         <<ItemInformation>>
      ......

<<TableEnd:Item>>

Let’s say for example, if in the source XML, I have the no data for the merge field ItemInformation (<<ItemInformation>>. Is there a way to remove the whole row about Item Information (the merge field and the label text as well?

Hi
Thanks for your request. I think, you can use IFieldMergingCallback to achieve what you need:
https://reference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback/
I created a simple example for you. Please see the attached field and the code below:

[Test]
public void Test001()
{
    // Create a dummy datasource.
    DataSet ds = new DataSet();
    ds.ReadXml(@"Test001\data.xml");
    // Open template and execute mail merge.
    Document doc = new Document(@"Test001\in.doc");
    // Specify field merging callback that will remove paragraphs with empty mergefields.
    doc.MailMerge.FieldMergingCallback = new FieldMergingCallback();
    doc.MailMerge.ExecuteWithRegions(ds);
    doc.Save(@"Test001\out.doc");
}
private class FieldMergingCallback: IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
        if (string.IsNullOrEmpty((string) args.FieldValue))
        {
            // Get paragraph where the empty value should be inserted and remove it.
            Paragraph paragraph = (Paragraph) args.Field.Start.GetAncestor(NodeType.Paragraph);
            if (paragraph != null)
                paragraph.Remove();
        }
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // do nothing
    }
}

Hope this helps.
Best regards,

Works wonderful with paragraph but Alexey, I should mention all of my fields are in table. So how can I get the row with the empty field and delete it? Thank you.
Regards
Tld

Hi Alexey,
Don’t worry, I got it working with my table now. I just change your code to

public void FieldMerging(FieldMergingArgs args) <? XML : NAMESPACE PREFIX = O />
{
    if (string.IsNullOrEmpty((string) args.FieldValue))
    {
        // Get row where the empty value should be inserted and remove it.
        Row row = (Row) args.Field.Start.GetAncestor(NodeType.Row);
        if (row != null)
            row.Remove();
    }
}

Thank you very much.
Regards
tld

Hi
It is perfect that you managed to implement what you need. Please feel free to ask in case of any issues, we will be glad to help you.
Best regards,