HeaderFooterCollection

HeaderFooterCollection class

提供对HeaderFooter的节点Section.

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

public class HeaderFooterCollection : NodeCollection

特性

姓名描述
Count { get; }获取集合中的节点数。
Item { get; }检索HeaderFooter在给定的索引处。 (3 indexers)

方法

姓名描述
Add(Node)在集合末尾添加一个节点。
Clear()从此集合和文档中删除所有节点。
Contains(Node)确定节点是否在集合中。
GetEnumerator()提供对节点集合的简单“foreach”样式迭代。
IndexOf(Node)返回指定节点的从零开始的索引。
Insert(int, Node)将节点插入到指定索引的集合中。
LinkToPrevious(bool)将所有页眉和页脚链接或取消链接到上一节中相应的 页眉和页脚。
LinkToPrevious(HeaderFooterType, bool)将指定的页眉或页脚链接或取消链接到上一节中相应的 页眉或页脚。
Remove(Node)从集合和文档中删除节点。
RemoveAt(int)从集合和文档中删除指定索引处的节点。
ToArray()复制全部页眉页脚从集合中取出一个新数组页眉页脚s. (2 methods)

评论

最多可以有一个HeaderFooter

每个HeaderFooterTypeper Section.

HeaderFooter对象可以在集合中以任何顺序出现。

例子

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

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

也可以看看