首页 > 编程语言 >在C#中实现Word页眉页脚的所有功能

在C#中实现Word页眉页脚的所有功能

时间:2022-12-30 12:01:41浏览次数:88  
标签:Word CharacterFormat C# section 页脚 页眉


页眉页脚常用于文章排版,在Word工具栏里,我们可以添加页眉,页脚,页码,日期和时间,图片等信息和内容。页眉/页脚有两个额外选项:首页不同,奇偶页不同。有时在不同的节(section)里插入不同的页眉页脚。从零开始在C#实现这些功能,工作量巨大。所以,今天向大家推荐一款免费的API库,Free Spire.Doc可以从​​CSDN​​,​​官网​​​,和 ​​Nuget​​直接下载。功能强大,容易上手。

这篇文章分为三个部分:
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页眉页脚的所有功能_net

第二部分:在C#里实现Word页眉页脚的图文混排

与文本相比,图片更容易吸引人注意。我们经常同时使用文本和图片(即图文混排)来引人注目。在一些正式报告,法律文件中的页眉页脚会使用到图文混排,以达到上述目的。这里我使用了维基百科的标志和关于它的一段介绍来展示在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");

在C#中实现Word页眉页脚的所有功能_word_02

值得一提的是,可以使用 ” 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");

在C#中实现Word页眉页脚的所有功能_word_03

使用该工具,还可以在奇数页,偶数页中设置不同的页码格式,添加不同的图片和表格,并设置不同的格式。实现代码可参照第一部分和第二部分。至于首页不同,代码与奇偶页不同相差无几,此处我仅列举两者代码中不同的部分:

//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添加页眉页脚的需求,是一款值得花时间测试使用的工具,对项目效率有极大的提升。
感谢阅读,欢迎留下宝贵意见。


标签:Word,CharacterFormat,C#,section,页脚,页眉
From: https://blog.51cto.com/u_15656056/5979851

相关文章

  • 用pageOffice控件实现 office word文档 编辑Word加图片的功能
    OA办公中,业务需要多人编辑word文档,需要加图片的功能。怎么实现word文档的编辑加图片呢?2实现方法通过pageOffice实现简单的在线打开编辑word时,通过设置关键代码:DataR......
  • HTML 2 WORD
    GroupAddauo_gruop,ahk_exemsedge.exeGroupAddauo_gruop,ahk_exechrome.exeGroupAddauo_gruop,ahk_exesnipaste.exeGroupAddauo_gruop,ahk_exemspaint.exe......
  • word vba 操作表格, 设置表格的样式
    一、新建和删除表格样式Sub表格样式()DimoldstyleAsStyle,tablestyleAsStyle'OnErrorResumeNext'删除表格样式'ActiveDocument.Styles("mytablestyl......
  • excel、word、PPT中插入PDF文件不显示图标问题
    插入PDF对象,不显示正确的PDF图标临时解决办法:手动修改对象图标具体位置,可在C:\Windows\Installer目录下搜索PDFFile_8.ico尝试如下操作:手动复制生成C:\Windows\SysW......
  • Java 导出word、pdf、excel的echart图形
    引用文章:https://www.codenong.com/cs109245248/利用jfreechart依赖导出点击查看代码<dependency><groupId>org.jfree</groupId><artifactId>jfreechart<......
  • word vba 操作表格, 设置表格中的段落
    一、表格中的段落设置Sub表格中的段落设置()DimpAsParagraphForEachpInActiveDocument.ParagraphsIfp.Range.Information(wdWithInTable)Then'针对......
  • 通过数据库两种方式修改WordPress密码
    这里介绍两种方式修改WordPress密码方式:方法一、mysql修改后台密码1)登上数据库,使用的语句是:mysql-uroot-p 2)输入语句查看所有数据库名:showdata......
  • SiteFactory支持Word图片粘贴
    ​ 这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下)<%@ page language="java"......
  • New password: BAD PASSWORD: The password contains the user name in some form
    有些时候服务器规定了用户的密码中不允许包含用户名,例如test用户你不能使用类似abtest123这种密码,但是我就是想要这么设置,可以使用chpasswd设置,我不确定是否在所有情况下都......
  • SiteFactory支持Word图片上传
    ​ 自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了。一般情况下我们将Word内容粘贴到Web编辑......