Deleting Headers and Footers from Word 2010 Documents by Using the Open XML SDK 2.0
Summary: Use the strongly typed classes in the Open XML SDK 2.0 to delete all headers and footers in a Microsoft Word document, without loading the document into Word.
Applies to: Microsoft Office 2010 | Visual Studio 2010 | 2007 Microsoft Office system
Published: February 2011
Provided by: Ken Getz, MCW Technologies, LLC
The Open XML file formats make it possible to retrieve and modify blocks of content from Word documents, but doing so requires some effort. The Open XML SDK 2.0 adds strongly typed classes that simplify access to the Open XML file formats: The SDK simplifies the tasks of working with, for example, the header and footer parts and references to those parts in the document. The code sample included with this Visual How To shows how to the use the SDK to delete all the header and footer parts, along with references to those parts in the main document part, all without requiring you to open the document in Microsoft Word.
The sample provided with this Visual How To includes the code necessary to delete all of the header and footer information in a Word 2007 or Word 2010 document. The following sections describe the code in detail. You should note that it's not enough to simply delete the header and footer parts from the document storage─you must also delete references to those parts from within the document itself. The sample code demonstrates both steps in the operation.
Setting Up References
To use the code from the Open XML SDK 2.0, you must add a few references to your project. The sample project already includes these references, but in your code, you must explicitly reference the following assemblies:
Also, include the following using/Imports statements at the top of the code file.
Imports System.Linq Imports DocumentFormat.OpenXml.Wordprocessing Imports DocumentFormat.OpenXml.Packaging
Examining the Procedure
The procedure works with the document you specify, deleting all header and footer parts and references to those parts. To call the procedure, pass the required parameter value, as shown in the example code. Verify that you provide a document named C:\temp\Headers.docx, which contains some specific headers and footers.
Accessing the Document
The code starts by opening the document, using the WordprocessingDocument.Open method and indicating that the document should be open 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
Using wdDoc = WordprocessingDocument.Open(docName, True) Dim docPart = wdDoc.MainDocumentPart ' Code removed here… End Using
Confirming Header/Footer Existence
Given a reference to the document part, the code next determines if it has work to do─that is, if the document contains any headers or footers. To make that decision, 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.
If (docPart.HeaderParts.Count > 0) Or (docPart.FooterParts.Count > 0) Then ' Code removed here… End If
Deleting the Header and Footer Parts
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.0. Instead, you can call the MainDocumentPart.DeleteParts method, passing in the collection of parts to be deleted. This method provides a shortcut for deleting a collection of parts. Therefore, the following two lines of code take the place of the loop that you would otherwise have to write yourself.
Working with the Document Content
Now, the code has deleted the header and footer parts, but it has left orphaned references to those parts in the document content itself. Before it makes the changes, the code retrieves a reference to the content of the document (that is, the XML content contained within the document part), and once it is done, saves the content. Between these two lines, the code will delete the necessary references.
Deleting the Header and Footer References
As its final step, the code first retrieves a collection of HeaderReference elements, converts the collection to a List, then loops through the collection calling the Remove method for each element that it found. Be aware that the code converts the IEnumerable returned by the Descendants property into a List so that it can delete items from the list, and that the HeaderReference type provided by the Open XML SDK 2.0 makes it easy to refer to elements of type HeaderReference in the XML content. Without that extra help, you would have to work with the details of the XML content directly. When it has removed all the headers, the code repeats the operation with the footer elements.
Dim headers = _ doc.Descendants(Of HeaderReference).ToList() For Each header In headers header.Remove() Next Dim footers = _ doc.Descendants(Of FooterReference).ToList() For Each footer In footers footer.Remove() Next
The following code example contains the complete sample procedure.
Public Sub WDDeleteHeadersAndFooters(ByVal docName As String) ' Given a document name, remove all headers and footers. Using wdDoc = WordprocessingDocument.Open(docName, True) Dim docPart = wdDoc.MainDocumentPart If (docPart.HeaderParts.Count > 0) Or (docPart.FooterParts.Count > 0) Then docPart.DeleteParts(docPart.HeaderParts) docPart.DeleteParts(docPart.FooterParts) ' Remove references to the headers and footers. Dim doc As Document = docPart.Document ' Remove references to the headers and footers. ' This requires digging into the XML content ' of the document: Dim headers = _ doc.Descendants(Of HeaderReference).ToList() For Each header In headers header.Remove() Next Dim footers = _ doc.Descendants(Of FooterReference).ToList() For Each footer In footers footer.Remove() Next doc.Save() End If End Using End Sub
The sample included with this Visual How To demonstrates code that deletes all the header and footer information from a Word document. To use the sample, you must install the Open XML SDK 2.0, available from the link listed in the Explore It section. The sample also uses a modified version of code included as part of a set of code snippets for the Open XML SDK 2.0. The Explore It section also includes a link to the full set of snippets, although you can use the sample without downloading and installing the snippets.
If you run the sample code in this Visual How To, starting with a document that contains header and/or footer information, you should find that after running the code, the headers and footers have all been removed from the document. Using this technique allows you to modify the content of the document without needing to open the document in Microsoft Word.
To understand what the sample code is doing, it is useful to examine the contents of the document using the Open XML SDK 2.0 Productivity Tool for Microsoft Office, which is included as part of the Open XML SDK 2.0. Figure 1 shows a sample document that contains several header and footer parts, opened in the tool. The sample code retrieves a reference to the Document part, and given that part, retrieves a collection of all the associated header and footer parts.
Figure 1. Open the sample document and locate the header and footer parts
When it has deleted the header and footer parts, the code finds all the references to those parts and deletes them. To do that, the code must examine the contents of the document part, as shown in Figure 2.
Figure 2. Within the document part, locate the header and footer references
Given a list of XML elements that contain references to the header and footer parts, the code removes each reference.
The sample application demonstrates only a handful of the available properties and methods provided by the Open XML SDK 2.0 that you can interact with when modifying document structure. For more information, investigate the documentation that comes with the Open XML SDK 2.0 Productivity Tool: Click the Open XML SDK Documentation tab in the lower-left corner of the application window, and search for the class you need to study. Although the documentation does not currently always include code examples, given the sample shown here and the documentation, you should be able to successfully modify the sample application.
Watch the video
About the Author