How to remove empty page at end of Word document
You may have various requirements to remove the last empty pages in a Word document. Syncfusion® Word Library (Essential® DocIO) is a non-UI component used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can iterate through Word document elements and remove empty paragraphs using C# and VB.NET.
The paragraph in the last empty page can be considered empty if the child entities count is zero, contains only elements invisible in the viewer (like bookmarks, field marks, etc.), or the text contains only escape characters (like whitespace, newline, etc.).
How to remove an empty page in a Word document using C# and VB.NET
To remove an empty page in a Word document, we suggest removing empty paragraphs with invisible elements from the Word document.
The following code demonstrates how to remove the empty page in a Word document.
C#
// 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 the document are apart
textBody = document.Sections[i].Body;
// Removes the last empty page in the Word document
RemoveEmptyItems(textBody);
// Removes the empty sections in the document
if (textBody.ChildEntities.Count == 0)
{
int SectionIndex = document.ChildEntities.IndexOf(document.Sections[i]);
document.ChildEntities.RemoveAt(SectionIndex);
}
}
// Saves and closes the Word document
document.Save("Sample.docx", FormatType.Docx);
document.Close();
VB
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 the document are apart
textBody = document.Sections(i).Body
' Removes the last empty page in the Word document
RemoveEmptyItems(textBody)
' Removes the empty sections in the document
If (textBody.ChildEntities.Count = 0) Then
Dim SectionIndex As Integer = document.ChildEntities.IndexOf(document.Sections(i))
document.ChildEntities.RemoveAt(SectionIndex)
End If
i = (i - 1)
Loop
' Saves and closes the Word document
document.Save("Sample.docx", FormatType.Docx)
document.Close()
The following code demonstrates how to remove empty paragraphs in the Word document.
C#
private void RemoveEmptyItems(WTextBody textBody)
{
// A flag to determine if any renderable item is 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 if item is an 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))
paragraph.Items.RemoveAt(pIndex);// Check if paragraph contains any renderable items.
else if (!(paragraphItem is BookmarkStart || paragraphItem is BookmarkEnd))
{
// Found renderable item and breaks the iteration.
IsRenderableItem = true;
break;
}
}
// Remove empty paragraph and the paragraph with bookmarks only
if (paragraph.Items.Count == 0 || !IsRenderableItem)
textBody.ChildEntities.RemoveAt(itemIndex);
}#endregion
}
}
VB
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 paragraph 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 paragraph.Items.RemoveAt(pIndex) End If 'Check if 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) Loop 'Remove empty paragraph and the paragraph with bookmarks only If ((paragraph.Items.Count = 0) _ OrElse Not IsRenderableItem) Then textBody.ChildEntities.RemoveAt(itemIndex) End If End If #endregion itemIndex = (itemIndex - 1) Loop End Sub
A complete working example of how to remove an empty page in a Word document in C# can be downloaded from Remove empty page in Word document.zip.
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 a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.