外围网上金喜网站

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

翻译|使用教程|编辑:胡涛|2022-06-24 10:15:59.447|阅读 349 次

概述:本文讨论如何通过Spire.Doc为不同部分添加页码。欢迎查阅!

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

相关链接:

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

Spire.Doc for.NET 最新下载

这里将在 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产品

获取更多信息请咨询  ;技术交流Q群(767755948)


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ipmdipw.cn


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
相关产品
Spire.Doc for .NET

Spire.Doc for .NET 是一款专门对 Word 文档进行操作的 .NET 类库。

Spire.Office for .NET

专业的.NET Office套件,涵盖office文档创建、编辑、转换、管理和OCR内容识别等操作

Spire.XLS for .NET

Spire.XLS for .NET是专业.NET Excel组件,快速完成对Excel各类编程操作

Aspose.Cells for .NET

专业的电子表格控件,无需MS Excel也可满足一切Excel表格功能。

Aspose.Words for .NET

无需Microsoft Word也可在任何平台上满足Word文档的一切操作需求。

扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP