Delete Header and Footer in Word using Java

This article guides how to delete header and footer in Word using Java. It has the detailed steps to write the application, the reference to the resources required for establishing the development environment, and a ready-to-run code to remove header and footer in Word using Java. You will learn the mechanism to remove all the headers and footers or remove the selected headers or footers based on your requirements.

  1. Set the IDE to use Aspose.Words for Java to delete headers and footers
  2. Load the source Word file using the Document class object for removing headers/footers
  3. Run a for loop on all the sections of the loaded document
  4. For removing all the headers and footers without any check, call the clear() method
  5. For removing selected headers or footers check the HeaderFooterType property
  6. Save the resultant World file after removing the desired headers and footers

These steps define how to delete header and footer in Word using Java. Start the process by loading the source Word file and implementing a for loop to parse all the sections in the Word file. In each iteration, either call the clear() method from the HeaderFooterCollection to remove all the items or access the target header/footer and remove it individually.

The above code demonstrates how to remove header and footer in Word using Java. Various headers and footers in each section of a Word file can be accessed using the HeaderFooterType. For deleting all the headers/footers from a particular section, call the clear() method in the collection, and for deleting selected entries, first filter them like for deleting the header of the first page only (when the first-page headers/footers are different than the rest of the document), use the HeaderFooterType.HEADER_FIRST value from the enumerator.

This article has taught us how to remove footer in Word using Java by filtering it if required. To remove all the section breaks from a World file, refer to the article on how to remove all section breaks in Word using Java.