上一篇 C#开源组件DocX处理Word文档基本操作(一) 介绍了DocX的段落、表格及图片的处理,本篇介绍页眉页脚的处理。

示例代码所用DocX版本为:1.3.0.0。关于版本的区别,请参见上篇,而对于版本不同的起因,请参见 开源组件DocX版本区别点滴 一文。

代码如下:

第一部分:基本的页眉页脚处理(包括图片插入)

private void DocXSetHeaderFooter(DocX document)
{
document.AddHeaders(); //增加页眉
document.AddFooters(); //增加页脚 document.DifferentFirstPage = true; // 首页有不同的页眉页脚
document.DifferentOddAndEvenPages = true; // 奇偶页有不同的页眉页脚
Header header_first = document.Headers.First; //首页页眉
Header header_odd = document.Headers.Odd; //奇数页眉
Header header_even = document.Headers.Even; //偶数页眉 Footer footer_first = document.Footers.First; //首页页脚
Footer footer_odd = document.Footers.Odd; //奇数页脚
Footer footer_even = document.Footers.Even; //偶数页脚
var HFFont = "微软雅黑"; //首页页眉 header_first
Paragraph pHeaderFirst = header_first.Paragraphs.Count > ? header_first.Paragraphs.First() : header_first.InsertParagraph();
pHeaderFirst.AppendPicture(document.AddImage(@"_Word.jpg").CreatePicture());
pHeaderFirst.Append("首页页眉").Font(HFFont).FontSize().Bold().Alignment = Alignment.center; //奇数页眉
Paragraph pHeaderOdd = header_odd.Paragraphs.Count > ? header_odd.Paragraphs.First() : header_odd.InsertParagraph();
pHeaderOdd.Append("奇数页页眉").Font(HFFont).FontSize().Alignment = Alignment.center;
//偶数页眉
Paragraph pHeaderEven = header_even.Paragraphs.Count > ? header_even.Paragraphs.First() : header_even.InsertParagraph();
pHeaderEven.Append("偶数页页眉").Font(HFFont).FontSize().Alignment = Alignment.center; //首页页脚
Paragraph pFooterFirst = footer_first.Paragraphs.Count > ? footer_first.Paragraphs.First() : footer_first.InsertParagraph();
pFooterFirst.Append("第页 共页").Font("微软雅黑").FontSize();
pFooterFirst.InsertPageNumber(PageNumberFormat.normal, );
pFooterFirst.InsertPageCount(PageNumberFormat.normal, ); //normal阿拉伯数字 roman罗马数字
pFooterFirst.Alignment = Alignment.center; //奇数页脚
Paragraph pFooterOdd = footer_odd.Paragraphs.Count > ? footer_odd.Paragraphs.First() : footer_odd.InsertParagraph();
//现在可以同上面一样处理基本的设置,下面是页眉插入表格及奇偶页的不同设置
DocXSetFooter(pFooterOdd.FontSize(), HFFont); //偶数页脚
Paragraph pFooterEven = footer_even.Paragraphs.Count > ? footer_even.Paragraphs.First() : footer_even.InsertParagraph();
//现在可以同上面一样处理基本的设置,下面是页眉插入表格及奇偶页的不同设置
DocXSetFooter(pFooterEven.FontSize(), HFFont, false);
}

第二部分,页脚表格及奇偶栏内容不同设置(你也可以同样来处理页眉):

private void DocXSetFooter(Paragraph pFooter, string hfFont = null, bool IsOdd = true)
{
Table tbl = pFooter.InsertTableBeforeSelf(, );
tbl.Design = TableDesign.None; //TableDesign.TableGrid; //
//tbl.AutoFit = AutoFit.Contents;//内容 //AutoFit.ColumnWidth;//字段宽度 //AutoFit.Fixed;//固定
tbl.SetWidthsPercentage(new float[] { 30f, 40f, 30f }, 1500f);
//tbl.SetWidths(new float[] { 300f, 600f, 300f }); if (IsOdd)
{
tbl.Rows[].Cells[].Paragraphs.First().InsertText(DateTime.Now.ToString("yyyy年MM月dd日"));
tbl.Rows[].Cells[].Paragraphs.First().InsertText("这里加入公司名称或其它信息");
tbl.Rows[].Cells[].Paragraphs.First().InsertText("第页 共页");
tbl.Rows[].Cells[].Paragraphs.First().InsertPageNumber(PageNumberFormat.normal, );
tbl.Rows[].Cells[].Paragraphs.First().InsertPageCount(PageNumberFormat.normal, );
}
else
{
tbl.Rows[].Cells[].Paragraphs.First().InsertText("第页 共页");
tbl.Rows[].Cells[].Paragraphs.First().InsertPageNumber(PageNumberFormat.normal, );
tbl.Rows[].Cells[].Paragraphs.First().InsertPageCount(PageNumberFormat.normal, );
tbl.Rows[].Cells[].Paragraphs.First().InsertText("这里加入公司名称或其它信息");
tbl.Rows[].Cells[].Paragraphs.First().InsertText(DateTime.Now.ToString("yyyy年MM月dd日"));
}
tbl.Rows[].Cells[].Paragraphs.First().Font(hfFont).FontSize().Alignment = Alignment.left;
tbl.Rows[].Cells[].Paragraphs.First().Font(hfFont).FontSize().Alignment = Alignment.center;
tbl.Rows[].Cells[].Paragraphs.First().Font(hfFont).FontSize().Alignment = Alignment.right; pFooter.Remove(false); //移去尾部多余的段落
}

这样可以处理一些需要在页眉页脚进行特殊栏目的设置。

好了,用开源组件DocX来处理Word文档的基本操作就介绍完了,谢谢你能来阅读,并希望能对你有些许帮助,就是我的最大安慰了。

最新文章

  1. python 文件操作(转)
  2. RoboGuice 3.0 (三)总结篇
  3. seajs和requirejs
  4. C++/C#互调步骤
  5. 翻译《Writing Idiomatic Python》(四):字典、集合、元组
  6. 【BZOJ】1934: [Shoi2007]Vote 善意的投票(网络流/-二分图匹配)
  7. 【暑假】[深入动态规划]UVAlive 4794 Sharing Chocolate
  8. 想挑战AlphaGO吗?先和PostgreSQL玩一玩?? PostgreSQL与人工智能(AI)
  9. Spring MVC 与ExtJS完美集成
  10. Python中subplots_adjust函数的说明
  11. 玩玩Qt(一)
  12. dll被设置为用记事本打开的解决方法
  13. Prism框架研究(一)
  14. python--类中的对象方法、类方法、静态方法的区别
  15. Win10系列:C#应用控件基础10
  16. swift 相机、相册、定位的权限判断
  17. 对于两个初始时设置为Sensor的刚体,不会触发preSolve和postSolve
  18. cygwin简介及使用
  19. pthread_join与pthread_detach细节问题
  20. PHP开发微信被动回复消息遇到的大坑

热门文章

  1. .NET Core开发的iNeuOS工业互联平台,iNeuKernel物联网核心组件在Docker容器中部署。
  2. [bzoj4942] [洛谷P3822] [NOI2017] 整数
  3. 以其他控制器为目标 在视图中生成输出URL
  4. 异想家Eclipse的偏好配置
  5. swoole(PHP异步网络通信引擎)的结构和运行流程
  6. Cassandra2.2.10安装过程
  7. Centos 7 最小化时间服务部署配置
  8. JDK源码之Byte类分析
  9. java.io 包下的类有哪些 + 面试题
  10. 五、Django学习之基于对象的跨表查询