HeaderFooterCollection

HeaderFooterCollection class

Provides typed access to HeaderFooter nodes of a Section.

public class HeaderFooterCollection : NodeCollection

Properties

Name Description
Count { get; } Gets the number of nodes in the collection.
Item { get; } Retrieves a HeaderFooter at the given index. (3 indexers)

Methods

Name Description
Add(Node) Adds a node to the end of the collection.
Clear() Removes all nodes from this collection and from the document.
Contains(Node) Determines whether a node is in the collection.
GetEnumerator() Provides a simple “foreach” style iteration over the collection of nodes.
IndexOf(Node) Returns the zero-based index of the specified node.
Insert(int, Node) Inserts a node into the collection at the specified index.
LinkToPrevious(bool) Links or unlinks all headers and footers to the corresponding headers and footers in the previous section.
LinkToPrevious(HeaderFooterType, bool) Links or unlinks the specified header or footer to the corresponding header or footer in the previous section.
Remove(Node) Removes the node from the collection and from the document.
RemoveAt(int) Removes the node at the specified index from the collection and from the document.
ToArray() Copies all HeaderFoorters from the collection to a new array of HeaderFoorters. (2 methods)

Remarks

There can be maximum of one HeaderFooter

of each HeaderFooterType per Section.

HeaderFooter objects can occur in any order in the collection.

Examples

Shows how to delete all footers from a document.

Document doc = new Document(MyDir + "Header and footer types.docx");

// Iterate through each section and remove footers of every kind.
foreach (Section section in doc.OfType<Section>())
{
    // There are three kinds of footer and header types.
    // 1 -  The "First" header/footer, which only appears on the first page of a section.
    HeaderFooter footer = section.HeadersFooters[HeaderFooterType.FooterFirst];
    footer?.Remove();

    // 2 -  The "Primary" header/footer, which appears on odd pages.
    footer = section.HeadersFooters[HeaderFooterType.FooterPrimary];
    footer?.Remove();

    // 3 -  The "Even" header/footer, which appears on even pages. 
    footer = section.HeadersFooters[HeaderFooterType.FooterEven];
    footer?.Remove();

    Assert.AreEqual(0, section.HeadersFooters.Count(hf => !((HeaderFooter)hf).IsHeader));
}

doc.Save(ArtifactsDir + "HeaderFooter.RemoveFooters.docx");

Shows how to create a header and a footer.

Document doc = new Document();

// Create a header and append a paragraph to it. The text in that paragraph
// will appear at the top of every page of this section, above the main body text.
HeaderFooter header = new HeaderFooter(doc, HeaderFooterType.HeaderPrimary);
doc.FirstSection.HeadersFooters.Add(header);

Paragraph para = header.AppendParagraph("My header.");

Assert.True(header.IsHeader);
Assert.True(para.IsEndOfHeaderFooter);

// Create a footer and append a paragraph to it. The text in that paragraph
// will appear at the bottom of every page of this section, below the main body text.
HeaderFooter footer = new HeaderFooter(doc, HeaderFooterType.FooterPrimary);
doc.FirstSection.HeadersFooters.Add(footer);

para = footer.AppendParagraph("My footer.");

Assert.False(footer.IsHeader);
Assert.True(para.IsEndOfHeaderFooter);

Assert.AreEqual(footer, para.ParentStory);
Assert.AreEqual(footer.ParentSection, para.ParentSection);
Assert.AreEqual(footer.ParentSection, header.ParentSection);

doc.Save(ArtifactsDir + "HeaderFooter.Create.docx");

See Also