Remove blank lines with RemoveEmptyParagraphs- or RemoveEmptyRegions- or?

Hi,
On a different application, we have a need to only list certain types of parties. Here is an example of the template:
Line above
{ MERGEFIELD :TableStart_Participants" }( IF {MERGEFIELD PARTY_ParticipantTypeCode } = "GRANDMOM" {MERGEFIELD "PARTY_FirstName" } { MERGEFIELD "PARTY LastName" }" ""){ MERGEFIELD "TableEnd_Participants" }
Line below
So the only thing between the tablestart and table end is an if statement. There are 5 parties in the database, but only one is type “GRANDMOM”, so in 4 cases the if statement should result in ‘’ and there would be nothing between the tablestart and tableend. We would like the document to show:
Line above
Grandma Jones
Line below

Instead we are getting 4 blank lines between Line above and Grandma Jones:
*Line above

Grandma Jones
Line below*
Here is how the code looks now:

string newDoc = String.Concat(caseDocsUnc, CaseNumber, "_", FileNumber, "\\", TemplateFile.Substring(0, TemplateFile.Length - 4), " ", dateTimeSuffix, ".DOC");
doc.MailMerge.RemoveEmptyParagraphs = true;
doc.MailMerge.RemoveEmptyRegions = true;
doc.MailMerge.ExecuteWithRegions(dsMerge);
doc.Save(newDoc);

Can you help me understand what needs to be changed to make this suppress the blank lines?
Thanks
Karen

Hi Karen,

Thanks for your inquiry and your detailed explanation. Could you also attach your input template here for testing? This will make it easier for us to see where the problem is.

Thanks,

Yes, thank you.
The first attachment (Buch …) allows you to focus on the one area of question
The second (Buchanan …) is the document the user sent to me. We actually want to do this in the table at the top. Thinking the table was the problem, I created the test area right below it. When I failed to get that to work either, I decided to post.
Thanks
Karen

Hi Karen,
Thank you for additional information. Aspose.Words does not remove these paragraphs because it does not consider them as empty. Technically, they are not empty, they contains IF field. I logged this issue in our defect database. We will consider changing the behavior of RemoveEmptyParagraphs so Aspose.Words considered such paragraphs as empty and remove them.
For now, you can remove these paragraphs by post-processing your documents:

DataTable data = new DataTable("Participants");
data.Columns.Add("PARTY_ParticipantTypeCode");
data.Columns.Add("PARTY_FirstName");
data.Columns.Add("PARTY_LastName");
data.Rows.Add(new object[]
{
    "test",
    "Alexey",
    "Noskov"
});
data.Rows.Add(new object[]
{
    "GRANDMOM",
    "Andrey",
    "Noskov"
});
data.Rows.Add(new object[]
{
    "",
    "John",
    "Owens"
});
data.Rows.Add(new object[]
{
    "",
    "Nick",
    "Winchester"
});
data.Rows.Add(new object[]
{
    "",
    "James",
    "Bond"
});
Document doc = new Document(@"Test001\in.doc");
doc.MailMerge.RemoveEmptyParagraphs = true;
doc.MailMerge.RemoveEmptyRegions = true;
doc.MailMerge.ExecuteWithRegions(data);
// Remove paragraphs with no text.
Node[] paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).ToArray();
foreach(Node paragraph in paragraphs)
{
    if (string.IsNullOrEmpty(paragraph.ToTxt().Trim()))
        paragraph.Remove();
}
doc.Save(@"Test001\out.doc");

hope this helps.
Best regards,

Thank you, I’ve incorporated this and it is very close. However, it is removing every blank line in the document. We need to only remove the ones where there is an if statement that will not produce any tex and we don’t know how to test for this. I’ve attached an updated test template showing the problem.

Hi
Thanks for your request. You can use IFieldMergingCallback to improve the code. Please see the following code:

[Test]
public void Test001()
{
    DataTable data = new DataTable("Participants");
    data.Columns.Add("PARTY_ParticipantTypeCode");
    data.Columns.Add("PARTY_FirstName");
    data.Columns.Add("PARTY_LastName");
    data.Rows.Add(new object[]
    {
        "test",
        "Alexey",
        "Noskov"
    });
    data.Rows.Add(new object[]
    {
        "GRANDMOM",
        "Andrey",
        "Noskov"
    });
    data.Rows.Add(new object[]
    {
        "",
        "John",
        "Owens"
    });
    data.Rows.Add(new object[]
    {
        "",
        "Nick",
        "Winchester"
    });
    data.Rows.Add(new object[]
    {
        "",
        "James",
        "Bond"
    });
    Document doc = new Document(@"Test001\in.doc");
    EmptyParagraphsHelper helper = new EmptyParagraphsHelper();
    doc.MailMerge.FieldMergingCallback = helper;
    doc.MailMerge.ExecuteWithRegions(data);
    helper.RemoveEmptyParagraphs();
    doc.Save(@"Test001\out.doc");
}
private class EmptyParagraphsHelper: IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
        // Add parent paragraph into the collection.
        Paragraph parent = args.Field.Start.ParentParagraph;
        if (!mParagraphs.Contains(parent))
            mParagraphs.Add(parent);
    }
    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do nothing here.
    }
    ///
    /// Call this method after executign mail merge to remove empty paragraphs.
    ///
    public void RemoveEmptyParagraphs()
    {
        foreach(Paragraph paragraph in mParagraphs)
        {
            if (string.IsNullOrEmpty(paragraph.ToTxt().Trim()) && paragraph.ParentNode != null)
                paragraph.Remove();
        }
        mParagraphs.Clear();
    }
    private ArrayList mParagraphs = new ArrayList();
}

Hope this helps.
Best regards,

Thank you.
We kind of liked the simplicity of your first solution. I ended up going with it and just modifying it a bit to check GetText too so that it doesn’t take out intentional carraige returns in the document with no field codes on the lines. It seems to work, but please let me know if there is a reason I should not be doing it this way.

// Remove paragraphs with no text.
Node[] paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).ToArray();
foreach(Node paragraph in paragraphs)
{
    if (string.IsNullOrEmpty(paragraph.ToTxt().Trim()))
        if (!(string.IsNullOrEmpty(paragraph.GetText().Trim())))
            paragraph.Remove();
}

Hi
Thanks for your request. The method I suggested in my previous answer process only paragraphs that contained merge fields. Your code process all paragraphs in your document. So your code is little less efficient. However, if your documents are small, your solution is good.
Best regards,

Hi,
I’m still having a little trouble with this. If the text within the if statement has a paragraph marker in it, it is not processing that correctly. I at first thought the problem was with my ‘simple’ approach, so I changed the code to use the handler as you proposed and I am still having a problem.
I’ve attached another test template to illustrate the issue. My example is silly but in the real world, users really need to do this. For example, in a child support case there can be multiple participants in a case that are alleged to be fathers. They have a participant type of ‘alleged father’, and several of the legal documents users need to produce will have one paragraph for each alleged father on the case.

Hi
Thanks for your request and sorry for delayed response. Maybe you should replace your IF fields with static text before removing empty paragraphs. Please see the following link for more information:https://docs.aspose.com/words/net/replace-fields/
Hope this helps.
Best regards,

Hi,

I would also like to add that we are working on mail merge cleanup options right at the moment. It will be possible to remove empty paragraphs that contain nothing but whitespace, as well as get rid of containing IF (or whatever) fields. Expect that in the forthcoming release.

Thanks.

The issues you have found earlier (filed as WORDSNET-5618) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Thanks, I’ve downloaded the .Net version of this and I see that RemoveEmptyRegions and RemoveEmptyParagraphs are now obsolete, and that new Cleanup options allow more choice over what to remove. I think I want to use RemoveEmptyParagraphs and RemoveContainingFields, but it’s not clear to me what the two in the middle say they are doing and if they apply to us. I checked in the HowTo sections of Help, but found references to RemoveEmptyRegions and callbacks. Do you have an example of usage of these new options in your help or on this site anywhere yet?
Thanks
Karen

Hi Karen,

Thanks for your inquiry.

Yes those two options sounds useful for you. The other option RemoveEmptyRegions will remove any regions that are not merged e.g if the TableStart and TableEnd fields still remain in the fully merged document then they will be removed along with any content inside the region.

The RemoveUnusedFields option will remove any merge fields that remain in the document.

I will be fully documenting these changes in the documentation shortly.

If I can help you with anything else, please feel free to ask.

Thanks,