Unable to Remove Blank 2nd Page

Using Aspose.Words for .Net

I have an RTF document that is appearing with a blank 2nd page. Even if I remove the blank paragraph that is after the last table, the PageCount is still 2 and when exported as a PDF the blank page shows up.

Test File: Blank 2nd Page Document.zip (11.8 KB)

Here’s how the issue can be reproduced:

var document = new Document(@"Blank 2nd Page Document.rtf");
// page count is 2
document.LastSection.Body.LastChild.Remove();
document.LastSection.Body.Remove();
document.LastSection.Remove();
// page count is still 2
document.Save(@"Blank 2nd Page Document.pdf");

What’s needed to remove any trailing blank pages please?

@ChrisRickwood In your document there is an empty section at the end, which can be remove. Also, please note, in MS Word documents table cannot be the last node, an empty paragraph is always added after the table:

So, you cannot simply remove the last empty paragraph, but you can reduce it’s size. Please see the following code:

Document doc = new Document(@"C:\Temp\in.rtf");

// Remove empty section at the end of the document.
while (IsSectionEmpty(doc.LastSection))
    doc.LastSection.Remove();

// Reduce size of the last paragraph break to zero.
doc.LastSection.Body.LastParagraph.ParagraphBreakFont.Size = 0;
            
doc.Save(@"C:\Temp\out.pdf");
private static bool IsSectionEmpty(Section sect)
{
    return string.IsNullOrEmpty(sect.Body.ToString(SaveFormat.Text).Trim())
        && sect.Body.GetChildNodes(NodeType.Shape, true).Count == 0;
}

Ok thanks! That seems to fix saving as PDF.

1 Like