C#代码实现为Word文档指定章节添加页码

 更新时间:2026年07月17日 08:27:40   作者:2501_93070778  
为 Word 文档添加页码可以提升文档的结构性和可读性,本文将介绍如何使用 C# 在 Word 文档中添加页码,并通过示例代码演示具体实现方法,有需要的小伙伴可以了解下

为 Word 文档添加页码可以提升文档的结构性和可读性。页码能够帮助读者快速定位内容,也方便浏览较长的文档。无论是报告、论文还是其他类型的文档,添加页码都是一种简单有效的方式,可以改善文档的整体组织和阅读体验。

本文将介绍如何使用 C# 在 Word 文档中添加页码,并通过示例代码演示具体实现方法。

安装相关组件

开始之前,需要先将项目所需的 DLL 文件添加到 .NET 项目中。你可以下载对应文件,或通过 NuGet 包管理器进行安装。

PM> Install-Package Spire.Doc

使用 C# 为 Word 文档添加页码

在 Word 文档中动态添加页码时,可以使用页码相关字段,例如 FieldPageFieldNumPagesFieldSectionPages。这些字段可以分别表示当前页码、总页数以及当前章节的页数,帮助实现自动显示和更新页码。

这些字段通常添加到文档的页眉或页脚中,可以通过 Paragraph.AppendField() 方法将其插入到指定位置。

以下步骤介绍如何在页脚中添加 FieldPageFieldNumPages 字段,并在文档中显示类似 “X/Y” 的页码格式。

  1. 创建一个 Document 对象。
  2. 从指定路径加载 Word 文档。
  3. 使用 Document.Sections[index] 属性获取第一个节。
  4. 使用 Section.HeadersFooters.Footer 属性获取第一个节的页脚。
  5. 使用 HeaderFooter.AddParagraph() 方法向页脚添加段落。
  6. 使用 Paragraph.AppendField()Paragraph.AppendText() 方法向段落中插入 FieldPage 字段、FieldNumPages 字段以及 “/” 符号。
  7. 将文档保存为新的 Word 文件。

完整示例代码如下:

using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace AddPageNumbersToDocument
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建一个 Document 对象
            Document document = new Document();

            // 加载 Word 文件
            document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Privacy Policy.docx");

            // 获取第一个节
            Section section = document.Sections[0];

            // 获取该节的页脚
            HeaderFooter footer = section.HeadersFooters.Footer;

            // 在页脚中添加“当前页码 / 总页数”
            Paragraph footerParagraph = footer.AddParagraph();
            footerParagraph.AppendField("page number", FieldType.FieldPage);
            footerParagraph.AppendText(" / ");
            footerParagraph.AppendField("page count", FieldType.FieldNumPages);
            footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center;

            // 设置页码格式
            ParagraphStyle style = new ParagraphStyle(document);
            style.CharacterFormat.Bold = true;
            style.CharacterFormat.FontName = "Times New Roman";
            style.CharacterFormat.FontSize = 18;
            style.CharacterFormat.TextColor = Color.Red;
            document.Styles.Add(style);
            footerParagraph.ApplyStyle(style);

            // 保存文档
            document.SaveToFile("AddPageNumbersToDocument.docx");

            // 释放资源
            document.Dispose();
        }
    }
}

使用 C# 为 Word 文档中的指定章节添加页码

默认情况下,Word 文档中每个节的页脚页码会自动与前一个节关联,以确保页码连续显示。但是,如果只希望为某个指定章节添加页码,则需要取消后续章节与前一章节的链接,并删除后续章节页脚中的内容。

以下步骤介绍如何使用 C# 在 Word 文档的指定章节中添加页码。

  1. 创建一个 Document 对象。
  2. 从指定路径加载 Word 文档。
  3. 使用 Document.Sections[index] 属性获取指定章节。
  4. 使用 Section.HeadersFooters.Footer 属性获取该章节的页脚。
  5. Section.PageSetup.RestartPageNumbering 属性设置为 true,并将 Section.PageSetup.PageStartingNumber 属性设置为 1,使页码从 1 开始重新编号。
  6. 使用 Paragraph.AppendField()Paragraph.AppendText() 方法,在页脚中插入 FieldPage 字段、FieldSectionPages 字段以及 “/” 符号。
  7. HeadersFooters.Footer.LinkToPrevious 属性设置为 false,取消“链接到前一节”设置。
  8. 删除后续章节页脚中的内容。
  9. 将文档保存为新的 Word 文件。

完整示例代码如下:

using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace AddPageNumbersToSpecificSection
{ 
    class Program
    {
        static void Main(string[] args)
        {
            // 创建一个 Document 对象
            Document document = new Document();

            // 加载 Word 文件
            document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Privacy Policy.docx");

            // 获取指定章节
            int sectionIndex = 1;
            Section section = document.Sections[sectionIndex];

            // 将页码重新从 1 开始编号
            section.PageSetup.RestartPageNumbering = true;
            section.PageSetup.PageStartingNumber = 1;

            // 获取该章节的页脚
            HeaderFooter footer = section.HeadersFooters.Footer;

            // 在页脚中添加“当前页码 / 章节页数”
            Paragraph footerParagraph = footer.AddParagraph();
            footerParagraph.AppendField("page number", FieldType.FieldPage);
            footerParagraph.AppendText(" / ");
            footerParagraph.AppendField("page count", FieldType.FieldSectionPages);
            footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center;

            // 设置页码格式
            ParagraphStyle style = new ParagraphStyle(document);
            style.CharacterFormat.Bold = true;
            style.CharacterFormat.FontName = "Times New Roman";
            style.CharacterFormat.FontSize = 18;
            style.CharacterFormat.TextColor = Color.Red;
            document.Styles.Add(style);
            footerParagraph.ApplyStyle(style);

            // 取消后续章节中的“链接到前一节”设置
            document.Sections[sectionIndex + 1].HeadersFooters.Footer.LinkToPrevious = false;

            // 删除后续章节页脚中的内容
            for (int i = sectionIndex + 1; i < document.Sections.Count; i++)
            {
                document.Sections[i].HeadersFooters.Footer.ChildObjects.Clear();
                document.Sections[i].HeadersFooters.Footer.AddParagraph();
            }

            // 保存文档
            document.SaveToFile("AddPageNumbersToSection.docx");

            // 释放资源
            document.Dispose();
        }
    }
}

使用 C# 为 Word 文档的不同章节添加不同页码

如果希望文档中的不同章节显示独立的页码,需要分别获取每个章节,并为其单独添加页码,同时在每个章节开始处将页码重新从 1 开始编号。

具体步骤如下:

  1. 创建一个 Document 对象。
  2. 从指定路径加载 Word 文档。
  3. 遍历文档中的各个章节。
  4. 使用 Document.Sections[index] 属性获取指定章节。
  5. 使用 Section.HeadersFooters.Footer 属性获取该章节的页脚。
  6. Section.PageSetup.RestartPageNumbering 属性设置为 true,并将 Section.PageSetup.PageStartingNumber 属性设置为 1,使页码从 1 开始重新编号。
  7. 使用 Paragraph.AppendField()Paragraph.AppendText() 方法,在页脚中插入 FieldPage 字段、FieldSectionPages 字段以及 “/” 符号。
  8. 将文档保存为新的 Word 文件。

完整示例代码如下:

using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace AddDifferentPageNumbersToDifferentSections
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建一个 Document 对象
            Document document = new Document();

            // 加载 Word 文件
            document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Privacy Policy.docx");

            // 遍历文档中的各个章节
            for (int i = 0; i < document.Sections.Count; i++)
            {
                // 获取指定章节
                Section section = document.Sections[i];

                // 将页码重新从 1 开始编号
                section.PageSetup.RestartPageNumbering = true;
                section.PageSetup.PageStartingNumber = 1;

                // 获取该章节的页脚
                HeaderFooter footer = section.HeadersFooters.Footer;

                // 在页脚中添加“当前页码 / 章节页数”
                Paragraph footerParagraph = footer.AddParagraph();
                footerParagraph.AppendField("page number", FieldType.FieldPage);
                footerParagraph.AppendText(" / ");
                footerParagraph.AppendField("page count", FieldType.FieldSectionPages);
                footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center;

                // 设置页码格式
                ParagraphStyle style = new ParagraphStyle(document);
                style.CharacterFormat.Bold = true;
                style.CharacterFormat.FontName = "Times New Roman";
                style.CharacterFormat.FontSize = 18;
                style.CharacterFormat.TextColor = Color.Red;
                document.Styles.Add(style);
                footerParagraph.ApplyStyle(style);
            }

            // 保存文档
            document.SaveToFile("AddDifferentPageNumbersToSections.docx");

            // 释放资源
            document.Dispose();
        }
    }
}

总结

本文介绍了如何使用 C# 为 Word 文档添加页码,包括为整个文档、指定章节以及不同章节分别设置页码。通过配置页脚字段,可以实现当前页码、总页数和章节页数的自动显示,满足不同文档结构的需求。

如果需要移除生成文档中的试用提示信息或解除功能限制,可以申请 30 天临时许可证(Temporary License),以完整体验相关功能。

到此这篇关于C#代码实现为Word文档指定章节添加页码的文章就介绍到这了,更多相关C# Word添加页码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C#访问应用程序配置文件的方法

    C#访问应用程序配置文件的方法

    C#访问应用程序配置文件的方法,需要的朋友可以参考一下
    2013-03-03
  • C#实现系统信息监控与获取功能

    C#实现系统信息监控与获取功能

    在 C# 开发的众多应用场景中,获取系统信息以及监控用户操作有着广泛的用途,比如在系统性能优化工具中,需要实时读取 CPU、GPU 资源信息,本文将详细介绍如何使用 C# 来实现这些功能,助力大家在开发中更好地与系统底层进行交互,需要的朋友可以参考下
    2025-01-01
  • C#中使用DataContractSerializer类实现深拷贝操作示例

    C#中使用DataContractSerializer类实现深拷贝操作示例

    这篇文章主要介绍了C#中使用DataContractSerializer类实现深拷贝操作示例,本文给出了实现深拷贝方法、测试深拷贝方法例子、DataContractSerializer类实现深拷贝的原理等内容,需要的朋友可以参考下
    2015-06-06
  • C#面向对象设计原则之开闭原则

    C#面向对象设计原则之开闭原则

    这篇文章介绍了C#面向对象设计原则之开闭原则,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • unity avprovideo插件的使用详解

    unity avprovideo插件的使用详解

    这篇文章主要介绍了unity avprovideo插件的使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • Unity通过脚本创建网格Mesh的方法

    Unity通过脚本创建网格Mesh的方法

    Unity中的网格作为组件不能脱离物体单独存在,通过新建脚本来实现相关操作,本文重点给大家介绍Unity通过脚本创建网格Mesh的方法,感兴趣的朋友一起看看吧
    2022-04-04
  • C#中弱引用使用小结

    C#中弱引用使用小结

    弱引用是一种不会阻止垃圾回收的对象引用,适用于缓存和事件监听等场景,但需注意其不确定性和潜在的性能影响,下面就来详细的介绍C#中弱引用使用小结,感兴趣的可以了解一下
    2025-12-12
  • C#读取字节数组某个位的值的具体实现方法

    C#读取字节数组某个位的值的具体实现方法

    字节数组中提取指定偏移量字节的指定位的值(对应 PLC 中 1 个字节对应 8 个布尔值的场景,如 I0.0~I0.7 对应 1 个字节的 bit0~bit7),核心是通过位运算实现,下面详细讲解具体方法和实现,需要的朋友可以参考下
    2026-01-01
  • C#实现六大设计原则之依赖倒置原则

    C#实现六大设计原则之依赖倒置原则

    这篇文章介绍了C#实现六大设计原则之依赖倒置原则的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-02-02
  • C# winform 请求http的实现(get,post)

    C# winform 请求http的实现(get,post)

    本文主要介绍了C# winform 请求http的实现(get,post),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06

最新评论