Last line of last paragrph getting disturb(getting extra space at end)

Hii , I want to remove Blank pages from Document so I use this Snippest
but in output one extra space add at last line of last paragraph

private void RemoveBlankPages()
        Document sourceDoc = new Aspose.Words.Document(@"C:\\Temp\\sourceDoc.docx");
        Document NewDoc = (Document)sourceDoc.Clone(false);
        for (int i = 0; i < sourceDoc.PageCount; i++)
            Document Page = (Document)sourceDoc.Clone(false);
            Page = sourceDoc.ExtractPages(i, 1);
            var pageText = Page.FirstSection.Body.ToString(SaveFormat.Text) + Page.LastSection.Body.ToString(SaveFormat.Text);
            var shapeCount = Page.FirstSection.Body.GetChildNodes(NodeType.Shape, true).Count + Page.LastSection.Body.GetChildNodes(NodeType.Shape, true).Count;
            if (string.IsNullOrEmpty(pageText.Trim()) && shapeCount == 0)
            NewDoc.AppendDocument(Page, ImportFormatMode.KeepSourceFormatting);
        if (NewDoc.Sections.Count > 0)
            NewDoc.FirstSection.PageSetup.RestartPageNumbering = true;
        sourceDoc = (Document)NewDoc.Clone(true);
        sourceDoc.Save(@"C:\\Temp\\Output.docx", SaveFormat.Docx);
    catch (Exception e)
        throw e;

SourceDoc.docx (2.0 MB)
OutputDoc.docx (2.0 MB)
Err.png (39.8 KB)
Err2.png (44.7 KB)

@Jayshiv1408 I have some comments about what you are doing and how you are doing it:

First, removing empty pages from an MS Word document is not easy to achieve because MS Word documents are flow documents, which means that they do not contain pages in their structure. The pages are calculated in the rendering process.

Second, what you see as extra empty space is actually a section break. When you append a document into another, it is appended by default in a new section, and the only way to remove this is by inserting everything in the same section. (Check here for more information.)

Third, your code could affect your document structure. You are basically looping over all the “pages” (which implies that every time you call the ExtractPage function, you are actually rendering the document behind the scenes) and appending each single page (if it’s not empty) in the final document, which creates a new section per page. But what happens if you have a table or a shape that uses more than one page? In the best case, it will be cut in two or more. A better approach to your solution could be to first get the indexes of the blank pages and then append those sections using the same method that you use. And if you want to remove the section breaks, you can insert all the content in the same section to avoid the section breaks in the document. See the following code:

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

// A List will hold blank page numbers
ArrayList emptyPageNumbers = new ArrayList();

// Extract each page as a separate Word document
int totalPages = doc.PageCount;
for (int i = 0; i < totalPages; i++)
    Document pageDoc = doc.ExtractPages(i, 1);

    // Get text representation of this Page and total count of Shapes
    int shapeCount = 0;
    string textOfPage = "";
    foreach (Section section in pageDoc.Sections)
        // Lets not consider the content of Headers and Footers
        textOfPage += section.Body.ToString(SaveFormat.Text);
        shapeCount += section.Body.GetChildNodes(NodeType.Shape, true).Count;

    // if text_of_Page is empty and does not contain any Shape nodes then consider this Page is blank
    if (string.IsNullOrEmpty(textOfPage.Trim()) && shapeCount == 0)

// Concatenate documents with non-empty pages again
Document final_Document = (Document)doc.Clone(false);

for (int i = 1; i < emptyPageNumbers.Count; i++)
    int index = (int)emptyPageNumbers[i - 1] + 1;
    int count = (int)emptyPageNumbers[i] - index;

    if (count > 0)
        final_Document.AppendDocument(doc.ExtractPages(index, count), ImportFormatMode.KeepSourceFormatting);

        // Insert everithing in the same section to avoid Section Breaks
        if(final_Document.Sections.Count > 1)
        final_Document.LastSection.PageSetup.RestartPageNumbering = true;


This solution (same as yours) consumes a lot of resources. So, it is up to you whether or not use it. A resource-friendly solution could be convert the document to PDF and then use the Aspose.Pdf API to remove the empty pages. Since a PDF document is a fixed document, it contains page information in its structure, making it easier to remove the empty pages.