How to: Remove the headers and footers from a word processing document (Open XML SDK)
Last modified: March 22, 2013
Applies to: Office 2013 | Open XML
In this article
Calling the Sample Method
How the Code Works
Confirm Header/Footer Existence
Remove the Header and Footer Parts
Work with the Document Content
Delete the Header and Footer References
This topic shows how to use the classes in the Open XML SDK 2.5 for Office to programmatically remove all headers and footers in a word processing document. It contains an example RemoveHeadersAndFooters method to illustrate this task.
To use the sample code in this topic, you must install the Open XML SDK 2.5. You must then explicitly reference the following assemblies in your project.
DocumentFormat.OpenXml (installed by the Open XML SDK)
You must also use the following using directives or Imports statements to compile the code in this topic.
You can use the RemoveHeadersAndFooters method to remove all header and footer information from a word processing document. Be aware that you must not only delete the header and footer parts from the document storage, you must also delete the references to those parts from the document too. The sample code demonstrates both steps in the operation. The RemoveHeadersAndFooters method accepts a single parameter, a string that indicates the path of the file that you want to modify.
The complete code listing for the method can be found in the Sample Code section.
The RemoveHeadersAndFooters 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. To decide, the code calls the Count method of both the HeaderParts and FooterParts properties of the document part, and if either returns a value greater than 0, the code continues. Be aware that the HeaderParts and FooterParts properties each return an IEnumerable of HeaderPart or FooterPart objects, respectively.
Given a collection of references to header and footer parts, you could write code to delete each one individually, but that is not necessary because of the Open XML SDK 2.5. Instead, you can call the DeleteParts<T> method, passing in the collection of parts to be deleted─this simple method provides a shortcut for deleting a collection of parts. Therefore, the following few lines of code take the place of the loop that you would otherwise have to write yourself.
At this point, the code has deleted the header and footer parts, but the document still contains orphaned references to those parts. Before the orphaned references can be removed, the code must retrieve a reference to the content of the document (that is, to the XML content contained within the main document part). Later, after the changes are made, the code must ensure that they persist by explicitly saving them. Between these two operations, the code must delete the orphaned references, as shown in the section that follows the following code example.
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. Note that the code converts the IEnumerable returned by the Descendants method into a List so that it can delete items from the list, and that the HeaderReference type that is provided by the Open XML SDK 2.5 makes it easy to refer to elements of type HeaderReference in the XML content. (Without that additional help, you would have to work with the details of the XML content directly.) Once it has removed all the headers, the code repeats the operation with the footer elements.
' Remove all references to the headers and footers. ' First, create a list of all descendants of type ' HeaderReference. Then, navigate the list and call ' Remove on each item to delete the reference. Dim headers = _ document.Descendants(Of HeaderReference).ToList() For Each header In headers header.Remove() Next ' First, create a list of all descendants of type ' FooterReference. Then, navigate the list and call ' Remove on each item to delete the reference. Dim footers = _ document.Descendants(Of FooterReference).ToList() For Each footer In footers footer.Remove() Next
The following is the complete RemoveHeadersAndFooters code sample in C# and Visual Basic.
' To remove all of the headers and footers in a document. Public Sub RemoveHeadersAndFooters(ByVal filename As String) ' Given a document name, remove all of the headers and footers ' from the document. Using doc = WordprocessingDocument.Open(filename, True) ' Get a reference to the main document part. Dim docPart = doc.MainDocumentPart ' Count the header and footer parts and continue if there ' are any. If (docPart.HeaderParts.Count > 0) Or (docPart.FooterParts.Count > 0) Then ' Remove the header and footer parts. docPart.DeleteParts(docPart.HeaderParts) docPart.DeleteParts(docPart.FooterParts) ' Get a reference to the root element of the main ' document part. Dim document As Document = docPart.Document ' Remove all references to the headers and footers. ' First, create a list of all descendants of type ' HeaderReference. Then, navigate the list and call ' Remove on each item to delete the reference. Dim headers = _ document.Descendants(Of HeaderReference).ToList() For Each header In headers header.Remove() Next ' First, create a list of all descendants of type ' FooterReference. Then, navigate the list and call ' Remove on each item to delete the reference. Dim footers = _ document.Descendants(Of FooterReference).ToList() For Each footer In footers footer.Remove() Next ' Save the changes. document.Save() End If End Using End Sub
Contribute to this article
Want to edit or suggest changes to this content? You can edit and submit changes to this article using GitHub.