Aspose.Words: how to remove blank lines from a section

I need to remove extra blank lines from the word document. I tried using the following code line: doc.Range.Replace("\r\f", "\f", true, true) without any success.
The process I am working adds a header section to the document which causes the parts of the section content to spill into a new blank page. However in most cases there isn’t much content in the section, but instead there are many line breaks. Ideally, I would like to remove all blank likes (line breaks) at the end of each section.
Can you advise how to do this? Thanks in advace.

Hello!
Thank you for your inquiry.
I’m not sure it is possible to remove them with your approach. At least you won’t remove them all if there are several empty paragraphs at the end of some section.
You can perform this using some manipulations with Paragraph class. First select all paragraphs in the document using Document.SelectNodes:

NodeList paras = doc.SelectNodes("//Body/Paragraph");

Then check Paragraph.IsEndOfSection and Paragraph.HasChildNodes (inherited from CompositeNode) traversing the collection in reverse direction. Remove the unneeded paragraphs from the document with Paragraph.Remove (inherited from Node).
To make this algorithm more effective (and a bit more complex) you can first select all sections ("//Section"). Then traverse children of each section’s body (Section.Body) in reverse order finding empty paragraphs. In this case you will have two loops, one nested in another but you won’t check Paragraph.IsEndOfSection and also inner loop will break once it founds a non-empty paragraph. That’s why it will be faster.
Please let me know whether this helps you or not.
Regards,

Following your suggestion, this code works on removing all the line breaks. I noticed that on a single pass, not all the empty paragraphs were removed, even if I made the inner for loop run in reverse order.

// remove all the empty line breaks and empty pages
bool brepeat = true;
while (brepeat)
{
    brepeat = false;
    foreach (Section srcSection in doc.Sections)
    {
        // loop through all block level nodes (paragraphs and tables) in the body of the section.
        foreach (Node srcNode in srcSection.Body)
        {
            // remove node if it is the last empty paragarph in the section.
            Paragraph para = srcNode as Paragraph;
            if ((para != null) && para.IsEndOfSection && !para.HasChildNodes)
            {
                para.Remove();
                brepeat = true;
            }
        }
    }
}

Hello!
This looks very straightforward but should solve the task. If you are satisfied with performance then leave as is.
Regards,