How to remove blank pages document

Hi Team,
How to delete the blank page from document.

Please find below code:

Stream HeaderTemplate = ePAPS_FileUtility.GetFile(ConfigurationManager.AppSettings["Templates"] + "PAPHeader.docx");
Stream ApprovalTemplate = ePAPS_FileUtility.GetFile(ConfigurationManager.AppSettings["Templates"] + "PAPApprovals.docx");
Aspose.Words.Document headerDoc = AsposeHelper.Document.WordDoc(HeaderTemplate);
DataTable SecData = Data.Tables[2];
engine.BuildReport(headerDoc, report);
Document doc = AsposeHelper.Document.WordDoc(ApprovalTemplate);
engine.BuildReport(doc, report);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("Table");
String strHTML = buildHtml(report);
builder.InsertHtml(strHTML);
String strHTML_Approvers_Comments = BuildHtml_Approvers_Comments(report);
builder.InsertHtml(strHTML_Approvers_Comments);
String strHTML_Taxonomy = buildESPTaxonomyHtml(Request_Number);
builder.InsertHtml(strHTML_Taxonomy);
Aspose.Words.Font font = builder.Font;
builder.MoveToBookmark("TOC");
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
builder.MoveToBookmark("Main");
await WriteData(SecData, builder, Request_Number: Request_Number, soeid: soeid);
headerDoc.AppendDocument(doc, Aspose.Words.ImportFormatMode.KeepSourceFormatting);
setTOCStyle(headerDoc, StyleIdentifier.Toc1);
setTOCStyle(headerDoc, StyleIdentifier.Toc2);
setTOCStyle(headerDoc, StyleIdentifier.Toc3);
headerDoc.UpdateFields();
//RemoveBlankPageFromWord(headerDoc);

Also find attached document: WordWithBlankPages.docx (19.6 KB)

@rs43733 In your document empty pages are produced by a bunch of empty paragraphs. To preserve original document structure, I would suggest to remove these empty paragraphs from the document. For example see the following code that removes empty paragraph of if the previous paragraph is also empty.

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

doc.GetChildNodes(NodeType.Paragraph, true).Where(p => IsEmptyParagraph(p) && IsEmptyParagraph(p.NextSibling))
    .ToList().ForEach(p => p.Remove());

doc.Save(@"C:\Temp\out.docx");
private static bool IsEmptyParagraph(Node node)
{
    if (node == null || node.NodeType != NodeType.Paragraph)
        return false;

    Paragraph p = (Paragraph)node;

    // Consider paragraph as empty if it does not have text and shapes.
    return string.IsNullOrEmpty(p.ToString(SaveFormat.Text).Trim()) &&
        (p.GetChildNodes(NodeType.Shape, true).Count == 0);
}

I am still getting an error while removing blank paragraph.

Below is my code:

Stream HeaderTemplate = ePAPS_FileUtility.GetFile(ConfigurationManager.AppSettings["Templates"] + "PAPHeader.docx");
Stream ApprovalTemplate = ePAPS_FileUtility.GetFile(ConfigurationManager.AppSettings["Templates"] + "PAPApprovals.docx");
Aspose.Words.Document headerDoc = AsposeHelper.Document.WordDoc(HeaderTemplate);
DataTable SecData = Data.Tables[2];
engine.BuildReport(headerDoc, report);
Document doc = AsposeHelper.Document.WordDoc(ApprovalTemplate);
engine.BuildReport(doc, report);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("Table");
String strHTML = buildHtml(report);
builder.InsertHtml(strHTML);
String strHTML_Approvers_Comments = BuildHtml_Approvers_Comments(report);
builder.InsertHtml(strHTML_Approvers_Comments);
String strHTML_Taxonomy = buildESPTaxonomyHtml(Request_Number);
builder.InsertHtml(strHTML_Taxonomy);
Aspose.Words.Font font = builder.Font;
builder.MoveToBookmark("TOC");
builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u");
builder.MoveToBookmark("Main");
await WriteData(SecData, builder, Request_Number: Request_Number, soeid: soeid);

//pass the document 
headerDoc.AppendDocument(RemoveBlankPages(doc), Aspose.Words.ImportFormatMode.KeepSourceFormatting);
setTOCStyle(headerDoc, StyleIdentifier.Toc1);
setTOCStyle(headerDoc, StyleIdentifier.Toc2);
setTOCStyle(headerDoc, StyleIdentifier.Toc3);
headerDoc.UpdateFields();
return headerDoc;

public Document RemoveBlankPages(Document doc)
{
    doc.GetChildNodes(NodeType.Paragraph, true).Where(p => IsEmptyParagraph(p) && IsEmptyParagraph(p.NextSibling))
        .ToList().ForEach(p => p.Remove());

    return doc;
}

private static bool IsEmptyParagraph(Node node)
{
    if (node == null || node.NodeType != NodeType.Paragraph)
        return false;

    Paragraph p = (Paragraph)node;

    // Consider paragraph as empty if it does not have text and shapes.
    return string.IsNullOrEmpty(p.ToString(SaveFormat.Text).Trim()) &&
        (p.GetChildNodes(NodeType.Shape, true).Count == 0);
}

Plz find attached error:

@rs43733 You should use System.Linq:

using System.Linq;

Linq class is already added.

@rs43733 Unfortunately, I cannot reproduce the issue. Which version of Aspose.Words do you use? Could you please create a simple console application that will demonstrate the problem?

I fixed the issue by this code :slight_smile:

public Document RemoveBlankParagraph(Document doc)
{
    foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
    {
        if (IsEmptyParagraph(para) && IsEmptyParagraph(para.NextSibling))
        {
            para.Remove();
        }
    }

    return doc;
}

private static bool IsEmptyParagraph(Node node)
{
    if (node == null || node.NodeType != NodeType.Paragraph)
        return false;

    Paragraph p = (Paragraph)node;

    // Consider paragraph as empty if it does not have text and shapes.
    return string.IsNullOrEmpty(p.ToString(SaveFormat.Text).Trim()) &&
        (p.GetChildNodes(NodeType.Shape, true).Count == 0);
}

@rs43733 It is perfect that you managed to resolve the problem. Please feel free to ask in case of any issues. We are always glad to help you.