Word控件Spire.Doc 【页面设置】教程(10) ;通过 Spire.Doc 在 Word 文档的不同部分添加页码

这里将在 3 个部分内导入一个测试文档,如下图所示。

通过 Spire.Doc 在 Word 文档的不同部分添加页码

以下是详细步骤:

第 1 步:创建一个新文档并加载测试字文件。

Document document = new Document("test.docx"); 

第 2 步:为第一部分创建页脚并在其中添加页码。

HeaderFooter footer = document.Sections[0].HeadersFooters.Footer;Paragraph footerParagraph = footer.AddParagraph();footerParagraph.AppendField("page number", FieldType.FieldPage);footerParagraph.AppendText(" of ");footerParagraph.AppendField("number of pages", FieldType.FieldSectionPages);footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;

第 3 步:重新开始下一节的页码,并将起始页码设置为 1。

document.Sections[1].PageSetup.RestartPageNumbering = true;document.Sections[1].PageSetup.PageStartingNumber = 1;

第 4 步:对其余部分重复第 2 步和第 3 步,因此使用 for 循环更改代码。

for (int i = 0; i < 3; i++){HeaderFooter footer = document.Sections[i].HeadersFooters.Footer;Paragraph footerParagraph = footer.AddParagraph();footerParagraph.AppendField("page number", FieldType.FieldPage);footerParagraph.AppendText(" of ");footerParagraph.AppendField("number of pages", FieldType.FieldSectionPages);footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;if (i == 2)break;else{document.Sections[i + 1].PageSetup.RestartPageNumbering = true;document.Sections[i + 1].PageSetup.PageStartingNumber = 1;}}

第 5 步:保存并查看。

document.SaveToFile("result.docx", FileFormat.Docx);System.Diagnostics.Process.Start("result.docx");

结果截图:

通过 Spire.Doc 在 Word 文档的不同部分添加页码

欢迎下载|体验更多E-iceblue产品

获取更多信息请咨询在线客服  


标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年5月20日
下一篇 2022年5月20日

相关推荐

发表回复

登录后才能评论