HeaderFooter

HeaderFooter class

Represents a container for the header or footer text of a section.

To learn more, visit the Working with Headers and Footers documentation article.

public class HeaderFooter : Story

Constructors

NameDescription
HeaderFooter(DocumentBaseHeaderFooterType)Creates a new header or footer of the specified type.

Properties

NameDescription
Count { get; }Gets the number of immediate children of this node.
CustomNodeId { get; set; }Specifies custom node identifier.
virtual Document { get; }Gets the document to which this node belongs.
FirstChild { get; }Gets the first child of the node.
FirstParagraph { get; }Gets the first paragraph in the story.
HasChildNodes { get; }Returns true if this node has any child nodes.
HeaderFooterType { get; }Gets the type of this header/footer.
override IsComposite { get; }Returns true as this node can have child nodes.
IsHeader { get; }True if this HeaderFooter object is a header.
IsLinkedToPrevious { get; set; }True if this header or footer is linked to the corresponding header or footer in the previous section.
LastChild { get; }Gets the last child of the node.
LastParagraph { get; }Gets the last paragraph in the story.
NextSibling { get; }Gets the node immediately following this node.
override NodeType { get; }Returns HeaderFooter.
Paragraphs { get; }Gets a collection of paragraphs that are immediate children of the story.
ParentNode { get; }Gets the immediate parent of this node.
ParentSection { get; }Gets the parent section of this story.
PreviousSibling { get; }Gets the node immediately preceding this node.
Range { get; }Returns a Range object that represents the portion of a document that is contained in this node.
StoryType { get; }Gets the type of this story.
Tables { get; }Gets a collection of tables that are immediate children of the story.

Methods

NameDescription
override Accept(DocumentVisitor)Accepts a visitor.
override AcceptEnd(DocumentVisitor)Accepts a visitor for visiting the end of the header.
override AcceptStart(DocumentVisitor)Accepts a visitor for visiting the start of the header.
AppendChild<T>(T)Adds the specified node to the end of the list of child nodes for this node.
AppendParagraph(string)A shortcut method that creates a Paragraph object with optional text and appends it to the end of this object.
Clone(bool)Creates a duplicate of the node.
CreateNavigator()Creates navigator which can be used to traverse and read nodes.
DeleteShapes()Deletes all shapes from the text of this story.
GetAncestor(NodeType)Gets the first ancestor of the specified NodeType.
GetAncestor(Type)Gets the first ancestor of the specified object type.
GetChild(NodeType, int, bool)Returns an Nth child node that matches the specified type.
GetChildNodes(NodeType, bool)Returns a live collection of child nodes that match the specified type.
GetEnumerator()Provides support for the for each style iteration over the child nodes of this node.
override GetText()Gets the text of this node and of all its children.
IndexOf(Node)Returns the index of the specified child node in the child node array.
InsertAfter<T>(T, Node)Inserts the specified node immediately after the specified reference node.
InsertBefore<T>(T, Node)Inserts the specified node immediately before the specified reference node.
NextPreOrder(Node)Gets next node according to the pre-order tree traversal algorithm.
PrependChild<T>(T)Adds the specified node to the beginning of the list of child nodes for this node.
PreviousPreOrder(Node)Gets the previous node according to the pre-order tree traversal algorithm.
Remove()Removes itself from the parent.
RemoveAllChildren()Removes all the child nodes of the current node.
RemoveChild<T>(T)Removes the specified child node.
RemoveSmartTags()Removes all SmartTag descendant nodes of the current node.
SelectNodes(string)Selects a list of nodes matching the XPath expression.
SelectSingleNode(string)Selects the first Node that matches the XPath expression.
ToString(SaveFormat)Exports the content of the node into a string in the specified format.
ToString(SaveOptions)Exports the content of the node into a string using the specified save options.

Remarks

HeaderFooter can contain Paragraph and Table child nodes.

HeaderFooter is a section-level node and can only be a child of Section. There can only be one HeaderFooter of each HeaderFooterType in a Section.

If Section does not have a HeaderFooter of a specific type or the HeaderFooter has no child nodes, this header/footer is considered linked to the header/footer of the same type of the previous section in Microsoft Word.

When HeaderFooter contains at least one Paragraph, it is no longer considered linked to previous in Microsoft Word.

Examples

Shows how to replace text in a document’s footer.

Document doc = new Document(MyDir + "Footer.docx");

HeaderFooterCollection headersFooters = doc.FirstSection.HeadersFooters;
HeaderFooter footer = headersFooters[HeaderFooterType.FooterPrimary];

FindReplaceOptions options = new FindReplaceOptions
{
    MatchCase = false,
    FindWholeWordsOnly = false
};

int currentYear = DateTime.Now.Year;
footer.Range.Replace("(C) 2006 Aspose Pty Ltd.", $"Copyright (C) {currentYear} by Aspose Pty Ltd.", options);

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

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