上一篇 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. js去除字符串的前后空白

    使用gravatar生成邮箱的头像时需要对邮箱地址哈希化,其中需要去除邮箱地址的首尾空白,查找了一些资料,总结一下 使用 js 提供的函数 trim() trim() 方法会删除一个字符串两端的空白字 ...

  2. js复制变量值

    来源:JavaScript高级程序设计(第3版)69页. 例如 : var a=1;   var b = a  ;      这里就是把a的值复制给变量  b   了. 但是 复制的变量值  分为  ...

  3. acmPush模块示例demo

    感谢论坛版主 马浩川 的分享. 模块介绍:  阿里移动推送(Alibaba Cloud Mobile Push)是基于大数据的移动智能推送服务,帮助App快速集成移动推送的功能,在实现高效.精确.实时 ...

  4. BFT-SMaRt:用Netty做客户端的可靠信道

    目录 一.Netty服务端的构建 1. 父类构造函数 ① 查找缓存 ② 相关日志 2. 服务端构造 ① 配置读取 ② 服务端配置 3. 服务端功能 ① 通用接口功能 ② Channel处理器 4. 节 ...

  5. 基于selenium爬取京东

    爬取iphone 注意:browser对象会发生变化,当对当前网页做任意操作时 import time from selenium import webdriver from selenium.web ...

  6. 13、python的路径处理

    前言:本文主要介绍python中路径的处理,包括os模块和有关的2个魔法变量. 一.os模块 python里面的os模块有许多方法可以让我们通过代码实现创建,删除和更改目录,具体如下: os.getc ...

  7. python类型-集合

    集合对象是一组无序排列的可哈希的值,集合成员可以做字典的键.集合有两种类型:可变集合,可以添加和删除元素,可变集合不是可哈希的,不能用作字典的键也不能作为其它集合中的元素:不可变集合相反,有哈希值,可 ...

  8. Linux系统搭建私有CA证书服务器

    一.CA简介 CA是什么?CA是Certificate Authority的简写,从字面意思翻译过来是凭证管理中心,认证授权.它有点类似我们生活中的身份证颁发机构,这里的CA就相当于生活中颁发身份证的 ...

  9. python3读取excel文档数据

    实现场景: 1.读取Excel表数据 2.把数据作为参数传给后面的函数 3.后面的函数循环读取参数执行操作 本案例Excel内容为下图,becks为表名 先贴代码 import xlrd #读取exc ...

  10. JDBC详细说明+使用

    JDBC详解 一.相关概念 1.什么是JDBC JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提 ...