C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# Word添加页码

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

作者: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添加页码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文