前言

本文是对Word页眉页脚的操作方法的进一步的阐述。在“C# 添加Word页眉页脚、页码”一文中,介绍了添加简单页眉页脚的方法,该文中的方法可满足于大多数的页眉页脚添加要求,但是对于比较复杂一点的文档,对页眉页脚的添加要求比较严格的,如需要设置奇、偶页的页眉页脚不同、首页页眉页脚不同、设置页码时需要对不同章节的内容设置不同页码、对包含重要信息的页眉页脚需要设置编辑权限、相同性质的文档需要复制指定页眉页脚等等操作,则可以参考本文中的方法。鉴于此,本文就以上操作要求分以下几个示例要点来进行:

  • 设置Word奇偶页页眉页脚不同
  • 设置Word首页页眉页脚不同
  • 不连续设置页码(即对不同章节的内容设置不同页码)
  • 复制页眉页脚
  • 锁定页眉页脚
  • 删除页眉页脚

使用工具:Free Spire.Doc for .NET(社区版)

:编程时注意在相应程序中添加引用Spire.Doc.dll,dll文件可在安装路径下的Bin文件夹中获取。


C#代码示例(供参考)

【示例1】设置Word奇偶页页眉页脚不同

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;namespace HeadersFootersForOddAndEvenPages
{class Program{static void Main(string[] args){//创建Document类,并加载测试文档Document document = new Document();document.LoadFromFile("test.docx");//获取指定节,并设置页眉页脚奇偶页不同的属性为trueSection section = document.Sections[0];section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = true;//设置奇偶数页的页脚Paragraph P1 = section.HeadersFooters.EvenFooter.AddParagraph();TextRange EF = P1.AppendText("偶数页页脚");EF.CharacterFormat.FontName = "Calibri";EF.CharacterFormat.FontSize = 12;EF.CharacterFormat.TextColor = Color.Green;EF.CharacterFormat.Bold = true;P1.Format.HorizontalAlignment = HorizontalAlignment.Right;Paragraph P2 = section.HeadersFooters.OddFooter.AddParagraph();TextRange OF = P2.AppendText("奇数页页脚");P2.Format.HorizontalAlignment = HorizontalAlignment.Left ;OF.CharacterFormat.FontName = "Calibri";OF.CharacterFormat.FontSize = 12;OF.CharacterFormat.Bold = true;OF.CharacterFormat.TextColor = Color.Blue;//设置奇偶数页的页眉Paragraph P3 = section.HeadersFooters.OddHeader.AddParagraph();TextRange OH = P3.AppendText("奇数页页眉");P3.Format.HorizontalAlignment = HorizontalAlignment.Left;OH.CharacterFormat.FontName = "Calibri";OH.CharacterFormat.FontSize = 12;OH.CharacterFormat.Bold = true;OH.CharacterFormat.TextColor = Color.Blue;Paragraph P4 = section.HeadersFooters.EvenHeader.AddParagraph();TextRange EH = P4.AppendText("偶数页页眉");P4.Format.HorizontalAlignment = HorizontalAlignment.Right;EH.CharacterFormat.FontName = "Calibri";EH.CharacterFormat.FontSize = 12;EH.CharacterFormat.Bold = true;EH.CharacterFormat.TextColor = Color.Green;//保存文档document.SaveToFile("result.docx", FileFormat.Docx2010);System.Diagnostics.Process.Start("result.docx");}}
}

奇偶页页眉页脚不同设置效果:

【示例2】设置Word首页页眉页脚不同

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;namespace HeadersFootersForOddAndEvenPages
{class Program{static void Main(string[] args){//创建Document类的对象,并加载测试文档Document document = new Document();document.LoadFromFile("test.docx");//获取指定节,并设置页眉页脚首页不同属性为trueSection section = document.Sections[0];section.PageSetup.DifferentFirstPageHeaderFooter = true;//加载图片添加到首页页眉Paragraph paragraph1 = section.HeadersFooters.FirstPageHeader.AddParagraph();paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Left;DocPicture headerimage = paragraph1.AppendPicture(Image.FromFile("2.png"));//添加文字到首页页脚Paragraph paragraph2 = section.HeadersFooters.FirstPageFooter.AddParagraph();paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center;TextRange FF = paragraph2.AppendText("首页页脚");FF.CharacterFormat.FontSize = 12;//添加页眉页脚到其他页面Paragraph paragraph3 = section.HeadersFooters.Header.AddParagraph();paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Center;TextRange NH = paragraph3.AppendText("非首页页眉");NH.CharacterFormat.FontSize = 12;Paragraph paragraph4 = section.HeadersFooters.Footer.AddParagraph();paragraph4.Format.HorizontalAlignment = HorizontalAlignment.Center;TextRange NF = paragraph4.AppendText("非首页页脚");NF.CharacterFormat.FontSize = 12;//保存文档document.SaveToFile("result.docx", FileFormat.Docx2010);System.Diagnostics.Process.Start("result.docx");}}
}

首页页眉页脚不同设置效果:

【示例3】不连续设置页码

using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;namespace DifferentPageNumber_Doc
{class Program{static void Main(string[] args){//创建Document对象,并加载测试文档Document doc = new Document();doc.LoadFromFile("test.docx");//实例化HeaderFooter对象(指定页码添加位置:页眉或页脚)HeaderFooter footer = doc.Sections[0].HeadersFooters.Footer;//添加段落到页脚Paragraph footerParagraph = footer.AddParagraph();//添加页码域到页脚footerParagraph.AppendField("page number", FieldType.FieldPage);//设置页码右对齐footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;//创建段落样式,包括字体名称、大小、颜色ParagraphStyle style = new ParagraphStyle(doc);style.CharacterFormat.Font = new Font("黑体", 10, FontStyle.Bold);style.CharacterFormat.TextColor = Color.Black;doc.Styles.Add(style);//应用段落样式到页脚
            footerParagraph.ApplyStyle(style.Name);//将第一节的页码样式设置为罗马数字doc.Sections[0].PageSetup.PageNumberStyle = PageNumberStyle.RomanLower;//将第二节的页码样式设置为阿拉伯数字,并重新开始编码doc.Sections[1].PageSetup.PageNumberStyle = PageNumberStyle.Arabic;doc.Sections[1].PageSetup.RestartPageNumbering = true;doc.Sections[1].PageSetup.PageStartingNumber = 1;//此处可任意指定起始页码数//保存文档doc.SaveToFile("output.docx", FileFormat.Docx2010);System.Diagnostics.Process.Start("output.docx");}}
}

页码添加效果:

【示例4】复制页眉页脚

using Spire.Doc;namespace CopyHeaderAndFooter_Doc
{class Program{static void Main(string[] args){//新建Word文档1,并加载带页眉的源文档Document doc1 = new Document();doc1.LoadFromFile("test1.docx");//获取文档1的页眉HeaderFooter Header = doc1.Sections[0].HeadersFooters.Header;//新建文档2,并加载目标文档Document doc2 = new Document("test2.docx");//遍历文档2中的所有Sectionforeach (Section section in doc2.Sections){foreach (DocumentObject obj in Header.ChildObjects){//将复制的页眉对象添加到section
                    section.HeadersFooters.Header.ChildObjects.Add(obj.Clone());}}//保存并打开文档doc2.SaveToFile("copyHeader.docx", FileFormat.Docx2013);System.Diagnostics.Process.Start("copyHeader.docx");}}
}

测试文档:

测试结果:

【示例5】锁定页眉页脚

using Spire.Doc;namespace ProtectHeaderFooter_Doc
{class Program{static void Main(string[] args){//加载测试文档Document doc = new Document();doc.LoadFromFile("sample.docx");//获取第一个sectionSection section = doc.Sections[0];//保护文档并设置 ProtectionType 为 AllowOnlyFormFields,并设置启用编辑的密码doc.Protect(ProtectionType.AllowOnlyFormFields, "123");//设置ProtectForm 为false 允许编辑其他区域section.ProtectForm = false;//保存文档doc.SaveToFile("result.docx", FileFormat.Docx2013);System.Diagnostics.Process.Start("result.docx");}}
}

运行程序生成的文档中,页眉将不允许被编辑,正确输入密码后,方可编辑页眉。

【示例6】删除页眉页脚

1.删除所有页面的页眉页脚

using Spire.Doc;namespace RemoveHeaderFooter_Doc
{class Program{static void Main(string[] args){//创建一个Document实例并加载示例文档Document doc = new Document();doc.LoadFromFile("sample.docx");//获取第一个sectionSection section = doc.Sections[0];//删除页眉
            section.HeadersFooters.Header.ChildObjects.Clear();//删除页脚
            section.HeadersFooters.Footer.ChildObjects.Clear();//保存文档 doc.SaveToFile("result.docx", FileFormat.Docx);System.Diagnostics.Process.Start("result.docx");}}
}

删除效果:

2.删除首页的页眉页脚(适用于文档封面,不需要页眉页脚的情况,或者其他情形)

using Spire.Doc;namespace RemoveHeaderFooter2_Doc
{class Program{static void Main(string[] args){//创建一个Document实例并加载示例文档Document doc = new Document();doc.LoadFromFile("sample.docx");//获取第一个sectionSection section = doc.Sections[0];//设置页眉页脚首页不同section.PageSetup.DifferentFirstPageHeaderFooter = true;//删除首页页眉页脚
            section.HeadersFooters.FirstPageHeader.ChildObjects.Clear();//保存文档 doc.SaveToFile("output.docx", FileFormat.Docx);System.Diagnostics.Process.Start("output.docx");}}
}

删除效果:

(本文完)

如需转载,请注明出处!

转载于:https://www.cnblogs.com/Yesi/p/10442140.html

C# 操作Word页眉页脚——奇偶页/首页不同、不连续设置页码、复制页眉页脚、锁定页眉页脚、删除页眉页脚...相关推荐

  1. C# 操作Word页眉页脚——奇偶页/首页不同、不连续设置页码、复制页眉页脚、锁定页眉页脚、删除页眉...

    序  本文是对Word页眉页脚的操作方法的进一步的阐述.在"C# 添加Word页眉页脚.页码"一文中,介绍了添加简单页眉页脚的方法,该文中的方法可满足于大多数的页眉页脚添加要求,但 ...

  2. docx4j生成Word添加页眉页脚水印 页眉页脚奇偶显示首页显示

    docx4j 单独添加页眉页脚.水印的代码一搜一大堆,但很少有人把页眉+水印.奇偶页眉页脚.首页页眉页脚全面实现,最近在做相关的内容,经过大量的查阅资料,总算完成了,分享记录一下. 先看效果: 水印在 ...

  3. wps排版-一级标题设置段前段后两行,段前不显示格式和怎么删除下一页分节符,不改变格式

    1. 问题 正常显示 2. 解决方法 (1)先清除"第1章 绪论"的段落布局,鼠标放在在"第1章 绪论"开头,点击---插入---分页----下一页分节符--- ...

  4. Android 9.0 recovery 菜单页跳过弹窗自动 WIPE_DATA(恢复出厂设置)

    1.概述 在9.0的系统产品开发中,对系统原生的recovery功能也是系统中比较重要的一个部分,所以对于恢复出厂设置在开发中也是常有的功能 而在一款产品的开发过程中,由于在recovery界面需要手 ...

  5. 论文页眉奇偶页不同怎么设置_怎样设置Word页眉页脚奇偶页不同?

    来自Excel之家,侵联系删 有伙伴问,怎么设置满足下列要求的页眉页脚: 页眉要求正文部分偶数页居中对齐为"XXXX毕业设计(论文)",奇数页居中对齐是各章章名:字体采用宋体5号. ...

  6. itext操作word,设置页眉页脚,html转word

    这两天学习了itext操作word生成可用的docx文档,以下是我翻阅网上资料最后的结果及相关记录,如下: 我的maven项目导入必要的itext依赖 <dependency><gr ...

  7. input设置点击无边框_设置Word页眉页脚奇偶页不同

    有个小伙伴求助,问如何设置满足下列要求的页眉页脚: 页眉要求正文部分偶数页居中对齐为"XXXX毕业设计(论文)",奇数页居中对齐是各章章名:字体采用宋体5号.页眉之下有一条下划线. ...

  8. python 操作word 修改页眉与页脚

    # -*- coding: utf-8 -*- # @Time : 2022年05月01日 19时59分 # @File : doctemp.py # @notice :from docxtpl im ...

  9. 毕业论文的页眉页脚?奇偶页不同?前言作为第一页?……?这样弄!

    下面的要求作为一个示例,如果有类似的要求,相信理解了其中的概念,你也能顺利解决你的论文要求了. 示例要求 总结一下重点要求: 正式页码从前言开始作为第一页(前言并不在论文的首页,前言前面有目录,摘要和 ...

  10. java操作word,添加页眉,页眉图片,替换书签,添加水印(全)

    java操作word文档,添加页眉文本,页眉图片,替换书签,水印 原模板截图: 生成后的文档效果截图: 第一步:引入maven <dependency><groupId>spi ...

最新文章

  1. js中Array数组中的常用方法汇总
  2. 买了《精通spring 2.0》
  3. Android 数据加密算法 Des,Base64详解
  4. pov-inc_yourself劳自己-懒惰的设计师的POV和一些Figma
  5. 栈大小和内存分部问题
  6. C++ 多态在异常中的使用
  7. neditor 自定义工具栏配置
  8. lucene学习之helloworld(简单实例)
  9. 参加集成电路EDA设计精英挑战赛的体会
  10. core部署iis的 调试net_ASP.NET Core环境变量和启动设置的配置教程
  11. 怎么使用计算机扩展,如何导入扩展到慧编程PC并进行使用
  12. 阿里云平台购买域名 备案步骤
  13. 阿里巴巴-菜鸟网络和申通快递面试
  14. 微服务架构 | 怎样解决超大附件分片上传?
  15. php 与shell有什么关系,shell是什么意思
  16. 企业寄件自定义短信通知教程
  17. ASSIST-GAI全色域指数计算工具
  18. (接上篇)浮动静态路由主备路径故障倒换实验NQA技术
  19. vSphere ESXI 7.0镜像 Rufus U盘安装盘制作(Windows)
  20. c语言考试系统题库判断和选择,C考试系统题库判断和选择.doc

热门文章

  1. 1156:求π的值(C C++)
  2. 深度学习在图像识别上的应用
  3. 欢聚时代java面试面经_面试经历—广州YY(欢聚时代) | 学步园
  4. ai旋转扭曲_AI变换及旋转图形工具详解
  5. 旭辉完成2020年销售目标:还要加码广西,却在北京违规被通报
  6. The JAVA_HOME environment variable is not defined correctly 解决方法
  7. console接口配置登录密码
  8. 二十三、小程序中的三级下拉框(下拉菜单)
  9. 致凯利定理(Cayley公式)
  10. spring事务传播机制之《REQUIRED》