页眉页脚经常使用于文章排版,在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页眉页脚的全部功能的更多相关文章

  1. 使用C#在word中插入页眉页脚

    //插入页脚 public void InsertFooter(string footer) { if (ActiveWindow.ActivePane.View.Type == WdViewType ...

  2. C#word(2007)操作类--新建文档、添加页眉页脚、设置格式、添加文本和超链接、添加图片、表格处理、文档格式转化

    转:http://www.cnblogs.com/lantionzy/archive/2009/10/23/1588511.html 1.新建Word文档 #region 新建Word文档/// &l ...

  3. C# 操作Word页眉页脚——奇偶页/首页不同、不连续设置页码、复制页眉页脚、锁定页眉页脚、删除页眉页脚

    前言 本文是对Word页眉页脚的操作方法的进一步的阐述.在“C# 添加Word页眉页脚.页码”一文中,介绍了添加简单页眉页脚的方法,该文中的方法可满足于大多数的页眉页脚添加要求,但是对于比较复杂一点的 ...

  4. js中window.print()去除页眉页脚

    //jsp打印时去除页眉页页脚 打印前加入下面代码即可 var HKEY_Root,HKEY_Path,HKEY_Key; HKEY_Root="HKEY_CURRENT_USER" ...

  5. word页眉页脚 首页 索引 正文各不同的处理方法

    1.在目录和正文之间,加入分隔符——分节符——下一页,然后再添加页眉页脚,然后再添加索引:

  6. Office WORD如何为每一页设置不同的页眉页脚

    如下图所示,我想要为封面和目录,摘要等等设置不同的页眉页脚(一般封面和目录不需要页脚)   而从正文开始,套用相同的页眉和以页数作为页脚(注意"第一章 绪论"不是这个文档的第一页) ...

  7. 怎么给PDF去除页眉页脚

    PDF文件我们现在都会使用到,但有时需编辑PDF文件的时候,小伙伴们都知道该怎么操作吗,不知道的小伙伴不用担心,今天小编就来跟大家分享一下怎么删除PDF文件的页眉页脚,我们一起来看看下面的文章吧 操作 ...

  8. LaTeX 页眉页脚的设置

    Latex中页眉页脚的设置 1. 首先要加页眉页脚的话,需要启动宏: 我通常用fancyhdr宏包来设置页眉和页脚. \usepackage{fancyhdr} 我们在 LaTeX 中先把 page ...

  9. 【Itext】7步制作Itext5页眉页脚pdf实现第几页共几页

    itext5页眉页脚工具类,实现page x of y 完美兼容各种格式大小文档A4/B5/B3,兼容各种文档格式自动计算页脚XY轴坐标 鉴于没人做的这么细致,自己就写了一个itext5页眉页脚工具类 ...

随机推荐

  1. FZU--2188--过河(bfs暴力条件判断)

    过河I Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...

  2. Red Hat Linux 安装 (本地、网络安装)

    Red Hat Linux 安装 (本地.网络安装) 650) this.width=650;" onclick='window.open("http://blog.51cto.c ...

  3. double和decimal的ToString("#.##")方法使用的是四舍五入;

    顺带提一下: 1. double和decimal的ToString("#.##")方法使用的是四舍五入: 2. 静态类System.Math下的Round(decimal d, i ...

  4. android图片资源的适配问题

    原文: http://hi.baidu.com/weiyousheng/blog/item/c622d701b9dec6c2277fb5cc.html 在之前的版本中,只有一个drawable,而2. ...

  5. Debian9.5解决在root用户下启用VNC后lightdm+Xfce4只能使用root登录,其他用户无法登陆

    Debian9.5解决在root用户下启用VNC后lightdm+Xfce4只能使用root登录,其他用户无法登陆 最近在研究debian9.5 VNC的问题,当VNC使用tightVNC或tiger ...

  6. react-native flatlist setState修改数据视图不刷新解决方案

    给flatlist添加属性:handleMethod = {({viewableItems}) => this.handleViewableItemsChanged(viewableItems) ...

  7. hiho week 37 P1 : 二分·二分查找之k小数

    P1 : 二分·二分查找之k小数 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 在上一回里我们知道Nettle在玩&l ...

  8. FragmentTransaction的commit和commitAllowingStateLoss的差别

    1.什么是FragmentTransaction? 使用Fragment时.能够通过用户交互来运行一些动作.比方添加.移除.替换等. 全部这些改变构成一个集合,这个集合被叫做一个transaction ...

  9. ChinaVis2015 第一天会议

    第二届  ChinaVis 2015 在天津举行,非常幸运发现者个会议,并在导师的带领下參与本次会议. 主要要是以可视化与可视分析为背景进行讲座,以马匡六为Speaker.袁晓如,张加万等致辞开幕式. ...

  10. android 图片特效处理之锐化效果

    这篇将讲到图片特效处理的锐化效果.跟前面一样是对像素点进行处理,算法是通用的. 算法原理: 一.简单算法:分别获取当前像素点和八个周围像素点的RGB值,先求出当前像素点的RGB值与八个像素点RGB值的 ...