openxml(二) 添加页眉,页脚

 openxml 中 word 文档的结构是如下图:

 

其中,页眉是 header,属于headerpart 部件,页脚是footer,属于footerpart 部件,图上还有其他的东西,之后会一一介绍。这些部件都属于MainDocumentPart 部件。之前讲过了 一个包(package)就是一个word文件。

<maindoucmentpart>

<headerpart>

<header>..........其他东西.......</header>

</headerpart>

<document>

<body>.........</body>

</document>

<footerpart>

<footer>........</footer>

</foooterpart>

</maindoucmentpart>

大概是这样子的关系。

页眉和页脚的对应的,页眉有三种,1.首页的页面(选择首页不同的话,这个会显示) 2.偶数页页面(选择奇偶页不同的话,这个会显示),3.默认页面(什么都不选,默认显示这个)。页脚也有三种与页面对应。

添加页眉,页脚的代码如下:

1  using (WordprocessingDocument packaging = WordprocessingDocument.Create("test.docx", WordprocessingDocumentType.Document))             {                 MainDocumentPart maindocumentpart = packaging.AddNewPart<MainDocumentPart>("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", "r001");
2 
3                 Document doc = new Document();                 doc.Body = new Body();                 maindocumentpart.Document = doc;                 maindocumentpart.Document.Body.AppendChild<Paragraph>(new Paragraph(new Run(new Text() { Text = "测试" })));
4 
5                 #region 页眉                 Header header1 = new Header(new Paragraph(new Run(new Text() { Text = "header1" })));                 Header header2 = new Header(new Paragraph(new Run(new Text() { Text = "header2" })));                 Header header3 = new Header(new Paragraph(new Run(new Text() { Text = "header3" })));                 maindocumentpart.AddNewPart<HeaderPart>("application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml", "r002").Header = header1;                 maindocumentpart.AddNewPart<HeaderPart>("application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml", "r003").Header = header2;                 maindocumentpart.AddNewPart<HeaderPart>("application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml", "r004").Header = header3;                 #endregion
6 
7                 #region 页脚                 Footer footer1 = new Footer(new Paragraph(new Run(new Text() { Text = "footer1" })));                 Footer footer2 = new Footer(new Paragraph(new Run(new Text() { Text = "footer2" })));                 Footer footer3 = new Footer(new Paragraph(new Run(new Text() { Text = "footer3" })));                 maindocumentpart.AddNewPart<FooterPart>("application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml", "r005").Footer = footer1;                 maindocumentpart.AddNewPart<FooterPart>("application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml", "r006").Footer = footer2;                 maindocumentpart.AddNewPart<FooterPart>("application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml", "r007").Footer = footer3;                 #endregion                 /*设置 页眉,页脚和页面关系*/                 SectionProperties sp = new SectionProperties(                  new HeaderReference() { Id = "r002", Type = HeaderFooterValues.First },                  new HeaderReference() { Id = "r003", Type = HeaderFooterValues.Even },                  new HeaderReference() { Id = "r004", Type = HeaderFooterValues.Default },                  new FooterReference() { Id = "r005", Type = HeaderFooterValues.First },                  new FooterReference() { Id = "r006", Type = HeaderFooterValues.Even },                  new FooterReference() { Id = "r007", Type = HeaderFooterValues.Default });                 maindocumentpart.Document.Body.AppendChild<SectionProperties>(sp);             }
View Code

 

posted @ 2015-09-24 22:47  IT修补匠  阅读(1993)  评论(1编辑  收藏  举报