How to Delete a Blank Page in Word using Java

This quick tutorial guides on how to delete a blank page in Word using Java. It has the details to set the development environment, a list of programming tasks for the application, and a runnable sample code demonstrating how to delete empty page in Word using Java. You will learn to access different types of content from the body of a section on a page and verify the existence of text or shape in it.

Steps to Delete Empty Page in Word using Java

  1. Set the environment to use Aspose.Words for Java to delete blank pages
  2. Load the source Word document and get its page count
  3. Parse through all the pages and get its text and shapes
  4. Save the list of page indexes where no text or shape is present
  5. Create a new Word document and add all the non-empty pages in it using the blank page numbers list
  6. Save the resultant Word file having no empty pages in it

These steps summarize how to remove blank page in Word DOC using Java. The employed logic is to load the source word file and parse each of its pages to save the indexes of blank pages with no text or shapes present. Subsequently, generate a new Word file and add all the pages from the source Word file into the new Word file whose indexes are not present in the list of empty pages indexes.

Code to Delete Blank Page in Word DOC using Java

This sample code demonstrates how to remove a blank page in Word using Java. We used the extractPages() method in the Document class to access the pages, the Section.getBody() method to extract the text and Section.getChildNodes() method with NodeType.SHAPE to extract the shapes on the page. You may modify this code to delete a single blank page based on its index if required.

This tutorial has guided us to remove a blank page in Word. If you want to delete all the comments in a Word file, refer to the article on how to delete comments from Word document in Java.

 English