HeaderFooter

HeaderFooter class

表示某个部分的页眉或页脚文本的容器。

要了解更多信息,请访问使用页眉和页脚文档文章。

public class HeaderFooter : Story

构造函数

姓名描述
HeaderFooter(DocumentBaseHeaderFooterType)创建指定类型的新页眉或页脚。

特性

姓名描述
Count { get; }获取此节点的直属子节点的数量。
CustomNodeId { get; set; }指定自定义节点标识符。
virtual Document { get; }获取此节点所属的文档。
FirstChild { get; }获取节点的第一个子节点。
FirstParagraph { get; }获取故事的第一段。
HasChildNodes { get; }返回真的如果此节点有任何子节点。
HeaderFooterType { get; }获取此页眉/页脚的类型。
override IsComposite { get; }返回真的因为这个节点可以有子节点。
IsHeader { get; }如果HeaderFooter对象是一个标题。
IsLinkedToPrevious { get; set; }如果此页眉或页脚链接到上一节中相应的页眉或页脚,则为真。
LastChild { get; }获取节点的最后一个子节点。
LastParagraph { get; }获取故事的最后一段。
NextSibling { get; }获取紧随此节点之后的节点。
override NodeType { get; }返回HeaderFooter.
Paragraphs { get; }获取故事的直接子段落集合。
ParentNode { get; }获取此节点的直接父节点。
ParentSection { get; }获取此故事的父部分。
PreviousSibling { get; }获取此节点前一个节点。
Range { get; }返回Range表示此节点中包含的文档部分的对象。
StoryType { get; }获取该故事的类型。
Tables { get; }获取故事的直接子表集合。

方法

姓名描述
override Accept(DocumentVisitor)接受访客。
override AcceptEnd(DocumentVisitor)接受访问者访问标题的末尾。
override AcceptStart(DocumentVisitor)接受访问者访问标题的开头。
AppendChild<T>(T)将指定节点添加到此节点的子节点列表的末尾。
AppendParagraph(string)创建Paragraph带有可选文本的对象并将其附加到此对象的末尾。
Clone(bool)创建节点的副本。
CreateNavigator()创建可用于遍历和读取节点的导航器。
DeleteShapes()从该故事的文本中删除所有形状。
GetAncestor(NodeType)获取指定的第一个祖先NodeType.
GetAncestor(Type)获取指定对象类型的第一个祖先。
GetChild(NodeType, int, bool)返回与指定类型匹配的第 N 个子节点。
GetChildNodes(NodeType, bool)返回与指定类型匹配的子节点的实时集合。
GetEnumerator()为该节点的子节点提供对每个样式迭代的支持。
override GetText()获取此节点及其所有子节点的文本。
IndexOf(Node)返回子节点数组中指定子节点的索引。
InsertAfter<T>(T, Node)在指定的参考节点后立即插入指定的节点。
InsertBefore<T>(T, Node)在指定的参考节点之前立即插入指定的节点。
NextPreOrder(Node)根据前序树遍历算法获取下一个节点。
PrependChild<T>(T)将指定节点添加到此节点的子节点列表的开头。
PreviousPreOrder(Node)根据前序树遍历算法获取前一个节点。
Remove()将自身从父级中移除。
RemoveAllChildren()删除当前节点的所有子节点。
RemoveChild<T>(T)删除指定的子节点。
RemoveSmartTags()删除所有SmartTag当前节点的后代节点。
SelectNodes(string)选择与 XPath 表达式匹配的节点列表。
SelectSingleNode(string)选择第一个Node与 XPath 表达式匹配。
ToString(SaveFormat)将节点的内容导出为指定格式的字符串。
ToString(SaveOptions)使用指定的保存选项将节点内容导出为字符串。

评论

HeaderFooter可以包含Paragraph桌子子节点。

HeaderFooter是节级节点,并且只能是Section. 只能有一个HeaderFooter每个HeaderFooterType在一个Section

如果Section没有HeaderFooter特定类型或 HeaderFooter没有子节点,则此页眉/页脚被视为链接到 Microsoft Word 中上一节相同类型的页眉/页脚。

什么时候HeaderFooter至少包含一个Paragraph,它不再被视为与 Microsoft Word 中的上一个相链接。

例子

展示如何替换文档页脚中的文本。

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");

展示如何从文档中删除所有页脚。

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

// 遍历每个部分并删除每种页脚。
foreach (Section section in doc.OfType<Section>())
{
    // 页脚和页眉类型有三种。
    // 1 - “第一”页眉/页脚,仅出现在部分的第一页上。
    HeaderFooter footer = section.HeadersFooters[HeaderFooterType.FooterFirst];
    footer?.Remove();

    // 2 – “主要”页眉/页脚,出现在奇数页上。
    footer = section.HeadersFooters[HeaderFooterType.FooterPrimary];
    footer?.Remove();

     // 3 - “偶数”页眉/页脚,出现在偶数页上。
    footer = section.HeadersFooters[HeaderFooterType.FooterEven];
    footer?.Remove();

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

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

展示如何创建页眉和页脚。

Document doc = new Document();

// 创建标题并在其后附加一个段落。该段落中的文本
// 将出现在本节每一页的顶部,正文上方。
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);

// 创建页脚并在其后附加一个段落。该段落中的文本
// 将出现在本节每一页的底部,正文下方。
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");

也可以看看