在C#中实现Word页眉页脚的全部功能
页眉页脚经常使用于文章排版,在Word工具栏里。我们能够加入页眉,页脚,页码,日期和时间。图片等信息和内容。页眉/页脚有两个额外选项:首页不同,奇偶页不同。有时在不同的节(section)里插入不同的页眉页脚。
从零開始在C#实现这些功能。工作量巨大。
所以,今天向大家推荐一款免费的API库,Free Spire.Doc能够从CSDN,官网,和 Nuget直接下载。功能强大,easy上手。
这篇文章分为三个部分:
1. 怎样在C#里为Word加入页码。
2. 怎样在C#里实现Word页眉页脚的图文混排。
3. 怎样在C#里实现Word页眉页脚的奇偶页不同和首页不同。
友情提示:Free Spire.Doc 能够独立创建和载入Word文档,这里的微软Word仅用于查看效果。
第一部分:在C#里为Word加入页码
假设Word文档包括很多页,我们能够在页眉页脚处加入页码。
该页码可显示总页数,当前页数。加入Free Spire.Doc Bin 目录里的.dll至Visual Studio作为引用。我使用了下面代码在C#中了实现对Word页码的加入。
//Create a Word document and add a section
Document doc = new Document();
Section section = doc.AddSection();
//Initial a HeaderFooter class
HeaderFooter header = doc.Sections[0].HeadersFooters.Header;
Paragraph headerParagraph = header.AddParagraph();
//Use the AppendField method to get the FieldPage and FieldNumpages
headerParagraph.AppendField("page number", FieldType.FieldPage);
headerParagraph.AppendText(" of ");
headerParagraph.AppendField("number of pages", FieldType.FieldNumPages);
headerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
//Save and launch the document
doc.SaveToFile("Test.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Test.docx");
在页脚处加入页码的方法与上述代码类似,这里我就不再赘述了。有须要的朋友能够參考上面的部分。因为我在文档中没有加入额外的页数,所以截图部分显示的是 1 of 1.
第二部分:在C#里实现Word页眉页脚的图文混排
与文本相比。图片更easy吸引人注意。我们经常同一时候使用文本和图片(即图文混排)来引人注目。在一些正式报告,法律文件里的页眉页脚会使用到图文混排,以达到上述目的。这里我使用了维基百科的标志和关于它的一段介绍来展示在C#里实现页脚部分的图文混排。同一时候,大家别忘了加入系统引用”System. Drawing”。
//Create a Word document and initial the footer class
Document document = new Document();
Section section = document.AddSection();
HeaderFooter footer = document.Sections[0].HeadersFooters.Footer;
//Add text and image to the footer
Paragraph paragraph = footer.AddParagraph();
DocPicture footerImage = paragraph.AppendPicture(Image.FromFile("Wiki.bmp"));
TextRange TR = paragraph.AppendText("Supported and Hosted by the non-profit Wikimedia Foundation.");
//Format the text and image
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 13;
TR.CharacterFormat.TextColor = Color.Black;
TR.CharacterFormat.Bold = true;
// Save the document and launch to see the output
document.SaveToFile("Test.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Test.docx");
值得一提的是,能够使用 ” TextWrappingStyle” 和 ”TextWrappingType”来编辑图片在文本中的位置和自己主动换行:
footerImage.TextWrappingStyle = TextWrappingStyle.Through;
footerImage.TextWrappingType = TextWrappingType.Left;
我尝试过使用free Spire.Doc在页眉页脚中加入表格,居然可行。有这需求的朋友,也能够測试一下。
第三部分:在C#里实现Word页眉页脚的奇偶页不同和首页不同
Word文件有默认设置每一页的页眉页脚都同样。然而在报告、书籍等排版中往往须要不同的页眉页脚来美化排版。日常编程中,假设我们仅仅须要首页页眉页脚不同。我们能够把首页单独成节。Spire.Doc 提供了更加简便高速的方法来设置页眉页脚首页不同,奇偶页不同。这里,我先以在C#中实现页眉奇偶页不同为例。代码例如以下:
//Create a document and section
Document document = new Document();
Section section = document.AddSection();
//Set the bool true and add the Odd Header and Even Header
section.PageSetup.DifferentOddAndEvenPagesHeaderFooter = true;
Paragraph oddHeader = section.HeadersFooters.OddHeader.AddParagraph();
TextRange oddHT = oddHeader.AppendText("Coding is the art of life");
Paragraph evenHeader = section.HeadersFooters.EvenHeader.AddParagraph();
TextRange evenHT = evenHeader.AppendText("Time is the most valuable thing");
//Format the headers
oddHeader.Format.HorizontalAlignment = HorizontalAlignment.Center;
oddHT.CharacterFormat.FontName = "Calibri";
oddHT.CharacterFormat.FontSize = 20;
oddHT.CharacterFormat.TextColor = Color.Green;
oddHT.CharacterFormat.Bold = true;
evenHeader.Format.HorizontalAlignment = HorizontalAlignment.Center;
evenHT.CharacterFormat.FontName = "Calibri";
evenHT.CharacterFormat.FontSize = 20;
evenHT.CharacterFormat.TextColor = Color.Green;
evenHT.CharacterFormat.Bold = true;
//Launch to see effects
document.SaveToFile("R.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("R.docx");
使用该工具,还能够在奇数页,偶数页中设置不同的页码格式。加入不同的图片和表格,并设置不同的格式。实现代码可參照第一部分和第二部分。至于首页不同。代码与奇偶页不同相差无几,此处我仅列举两者代码中不同的部分:
//set the first page header and footer
section.PageSetup.DifferentFirstPageHeaderFooter = true;
Paragraph paragraph1 = section.HeadersFooters.FirstPageHeader.AddParagraph();
Paragraph paragraph2 = section.HeadersFooters.FirstPageFooter.AddParagraph();
//set the rest page header and footer
Paragraph paragraph3 = section.HeadersFooters.Header.AddParagraph();
Paragraph paragraph4 = section.HeadersFooters.Footer.AddParagraph();
假设你仅仅须要首页的页眉页脚,不设置其它页的页眉页脚就能够了。
这样就仅仅会有首页的页眉页脚。
结论:
在我看来, free Spire.Doc完美满足了我在C#中为Word加入页眉页脚的需求,是一款值得花时间測试使用的工具,对项目效率有极大的提升。
感谢阅读,欢迎留下宝贵意见。
在C#中实现Word页眉页脚的全部功能的更多相关文章
- 使用C#在word中插入页眉页脚
//插入页脚 public void InsertFooter(string footer) { if (ActiveWindow.ActivePane.View.Type == WdViewType ...
- C#word(2007)操作类--新建文档、添加页眉页脚、设置格式、添加文本和超链接、添加图片、表格处理、文档格式转化
转:http://www.cnblogs.com/lantionzy/archive/2009/10/23/1588511.html 1.新建Word文档 #region 新建Word文档/// &l ...
- C# 操作Word页眉页脚——奇偶页/首页不同、不连续设置页码、复制页眉页脚、锁定页眉页脚、删除页眉页脚
前言 本文是对Word页眉页脚的操作方法的进一步的阐述.在“C# 添加Word页眉页脚.页码”一文中,介绍了添加简单页眉页脚的方法,该文中的方法可满足于大多数的页眉页脚添加要求,但是对于比较复杂一点的 ...
- js中window.print()去除页眉页脚
//jsp打印时去除页眉页页脚 打印前加入下面代码即可 var HKEY_Root,HKEY_Path,HKEY_Key; HKEY_Root="HKEY_CURRENT_USER" ...
- word页眉页脚 首页 索引 正文各不同的处理方法
1.在目录和正文之间,加入分隔符——分节符——下一页,然后再添加页眉页脚,然后再添加索引:
- Office WORD如何为每一页设置不同的页眉页脚
如下图所示,我想要为封面和目录,摘要等等设置不同的页眉页脚(一般封面和目录不需要页脚) 而从正文开始,套用相同的页眉和以页数作为页脚(注意"第一章 绪论"不是这个文档的第一页) ...
- 怎么给PDF去除页眉页脚
PDF文件我们现在都会使用到,但有时需编辑PDF文件的时候,小伙伴们都知道该怎么操作吗,不知道的小伙伴不用担心,今天小编就来跟大家分享一下怎么删除PDF文件的页眉页脚,我们一起来看看下面的文章吧 操作 ...
- LaTeX 页眉页脚的设置
Latex中页眉页脚的设置 1. 首先要加页眉页脚的话,需要启动宏: 我通常用fancyhdr宏包来设置页眉和页脚. \usepackage{fancyhdr} 我们在 LaTeX 中先把 page ...
- 【Itext】7步制作Itext5页眉页脚pdf实现第几页共几页
itext5页眉页脚工具类,实现page x of y 完美兼容各种格式大小文档A4/B5/B3,兼容各种文档格式自动计算页脚XY轴坐标 鉴于没人做的这么细致,自己就写了一个itext5页眉页脚工具类 ...
随机推荐
- django 笔记2
默默坚持 :路由系统 URL :视图 request.GET request.POST request.FILES #checkbox等多选的内容 request.POST.getlist() #上传 ...
- Codeforces 344C Rational Resistance
Description Mad scientist Mike is building a time machine in his spare time. To finish the work, he ...
- ASP.net Web API允许跨域访问解决办法
来源 http://blog.csdn.net/wxg_kingwolfmsncn/article/details/48545099 遇到此跨域访问问题,解决办法如下: 方法一: 1. 在we ...
- Hexo High一下以及压缩排版问题
背景介绍 集成Hight一下以及Gulp-html压缩之后出现的问题: High一下功能多次点击,会创建多个Audio对象,导致同时播放多次音乐,重音.解决办法:判断是否添加Audio对象,如果存在则 ...
- 基于zookeeper实现分布式配置中心(二)
上一篇(基于zookeeper实现分布式配置中心(一))讲述了zookeeper相关概念和工作原理.接下来根据zookeeper的特性,简单实现一个分布式配置中心. 配置中心的优势 1.各环境配置集中 ...
- OpenStack_Swift源代码分析——Object-auditor源代码分析(2)
1 Object-aduitor审计详细分析 上一篇文章中,解说了Object-aduitor的启动,当中审计的详细运行是AuditorWorker实现的.在run_audit中实例化了Auditor ...
- QCAD Plugin 开发
QCAD Plugin 开发 eryar@163.com Abstract. QCAD是基于GPL协议的开源CAD软件,核心功能基于Qt使用C++开发,界面及其交互使用Javascript脚本进行开发 ...
- 关于集合类set
list中允许有重复的元素,而set中不允许有重复的元素. package cn.hncu.Test; import java.util.HashMap; import java.util.Map; ...
- Handle-postDelayed 延迟操作
今天在工作的时候,遇到了一个方法,是关于Handle来实现延时操作的,自己写了一个小demo,学习总结如下 xml <?xml version="1.0" encoding= ...
- html5中的空格符
html5中的空格符 1,Html中空格 不断行的空白(1个字符宽度) 半个空白(1个字符宽度) 一个空白(2个字符宽度) 窄空白(小于1个字符宽度) 2,Css ...