上一篇 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文档的基本操作就介绍完了,谢谢你能来阅读,并希望能对你有些许帮助,就是我的最大安慰了。

C#开源组件DocX处理Word文档基本操作(二)的更多相关文章

  1. C#开源组件DocX处理Word文档基本操作(一)

    C#中处理Word文档,是大部分程序猿绕不过的一道门.小公司或一般人员会选择使用开源组件.目前网络上出现的帖子,大部分是NPOI与DocX,其它的也有.不啰嗦了,将要使用DocX的基本方法贴出来,供参 ...

  2. C#使用Docx操作word文档

    C#使用Docx编写word表格 最近接手了一个小Demo,要求使用Docx,将Xml文件中的数据转换为word文档,组织数据形成表格. 写了已经一周,网络上的知识太零碎,就想自己先统计整理出来,方便 ...

  3. Docx 生成word文档二

    /// <summary> /// 生产word 文档 /// </summary> public class GenerateWord { /// <summary&g ...

  4. Docx 生成word文档

    1.生成word代码 /// <summary> /// 生成word文档 /// </summary> /// <param name="tempPath&q ...

  5. C#中使用Spire.docx操作Word文档

    使用docx一段时间之后,一些地方还是不方便,然后就尝试寻找一种更加简便的方法. 之前有尝试过使用Npoi操作word表格,但是太烦人了,随后放弃,然后发现免费版本的spire不错,并且在莫种程度上比 ...

  6. C# 复制一个Word文档的部分或全部内容到另一个Word文档

    C# 复制一个Word文档的部分或全部内容到另一个Word文档 我最近喜欢折腾Office软件相关的东西,想把很多Office软件提供的功能用.NET来实现,如果后期能把它用来开发一点我自己的小应用程 ...

  7. 打开和写入word文档

    一. 使用win32读取word内容 # -*- coding: utf-8 -*- from win32com import client as wc def readDocx2(): word = ...

  8. python自动化之word文档

    #########################docx文件############################ ''' .docx文件有很多结构,有3种不同的类型来表示 在最高一层,Docum ...

  9. 使用PHPWord生成word文档

    有时我们需要把网页内容保存为Word文档格式,以供其他人员查看和编辑.PHPWord是一个用纯PHP编写的库,使用PHPWord可以轻松处理word文档内容,生成你想要的word文档. 下载源码 安装 ...

随机推荐

  1. mysql索引创建和使用细节

    最近困扰自己很久的膝盖积液手术终于做完,在家养伤,逛技术博客看到easyswoole开发组成员仙士可博客有关mysql索引方面的知识,自己打算重温下. 正常业务起步数据表数据量较少,不用考虑使用索引, ...

  2. 双重 hash

    #include<stdio.h> #include<map> #include<queue> #include<algorithm> using na ...

  3. Jquery电子签名制作_jSignature

    今天用Jquery的jSignature库制作一个电子签名 后台.net core上传到指定文件夹 下载jquery库 提取码:rd9g html @{ Layout = null; } <!D ...

  4. Python语言的configparser模块便捷的读取配置文件内容

    配置文件是在写脚本过程中经常会用到的,所以读取配置文件的模块configparser也非常重要,但是很简单. 首先我们的配置文件内容为: 这样的配置文件,[]里面的内容叫section,[]下面的内容 ...

  5. 关于爬虫的日常复习(17)——scrapy系列1

  6. 跨源请求cors和jsonp

    0.产生跨域的原因 浏览器的同源策略 什么是浏览器的同源策略? src开发 ajax禁止 解决方法 jsonp 通过src绕过浏览器的同源策略 缺点:只发送GET请求 cors 通过设置相应头 分类 ...

  7. shh登入不能自动执行.bashrc

    在linux 上新安装的anconda来管理软件,把环境变量放在home目录的.bashrc.结果每次开终端,不能直接使用conda.需要手动加环境变量. 用户登入后计算机执行了哪些文件 用户登录时b ...

  8. Photoshop 2020安装教程

    首先来看看新增功能 [Photoshop 2020新增功能] 1.ipad和云文档 2.预设改进 3.新对象选择工具 4.转换行为一致 5.改进的属性面板 6.智能对到图层 7.增强的转换变形. 需要 ...

  9. C#反射与特性(四):实例化类型

    目录 1,实例化类型 1.1 Activator.CreateInstance() 1.2 ConstructorInfo.Invoke() 2,实例化委托 3,实例化泛型类型 3.1 实例化泛型 3 ...

  10. centos7搭建hadoop2.10高可用(HA)

    本篇介绍在centos7中搭建hadoop2.10高可用集群,首先准备6台机器:2台nn(namenode);4台dn(datanode):3台jns(journalnodes) IP hostnam ...