Partager via


How to: Delete the Headers and Footers in a Word 2007 Document by Using the Open XML API

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The Office Open XML Package specification defines a set of XML files that contain the content and define the relationships for all of the parts stored in a single package. These packages combine the parts that make up the document files for Microsoft® Office Excel® 2007, Microsoft Office PowerPoint® 2007, and Microsoft Office Word 2007. The Open XML object model allows you to create packages and manipulate the files that make up the packages. This topic walks through the code and steps to remove the header and footer parts in an Office Open XML package in Office Word 2007, although the steps are the same for each of the three 2007 Microsoft Office system programs that support the Office Open XML Format.

NoteNote

The code samples in this topic are in Microsoft Visual Basic® .NET and Microsoft Visual C#®. You can use them in an add-in created in Microsoft Visual Studio 2008. For more information about how to create an add-in in Visual Studio 2008, see Getting Started with the Open XML Format SDK 1.0.

Deleting the Headers and Footers in a Document

In the following code, you delete the header part and footer part from the Office Open XML package and remove any references to those parts from the main document part:

' How to delete headers and footers in a document.
Public Sub WDRemoveHeadersFooters(ByVal docName As String)
   ' Given a document name, remove all headers and footers.
   Const wordmlNamespace As String = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
   Dim wdDoc As WordprocessingDocument = WordprocessingDocument.Open(docName, true)
   Using (wdDoc)
      If ((wdDoc.MainDocumentPart.GetPartsCountOfType(Of HeaderPart)() > 0) 
      OR (wdDoc.MainDocumentPart.GetPartsCountOfType(Of FooterPart)() > 0))
         wdDoc.MainDocumentPart.DeleteParts(wdDoc.MainDocumentPart.HeaderParts)
         wdDoc.MainDocumentPart.DeleteParts(wdDoc.MainDocumentPart.FooterParts)

         ' Manage namespaces to perform XPath queries.
         Dim nt As NameTable = New NameTable
         Dim nsManager As XmlNamespaceManager = New XmlNamespaceManager(nt)
         nsManager.AddNamespace("w", wordmlNamespace)

         '  Get the document part from the package.
         ' Load the XML in the document part into an XmlDocument instance.
         Dim xdoc As XmlDocument = New XmlDocument(nt)
         xdoc.Load(wdDoc.MainDocumentPart.GetStream)

         ' Find the node containing the document layout.
         Dim layoutNodes As XmlNodeList = xdoc.SelectNodes("//w:sectPr", nsManager)
         For Each layoutNode As System.Xml.XmlNode In layoutNodes
            ' Delete any existing references to headers.
            Dim headerNodes As XmlNodeList = layoutNode.SelectNodes("./w:headerReference", nsManager)
            For Each headerNode As System.Xml.XmlNode In headerNodes
               layoutNode.RemoveChild(headerNode)
            Next

            ' Delete any existing references to footers.
            Dim footerNodes As XmlNodeList = layoutNode.SelectNodes("./w:footerReference", nsManager)
            For Each footerNode As System.Xml.XmlNode In footerNodes
               layoutNode.RemoveChild(footerNode)
            Next
         Next

         ' Save the document XML back to its document part.
         xdoc.Save(wdDoc.MainDocumentPart.GetStream(FileMode.Create))
      End If
   End Using
End Sub
// How to delete headers and footers from a document.
public static void WDRemoveHeadersFooters(string docName)
{
   // Given a document name, remove all headers and footers.

   const string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

   using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(docName, true))
   {
      if (wdDoc.MainDocumentPart.GetPartsCountOfType<HeaderPart>() > 0 ||
        wdDoc.MainDocumentPart.GetPartsCountOfType<FooterPart>() > 0)
      {
         wdDoc.MainDocumentPart.DeleteParts(wdDoc.MainDocumentPart.HeaderParts);
         wdDoc.MainDocumentPart.DeleteParts(wdDoc.MainDocumentPart.FooterParts);

         // Manage namespaces to perform XPath queries.
         NameTable nt = new NameTable();
         XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
         nsManager.AddNamespace("w", wordmlNamespace);

         // Get the document part from the package.
         // Load the XML in the document part into an XmlDocument instance.
         XmlDocument xdoc = new XmlDocument(nt);
         xdoc.Load(wdDoc.MainDocumentPart.GetStream());

         // Find the node containing the document layout.
         XmlNodeList layoutNodes = xdoc.SelectNodes("//w:sectPr", nsManager);
         foreach (System.Xml.XmlNode layoutNode in layoutNodes)
         {
            // Delete any existing references to headers.
            XmlNodeList headerNodes = layoutNode.SelectNodes("./w:headerReference", nsManager);
            foreach (System.Xml.XmlNode headerNode in headerNodes)
            {
               layoutNode.RemoveChild(headerNode);
            }

            // Delete any existing references to footers.
            XmlNodeList footerNodes = layoutNode.SelectNodes("./w:footerReference", nsManager);
            foreach (System.Xml.XmlNode footerNode in footerNodes)
            {
               layoutNode.RemoveChild(footerNode);
            }
         }

         // Save the document XML back to its document part.
         xdoc.Save(wdDoc.MainDocumentPart.GetStream(FileMode.Create));
      }
   }
}

To delete the headers and footers from the Open XML package and remove any references from the main document part

  1. First, pass in a parameter representing the path to and the name of the source Word 2007 document.

  2. Then, open the document as a WordprocessingDocument object.

  3. Next, the header and footer parts are checked to see if either exist and whether one or both are deleted.

    The remainder of the code uses XPath statements to remove the elements in the main document part that refer to the header part and footer part.