HeaderFooter

HeaderFooter constructor

创建指定类型的新页眉或页脚。

public HeaderFooter(DocumentBase doc, HeaderFooterType headerFooterType)
范围类型描述
docDocumentBase所有者文件。
headerFooterTypeHeaderFooterTypeAHeaderFooterTypevalue 指定页眉或页脚的类型。

评论

什么时候HeaderFooter创建后,它属于指定文档,但还不是 文档的一部分并且ParentNode无效的

追加HeaderFooter到一个Section使用InsertAfter,InsertBefore, 或HeadersFooters属性和方法Add,Insert

例子

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

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

也可以看看