This quick tutorial is designed to explain how to remove blank page in Word using C# like DOCX, DOC, etc. It provides details to configure the environment along with the steps to perform for the task and a sample code that implements the complete logic to erase blank page in Word using C#. You can run this code without installing any third-party software using any of the operating systems like Windows, Linux, or macOS.
Steps to Delete Empty Page in Word using C#
- Configure the project to add Aspose.Words library into the project
- Load the source Word file into the Document class object to remove empty pages
- Parse through all the pages in the source document and fetch each page separately
- Create a separate document with every single page only from the source document
- Analyze this single page document and check if any section contains some text/shape node or not
- Maintain a list of all the non-empty pages in the original document
- Append all the non-empty pages to the new Word file
- Save this file having the pages only with some text or shape
These steps provide details to delete the empty page in Word using C#. First, you load the source Word file and then parse through each of the pages in it such that for each page a new Word document is created. It is required because we have to analyze each page separately and the features of the Document class will help us to identify the non-empty pages. During the parsing, each non-empty page is added to a new Word file that will contain all such pages.
Code to Delete the Blank Page in Word using C#
This code to remove blank page from Word document using C# describes the complete logic where each page of the source Word file is saved into a new Word file and then analyzed for any content in it. During this process, the headers and footers are not considered however you may include those also. Once all the pages are analyzed and their numbers are saved in an array, this array is used to append all those pages to a new Word file.
In this tutorial, we removed all the blank pages from a Word file. If you want to learn some other functionality like converting Word documents to HTML, refer to the article on how to convert Word document to HTML using C#.