页眉页脚经常使用于文章排版,在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. Windows cannot find ". Make sure you typed the name correctly, and then try again

    https://answers.microsoft.com/en-us/windows/forum/windows_10-desktop/windows-10-explorerexe-error-wi ...

  2. 关于Javascript的forEach 和 map

    本篇博客转载自 https://blog.fundebug.com/2018/02/05/map_vs_foreach/ 如果你已经有使用JavaScript的经验,你可能已经知道这两个看似相同的方法 ...

  3. BZOJ 1050 枚举+并查集

    思路: 枚举最大边 像Kruskal一样加边 每回更新一下 就搞定了- //By SiriusRen #include <cstdio> #include <cstring> ...

  4. 之前写的收集的一些c++代码片,算法排序,读文件,写日志,快速求积分等等

    写日志: class LogFile { public: static LogFile &instance(); operator FILE *() const { return m_file ...

  5. vue 实现文本域还剩多少字符

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. TwinCAT 3中基于UDP协议通讯的C++实现

    因为项目需要,学习了TwinCAT3中使用UDP协议进行通讯的基本知识.这个做个简单的笔记,方便以后查询. 1 概述 倍福为了实现从实时环境中直接访问网卡(network cards)专门提供了一个函 ...

  7. 威联通NAS 网站无法登录,可以ssh情况下重启设备方法

    步骤: 1.VPN登录NAS 2.PUTTY SSH登录设备 3.reboot设备 等待重启约5分钟.

  8. tr---对来自标准输入的字符进行替换、压缩和删除。

    tr命令可以对来自标准输入的字符进行替换.压缩和删除.它可以将一组字符变成另一组字符,经常用来编写优美的单行命令,作用很强大. 语法 tr(选项)(参数) 选项 -c或——complerment:取代 ...

  9. iOS 的组件化开发

    在一个APP开发过程中,如果项目较小且团队人数较少,使用最基本的MVC.MVVM开发就已经足够了,因为维护成本比较低. 但是当一个项目开发团队人数较多时,因为每个人都会负责相应组件的开发,常规开发模式 ...

  10. 安装成功的nginx,如何添加未编译安装模块(非覆盖安装http_image_filter_module)

    背景:1.做了图片上传小项目.2.图片上传,需要多图管理.3.图片上传,需要存储到Fastdfs.4.Fastdfs上的图片,和Nginx结合.5.Nginx从Fastdfs获得的图片,需要使用缩略图 ...