前言

本文是对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"); //获取指定节,并设置页眉页脚奇偶页不同的属性为true
Section section = document.Sections[];
section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = true; //设置奇偶数页的页脚
Paragraph P1 = section.HeadersFooters.EvenFooter.AddParagraph();
TextRange EF = P1.AppendText("偶数页页脚");
EF.CharacterFormat.FontName = "Calibri";
EF.CharacterFormat.FontSize = ;
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 = ;
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 = ;
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 = ;
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"); //获取指定节,并设置页眉页脚首页不同属性为true
Section section = document.Sections[];
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 = ; //添加页眉页脚到其他页面
Paragraph paragraph3 = section.HeadersFooters.Header.AddParagraph();
paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange NH = paragraph3.AppendText("非首页页眉");
NH.CharacterFormat.FontSize = ;
Paragraph paragraph4 = section.HeadersFooters.Footer.AddParagraph();
paragraph4.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange NF = paragraph4.AppendText("非首页页脚");
NF.CharacterFormat.FontSize = ; //保存文档
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[].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("黑体", , FontStyle.Bold);
style.CharacterFormat.TextColor = Color.Black;
doc.Styles.Add(style);
//应用段落样式到页脚
footerParagraph.ApplyStyle(style.Name); //将第一节的页码样式设置为罗马数字
doc.Sections[].PageSetup.PageNumberStyle = PageNumberStyle.RomanLower; //将第二节的页码样式设置为阿拉伯数字,并重新开始编码
doc.Sections[].PageSetup.PageNumberStyle = PageNumberStyle.Arabic;
doc.Sections[].PageSetup.RestartPageNumbering = true;
doc.Sections[].PageSetup.PageStartingNumber = ;//此处可任意指定起始页码数 //保存文档
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[].HeadersFooters.Header; //新建文档2,并加载目标文档
Document doc2 = new Document("test2.docx"); //遍历文档2中的所有Section
foreach (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"); //获取第一个section
Section section = doc.Sections[]; //保护文档并设置 ProtectionType 为 AllowOnlyFormFields,并设置启用编辑的密码
doc.Protect(ProtectionType.AllowOnlyFormFields, ""); //设置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");
//获取第一个section
Section section = doc.Sections[]; //删除页眉
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"); //获取第一个section
Section section = doc.Sections[]; //设置页眉页脚首页不同
section.PageSetup.DifferentFirstPageHeaderFooter = true; //删除首页页眉页脚
section.HeadersFooters.FirstPageHeader.ChildObjects.Clear(); //保存文档
doc.SaveToFile("output.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("output.docx");
}
}
}

删除效果:

(本文完)

如需转载,请注明出处!

C# 操作Word页眉页脚——奇偶页/首页不同、不连续设置页码、复制页眉页脚、锁定页眉页脚、删除页眉页脚的更多相关文章

  1. word文档常用的格式操作(分页、分节、每一节如何设置页码、以及生成目录)

    分页: 分节: 自动生成目录:

  2. Java 操作Word表格——创建嵌套表格、添加/复制表格行或列、设置表格是否禁止跨页断行

    本文将对如何在Java程序中操作Word表格作进一步介绍.操作要点包括 如何在Word中创建嵌套表格. 对已有表格添加行或者列 复制已有表格中的指定行或者列 对跨页的表格可设置是否禁止跨页断行 创建表 ...

  3. word中怎样设置页码包含总页数

    一个同事做毕业论文,论文是Word格式,1-2页是封面和目录,不需要页码,第3-10页是论文内容,需要从第1页开始显示,并显示论文内容的总页数8 页.具体为页脚处显示“第*页共*页”.他让我帮忙设置一 ...

  4. word怎样从第三页开始设置页码

    一般的文件都是有封面,目录.然后才是正文.所以基本上第一页的封面,第二页是目录,第三页才是正文的开始.但是默认的页码会从第一页开始的,封面上海有页码这会很难看,今天和小编一起来看看怎样将页码从第三页开 ...

  5. word从任意页设置页码

    把所有页都设置页码 首先设置分隔符,下一页 在第二节中,找到插入页码,设置起始页码为1即可

  6. [转]C#操作Word的超详细总结

    本文中用C#来操作Word,包括: 创建Word: 插入文字,选择文字,编辑文字的字号.粗细.颜色.下划线等: 设置段落的首行缩进.行距: 设置页面页边距和纸张大小: 设置页眉.页码: 插入图片,设置 ...

  7. C# 操作Word知识汇总

    转自:http://blog.csdn.net/jiutao_tang/article/details/6574740 1. 微软官方实例: 段落.表格.图表 HOW TO:利用 Visual C# ...

  8. C#操作Word的辅助类(word2003) 修改完善版

    转自:http://blog.csdn.net/jiutao_tang/article/details/6567608 该类在他人编写的几个类基础上扩展完善而来,主要功能有: (1)插入文本 (2)插 ...

  9. 比较全的 C# 操作 Word的代码

    using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word; ...

随机推荐

  1. java中你不知道的字符串知识!!!

    声明:这是上次写完String和StringBuffer后的补充(看上次的请复制链接在搜索栏粘贴访问) 链接:http://www.cnblogs.com/ytsbk/p/7420581.html 一 ...

  2. SQL语句的CRUD

    一.基础 .说明:创建数据库 CREATE DATABASE database-name .说明:删除数据库 drop database dbname .说明:备份sql server --- 创建 ...

  3. java并发之非阻塞算法介绍

    在并发上下文中,非阻塞算法是一种允许线程在阻塞其他线程的情况下访问共享状态的算法.在绝大多数项目中,在算法中如果一个线程的挂起没有导致其它的线程挂起,我们就说这个算法是非阻塞的. 为了更好的理解阻塞算 ...

  4. API Gateway性能比较:NGINX vs. ZUUL vs.Cloud Gateway vs. Linkerd[译]

      2018-03-04 15:07 联发科的反思 前几天拜读了 OpsGenie 公司(一家致力于 Dev & Ops 的公司)的资深工程师 Turgay elik 博士写的一篇文章(链接在 ...

  5. Load balancer does not have available server for client

    最近在研究spring-cloud,研究zuul组件时发生下列错误: Caused by: com.netflix.client.ClientException: Load balancer does ...

  6. BOM(浏览器对象模型)

    .t1 { background-color: #ff8080; width: 1100px; height: 40px } 一.BOM(浏览器对象模型) 1.screen对象. console.lo ...

  7. .Net Core vs .Net Framework 如何为一个应用程序选择一个运行时

    .Net Core是下一件大事吗?我已经使用了一段时间了,我倾向认为它是.事实上,我们推测,在2018年,对这项技术熟练的开发人员将会有巨大的需求.但是它和.Net Framework的区别是什么?你 ...

  8. APNs 推送原理及问题

    http://bbs.csdn.net/topics/390461996 在 iOS 平台上,大部分应用是不允许在后台运行并连接网络的.在应用没有被运行的时候,只能通过 Apple Push Noti ...

  9. goroutine背后的系统知识

    http://www.sizeofvoid.net/goroutine-under-the-hood/ o语言从诞生到普及已经三年了,先行者大都是Web开发的背景,也有了一些普及型的书籍,可系统开发背 ...

  10. python获取操作系统平台、版本及架构

    platform模块提供了底层系统平台的相关信息 系统架构 32位还是64位 >>>import platform >>> platform.architectur ...