C#开源组件DocX处理Word文档基本操作(二)
上一篇 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文档基本操作(二)的更多相关文章
- C#开源组件DocX处理Word文档基本操作(一)
C#中处理Word文档,是大部分程序猿绕不过的一道门.小公司或一般人员会选择使用开源组件.目前网络上出现的帖子,大部分是NPOI与DocX,其它的也有.不啰嗦了,将要使用DocX的基本方法贴出来,供参 ...
- C#使用Docx操作word文档
C#使用Docx编写word表格 最近接手了一个小Demo,要求使用Docx,将Xml文件中的数据转换为word文档,组织数据形成表格. 写了已经一周,网络上的知识太零碎,就想自己先统计整理出来,方便 ...
- Docx 生成word文档二
/// <summary> /// 生产word 文档 /// </summary> public class GenerateWord { /// <summary&g ...
- Docx 生成word文档
1.生成word代码 /// <summary> /// 生成word文档 /// </summary> /// <param name="tempPath&q ...
- C#中使用Spire.docx操作Word文档
使用docx一段时间之后,一些地方还是不方便,然后就尝试寻找一种更加简便的方法. 之前有尝试过使用Npoi操作word表格,但是太烦人了,随后放弃,然后发现免费版本的spire不错,并且在莫种程度上比 ...
- C# 复制一个Word文档的部分或全部内容到另一个Word文档
C# 复制一个Word文档的部分或全部内容到另一个Word文档 我最近喜欢折腾Office软件相关的东西,想把很多Office软件提供的功能用.NET来实现,如果后期能把它用来开发一点我自己的小应用程 ...
- 打开和写入word文档
一. 使用win32读取word内容 # -*- coding: utf-8 -*- from win32com import client as wc def readDocx2(): word = ...
- python自动化之word文档
#########################docx文件############################ ''' .docx文件有很多结构,有3种不同的类型来表示 在最高一层,Docum ...
- 使用PHPWord生成word文档
有时我们需要把网页内容保存为Word文档格式,以供其他人员查看和编辑.PHPWord是一个用纯PHP编写的库,使用PHPWord可以轻松处理word文档内容,生成你想要的word文档. 下载源码 安装 ...
随机推荐
- Spring Cloud(三):Web服务客户端之Feign
前文介绍了实现客户端负载均衡的Ribbon,但直接使用Ribbon的API来实现服务间的调用相对较为繁琐,服务间的调用能否像本地接口调用一样便捷.透明,更符合编程习惯呢?Feign就是用来干这事的. ...
- playbooks框架部署远程主机
进入到ansible和python环境 进入python3.6虚拟环境 #su - deploy #source .py3-a2.5-env/bin/activate 加载ansible 2.5版本 ...
- PageHelper踩坑
刚开始死活分不了页,只显示默认的前 10条.搞了一下午,打了无数个断点都试不出毛病在哪. 下班又死磕到快8点,就在我已经绝望的时候,最后终于试出来了,把page.getTotal()给传到前端就好了. ...
- DataFrame数据合并
一.join 作用:默认情况下,他是把行索引相同的数据合并到一起注意:以左为准,没有的部分用NaN补全 例子 import pandas as pd import numpy as np df1 = ...
- ORM基础4 跨表查询+原子性操作
一.跨表查询 1.# # 正向查找 对象查找 # book_obj = models.Book.objects.get(id=3) # print(book_obj) # ret = book_obj ...
- Java单体应用 - Markdown - 03.高级技巧
原文地址:http://www.work100.net/training/monolithic-markdown-advance.html 更多教程:光束云 - 免费课程 高级技巧 序号 文内章节 视 ...
- Python入门小建议
"java从心"正式更名"KEN DO EVERTHING".Ken(Can) Do Everything,无所不能 Believe youseft then ...
- Windows10 java环境配置
1.下载 JDK 下载地址:https ://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html 点击下载按钮 ...
- dotnet restore 初次运行 这个 指令 会安装 特别多的 4.0.0 或者 4.1 的 rc2-24027的 东东 这些东西。
- 使用Razor表达式 使用条件语句 来自 精通ASP-NET-MVC-5-弗瑞曼