Aspose.Words provide API for working with headers and footers in Microsoft Word document. We can use the Section.HeadersFooters object to get the collection of header/footer in a document section. The HeaderFooter class is a container for section header or footer text. That being said, HeaderFooter is a section-level node and can only be a child of a section. There can be only one or each HeaderFooter of HeaderFooterType in a section.
The following code example shows how to remove header and footer from a Word document:
You can also do the same using the Open XML SDK. At the same time, note that it looks somewhat more complicated and more cumbersome.
The method works with the document you specify, deleting all of the header and footer parts and references to those parts. The code starts by opening the document, using the Open
method and indicating that the document should be opened for read/write access (the final true parameter). Given the open document, the code uses the MainDocumentPart property to navigate to the main document, storing the reference in a variable named docPart.
Given a reference to the document part, the code next determines if it has any work to do─that is, if the document contains any headers or footers. This simple method DeleteParts provides a shortcut for deleting a collection of parts.
To remove the stranded references, the code first retrieves a collection of HeaderReference elements, converts the collection to a List, and then loops through the collection, calling the Remove method for each element found.
The following code example shows how to remove header and footer from a Word document: