Articles in this section
Category / Section

How to remove empty page at end of Word document

3 mins read

You may have various requirement to remove the last empty pages in a Word document. Syncfusion Word library (Essential DocIO) is a non-UI component which used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can iterate into Word document elements and remove empty paragraphs using C# and VB.NET

The paragraph in the last empty page can be considered as empty if the child entities count is zero, contains only the elements invisible in viewer (like bookmark, field marks, etc.), or text contains only escape characters (like white space, new line, etc.).

How to remove empty page in Word document using C# and VB.NET

To remove empty page in Word document, we suggest to remove empty paragraphs with invisible elements from the Word document.

The following code demonstrates how to remove the empty page in a Word document.

//Opens an existing Word document
WordDocument document = new WordDocument(“Adventure.docx”, FormatType.Docx);
WTextBody textBody = null;
//Iterates sections in Word document.
for (int i = document.Sections.Count - 1 ;  i >= 0 ; i--)
    //Accesses the Body of section where all the contents in document are apart
    textBody = document.Sections[i].Body;
    //Removes the last empty page in the Word document
    //Removes the empty sections in the document
    if (textBody.ChildEntities.Count == 0)
     int SectionIndex = document.ChildEntities.IndexOf(document.Sections[i]);
//Saves and closes the Word document
document.Save("Sample.docx", FormatType.Docx);


Dim document As WordDocument = New WordDocument(“Adventure.docx”, FormatType.Docx)
Dim textbody As WTextBody = Nothing
'Iterates sections in Word document.
Dim i As Integer = (document.Sections.Count - 1)
Do While (i >= 0)
    'Accesses the Body of section where all the contents in document are apart
    textBody = document.Sections(i).Body
    'Removes the last empty page in the Word document
    'Removes the empty sections in the document
    If (textBody.ChildEntities.Count = 0) Then
        Dim SectionIndex As Integer = document.ChildEntities.IndexOf(document.Sections(i))
    End If
    i = (i - 1)
'Saves and closes the Word document
document.Save("Sample.docx", FormatType.Docx)

The following code demonstrates how to remove empty paragraphs in the Word document.


private void RemoveEmptyItems(WTextBody textBody)
    //A flag to determine any renderable item found in the Word document.
    bool IsRenderableItem = false;
    //Iterates into textbody items.
    for (int itemIndex = textBody.ChildEntities.Count - 1 ; itemIndex >= 0 && !IsRenderableItem; itemIndex--)
        #region Removes last empty paragraph
        //Checks item is empty paragraph and removes it.
        if (textBody.ChildEntities[itemIndex] is WParagraph)
            WParagraph paragraph = textBody.ChildEntities[itemIndex] as WParagraph;
            //Iterates into paragraph
            for (int pIndex = paragraph.Items.Count - 1; pIndex >= 0; pIndex--)
                ParagraphItem paragraphItem = paragraph.Items[pIndex];
                //Removes page breaks
                if ((paragraphItem is Break && (paragraphItem as Break).BreakType == BreakType.PageBreak))
                //Check paragraph contains any renderable items.
                else if (!(paragraphItem is BookmarkStart || paragraphItem is BookmarkEnd))
                    //Found renderable item and break the iteration.
                    IsRenderableItem = true;
            //Remove empty paragraph and the paragraph with bookmarks only
            if (paragraph.Items.Count == 0 || !IsRenderableItem)


 Private Sub RemoveEmptyItems(ByVal textBody As WTextBody)        '
A flag to determine any renderable item found in the Word document.
       Dim IsRenderableItem As Boolean = false
        'Iterates into textbody items.
        Dim itemIndex As Integer = (textBody.ChildEntities.Count - 1)
        Do While ((itemIndex >= 0)  _
                    AndAlso Not IsRenderableItem)
            #            Dim Removes As region
            Dim empty As last
            If (TypeOf textBody.ChildEntities(itemIndex) Is WParagraph) Then
                Dim paragraph As WParagraph = CType(textBody.ChildEntities(itemIndex),WParagraph)
                'Iterates into paragraph
                Dim pIndex As Integer = (paragraph.Items.Count - 1)
                Do While (pIndex >= 0)
                    Dim paragraphItem As ParagraphItem = paragraph.Items(pIndex)
                    'Removes page breaks
                    If (TypeOf paragraphItem Is (Break  _
                                AndAlso (CType(paragraphItem,Break).BreakType = BreakType.PageBreak))) Then
                    End If
                    'Check paragraph contains any renderable items.
                    If Not (TypeOf paragraphItem Is (TypeOf (BookmarkStart OrElse paragraphItem) Is BookmarkEnd)) Then
                        'Found renderable item and break the iteration.
                        IsRenderableItem = true
                        Exit For
                    End If
                    pIndex = (pIndex - 1)
                'Remove empty paragraph and the paragraph with bookmarks only
                If ((paragraph.Items.Count = 0)  _
                            OrElse Not IsRenderableItem) Then
                End If
            End If
            itemIndex = (itemIndex - 1)
End Sub


A complete working example of how to remove empty page in Word document in C# can be downloaded from Remove empty page in Word

Take a moment to peruse the documentation, where you can find basic Word document processing options along with features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly PDF and Image conversions with code examples.


Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.


Did you find this information helpful?
Help us improve this page
Please provide feedback or comments
Please sign in to leave a comment