Export (0) Print
Expand All
Expand Minimize
0 out of 11 rated this helpful - Rate this topic

Deleting Headers and Footers from Word 2010 Documents by Using the Open XML SDK 2.0

Office 2010

Office Visual How To

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

Overview

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.

Code It

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:

  • WindowsBase─This reference may be set for you, depending on the kind of project that you create.

  • DocumentFormat.OpenXml─Installed by the Open XML SDK 2.0.

Also, include the following using/Imports statements at the top of the code file.

using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

Examining the Procedure

The WDDeleteHeadersAndFooters procedure accepts a single parameter (a string that indicates the path to the file you want to modify).

public static void WDRemoveHeadersFooters(string docName)

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 docPart.

using (WordprocessingDocument 
  wdDoc = WordprocessingDocument.Open(docName, true))
{
  var docPart = wdDoc.MainDocumentPart;
  // Code removed here…
}

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 || docPart.FooterParts.Count() > 0)
{
  // Code removed here…
}

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.

docPart.DeleteParts(docPart.HeaderParts);
docPart.DeleteParts(docPart.FooterParts);

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.

Document doc = docPart.Document;
// Code removed here…
doc.Save();

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.

var headers =
  doc.Descendants<HeaderReference>().ToList();
foreach (var header in headers)
{
  header.Remove();
}

var footers =
  doc.Descendants<FooterReference>().ToList();
foreach (var footer in footers)
{
  footer.Remove();
}

Sample Procedure

The following code example contains the complete sample procedure.

public static void WDRemoveHeadersFooters(string docName)
{
  // Given a document name, remove all headers and footers.
  using (WordprocessingDocument wdDoc = 
    WordprocessingDocument.Open(docName, true))
  {
    var docPart = wdDoc.MainDocumentPart;
    if (docPart.HeaderParts.Count() > 0 || 
        docPart.FooterParts.Count() > 0)
    {
      // Remove header and footer parts.
      docPart.DeleteParts(docPart.HeaderParts);
      docPart.DeleteParts(docPart.FooterParts);

      Document doc = docPart.Document;

      // Remove references to the headers and footers.
      // This requires digging into the XML content
      // of the document:
      var headers =
        doc.Descendants<HeaderReference>().ToList();
      foreach (var header in headers)
      {
        header.Remove();
      }

      var footers =
        doc.Descendants<FooterReference>().ToList();
      foreach (var footer in footers)
      {
        footer.Remove();
      }
      doc.Save();
    }
  }
}
Read It

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

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

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.

See It

Watch the video

Watch video

Length: 00:06:55

Click to grab code

Grab the Code

Explore It

About the Author
Ken Getz is a senior consultant with MCW Technologies. He is coauthor of ASP.NET Developers Jumpstart (Addison-Wesley, 2002), Access Developer's Handbook (Sybex, 2001), and VBA Developer's Handbook, 2nd Edition (Sybex, 2001).

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.