Removing empty mergefields and corresponding tablerows

Hi,
I am new with Aspose and I am trying to populate a table with 2 columns and a lot of rows. Each cell contains one mergefield and populating the fields works fine but I am stuck on the part where I try to remove the table rows which contain empty merge fields. I have been looking for this for quite a while now and I just can’t get it to work. Removing the empty mergefields was easy, but actually removing the whole row (so there is a decent layout) is just killing me…
If my explanation isn’t clear enough, please ask!
Any help would be really appreciated!

Hi there,
Thanks for your inquiry.
Please try using the code below which should achieve this.

// Remove all merge fields inside a table and remove the row it's contained in as well.
foreach(FieldStart fieldStart in doc.GetChildNodes(NodeType.FieldStart, true))
{
    if (fieldStart.FieldType == FieldType.FieldMergeField)
    {
        Row row = (Row) fieldStart.GetAncestor(NodeType.Row);
        if (row != null)
            row.Remove();
    }
}
// Remove the remaining merge fields.
doc.MailMerge.DeleteFields();

Thanks,

Hi Adam
Thanks for the fast reply but the problem stays the same. I will add some illustrations of what the exact problem is.

Thanks a lot in advance!

Hi there,
Thanks for this additional information.
Please try with this small change to the code:

foreach(FieldStart fieldStart in doc.GetChildNodes(NodeType.FieldStart, true).ToArray())

Also please make sure you are running the first part of the code before calling DeleteFields.
If this is still not working, could you please attach your template here for testing? We will then provide you with some further suggestions.
Thanks,

The .ToArray() solved the problem!

Thanks Adam

Hi Adam,
Making my solution more and more complex I got stuck on another point related to my previous question.
If a mergefield (used to populate a bullet list) is empty I am able to remove the mergefield but the bullet is remaining on my document!
For the solution I am looking in the direction of:

if (fieldStart.FieldType == FieldType.FieldMergeField)
{
    Row row = (Row) fieldStart.GetAncestor(NodeType.Row);
    if (row != null)

But I don’t know what kind of NodeType I should use.
Any help would be really appreciated!

Hi

Thanks for your inquiry. List item is just simple paragraph. So you should use NodeType.Paragraph.
Please let me know if you need more assistance, I will be glad to help you.
Best regards.

Thanks for the reply but this is removing to much. I only want to remove this field and the bullet list. Not the entire paragraph.
Is this possible?

Hi

Thanks for your request. Could you please attach your document and expected output? I will investigate and provide you more information.
Best regards,

Hi Alexey!
Thanks for the reply and for taking the time to investigate my problem!
I added a word document as an attachment wich will explain my document, my problem and my expedted output.
The problem exists with creating the EXPECTED OUTPUT (possibility 3)
Thanks again!

Hi

Thank you for additional information. Maybe you should try using code like the following:

if (fieldStart.FieldType == FieldType.FieldMergeField)
{
    Paragraph paragraph = (Paragraph) fieldStart.GetAncestor(NodeType.Paragraph);
    if (paragraph != null && paragraph.IsListItem)
        paragraph.Remove();
}

Best regards,

This solved the problem.
Thanks a lot!