1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Word控件Spire.Doc 【页面设置】教程(10) ;通过 Spire.Doc 在 Word 文档的不同部分添加页码

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

时间:2019-05-23 20:54:34

相关推荐

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

有时,开发人员需要在一个 Word 文档中为不同的部分添加页码,例如封面、目录和内容位于不同的部分。本文讨论如何通过Spire.Doc为不同部分添加页码。

Spire.Doc 最新下载/product/3368/download

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

以下是详细步骤:

第 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");

结果截图:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。