首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >使用C#代码在 Word 文档中添加、插入或删除页面

使用C#代码在 Word 文档中添加、插入或删除页面

原创
作者头像
用户12401097
发布于 2026-10-15 17:51:12
发布于 2026-10-15 17:51:12
200
举报

在 Word 文档中添加、插入或删除页面,有助于灵活调整文档结构和管理内容。添加或插入页面可以为新内容预留空间,使信息组织更加清晰;删除页面则可以移除多余或错误的内容,让文档更加简洁。本文将介绍如何在 C# 项目中通过编程方式添加、插入或删除 Word 文档中的页面。

环境配置

在开始之前,需要配置 C# 项目的开发环境,并引入用于处理 Word 文档的相关组件。可以根据项目需求,通过添加 DLL 文件引用或使用 NuGet 包管理器完成安装。

代码语言:C#
复制
PM> Install-Package Spire.Doc

使用 C# 在 Word 文档中添加页面

要在 Word 文档末尾添加新页面,可以先获取文档的最后一个节,再在该节最后一个段落的末尾插入分页符,使后续添加的内容从新页面开始。具体步骤如下:

  1. 创建一个 Document 对象。
  2. 使用 Document.LoadFromFile() 方法加载 Word 文档。
  3. 通过 Document.LastSection.Body 获取文档最后一个节的正文内容。
  4. 调用 Paragraph.AppendBreak(BreakType.PageBreak) 方法插入分页符。
  5. 创建一个新的 ParagraphStyle 对象。
  6. 使用 Document.Styles.Add() 方法将段落样式添加到文档的样式集合中。
  7. 创建一个新的 Paragraph 对象,并设置段落文本。
  8. 调用 Paragraph.ApplyStyle(ParagraphStyle.Name) 方法,将创建的样式应用于新段落。
  9. 使用 Body.ChildObjects.Add(Paragraph) 方法将新段落添加到文档正文中。
  10. 最后,调用 Document.SaveToFile() 方法保存修改后的文档。

完整示例代码如下:

代码语言:C#
复制
// 创建一个新的文档对象
Document document = new Document();

// 加载文档
document.LoadFromFile("Sample.docx");

// 获取文档最后一个节的正文
Body body = document.LastSection.Body;

// 在正文的最后一个段落后插入分页符
body.LastParagraph.AppendBreak(BreakType.PageBreak);

// 创建一个新的段落样式
ParagraphStyle paragraphStyle = new ParagraphStyle(document);
paragraphStyle.Name = "CustomParagraphStyle1";
paragraphStyle.ParagraphFormat.LineSpacing = 12;
paragraphStyle.ParagraphFormat.AfterSpacing = 8;
paragraphStyle.CharacterFormat.FontName = "Microsoft YaHei";
paragraphStyle.CharacterFormat.FontSize = 12;

// 将段落样式添加到文档的样式集合中
document.Styles.Add(paragraphStyle);

// 创建一个新段落并设置文本内容
Paragraph paragraph = new Paragraph(document);
paragraph.AppendText("感谢您使用我们的 Spire.Doc for .NET 产品。试用版会在生成的文档中添加红色水印,并且仅支持将前 10 页转换为其他格式。购买并应用许可证后,这些水印将被移除,功能限制也将解除。");

// 应用段落样式
paragraph.ApplyStyle(paragraphStyle.Name);

// 将段落添加到正文的内容集合中
body.ChildObjects.Add(paragraph);

// 再次创建一个新段落并设置文本内容
paragraph = new Paragraph(document);
paragraph.AppendText("为了让每位客户更全面地体验产品,我们免费提供为期一个月的临时许可证。请发送电子邮件至 sales@e-iceblue.com,我们将在一个工作日内向您发送许可证。");

// 应用段落样式
paragraph.ApplyStyle(paragraphStyle.Name);

// 将段落添加到正文的内容集合中
body.ChildObjects.Add(paragraph);

// 将文档保存到指定路径
document.SaveToFile("Add a Page.docx", FileFormat.Docx);

// 关闭文档
document.Close();

// 释放文档对象占用的资源
document.Dispose();

使用 C# 在 Word 文档中插入页面

在 Word 文档的指定位置插入新页面时,需要先确定目标页面最后一段内容在所属节中的索引位置,然后在该位置之后依次添加新内容。最后,插入分页符,将新增内容与后续页面的内容分隔开。具体步骤如下:

  1. 创建一个 Document 对象。
  2. 使用 Document.LoadFromFile() 方法加载 Word 文档。
  3. 创建一个 FixedLayoutDocument 对象。
  4. 获取文档中目标页面对应的 FixedLayoutPage 对象。
  5. 确定目标页面最后一个段落在所属节中的索引位置。
  6. 创建一个新的 ParagraphStyle 对象。
  7. 使用 Document.Styles.Add() 方法将段落样式添加到文档的样式集合中。
  8. 创建一个新的 Paragraph 对象,并设置文本内容。
  9. 使用 Paragraph.ApplyStyle(ParagraphStyle.Name) 方法,将创建的段落样式应用于新段落。
  10. 调用 Body.ChildObjects.Insert(index, Paragraph) 方法,在指定索引位置插入新段落。
  11. 再创建一个新的 Paragraph 对象并设置文本内容,调用 Paragraph.AppendBreak(BreakType.PageBreak) 方法插入分页符,应用之前创建的段落样式,然后将该段落插入文档。
  12. 最后,使用 Document.SaveToFile() 方法保存修改后的文档。

完整示例代码如下:

代码语言:C#
复制
using Spire.Doc;
using Spire.Doc.Pages;
using Spire.Doc.Documents;

namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建一个新的文档对象
            Document document = new Document();

            // 从文件加载示例文档
            document.LoadFromFile("Sample.docx");

            // 创建一个固定版式文档对象
            FixedLayoutDocument layoutDoc = new FixedLayoutDocument(document);

            // 获取第一页
            FixedLayoutPage page = layoutDoc.Pages[0];

            // 获取文档的正文
            Body body = page.Section.Body;

            // 获取当前页面的最后一个段落
            Paragraph paragraphEnd = page.Columns[0].Lines[page.Columns[0].Lines.Count - 1].Paragraph;

            // 初始化结束索引
            int endIndex = 0;
            if (paragraphEnd != null)
            {
                // 获取最后一个段落的索引
                endIndex = body.ChildObjects.IndexOf(paragraphEnd);
            }

            // 创建一个新的段落样式
            ParagraphStyle paragraphStyle = new ParagraphStyle(document);
            paragraphStyle.Name = "CustomParagraphStyle1";
            paragraphStyle.ParagraphFormat.LineSpacing = 12;
            paragraphStyle.ParagraphFormat.AfterSpacing = 8;
            paragraphStyle.CharacterFormat.FontName = "Microsoft YaHei";
            paragraphStyle.CharacterFormat.FontSize = 12;

            // 将段落样式添加到文档的样式集合中
            document.Styles.Add(paragraphStyle);

            // 创建一个新段落并设置文本内容
            Paragraph paragraph = new Paragraph(document);
            paragraph.AppendText("感谢您使用我们的 Spire.Doc for .NET 产品。试用版会在生成的文档中添加红色水印,并且仅支持将前 10 页转换为其他格式。购买并应用许可证后,这些水印将被移除,功能限制也将解除。");

            // 应用段落样式
            paragraph.ApplyStyle(paragraphStyle.Name);

            // 在指定位置插入段落
            body.ChildObjects.Insert(endIndex + 1, paragraph);

            // 再创建一个新段落
            paragraph = new Paragraph(document);
            paragraph.AppendText("为了让每位客户更全面地体验产品,我们免费提供为期一个月的临时许可证。请发送电子邮件至 sales@e-iceblue.com,我们将在一个工作日内向您发送许可证。");

            // 应用段落样式
            paragraph.ApplyStyle(paragraphStyle.Name);

            // 插入分页符
            paragraph.AppendBreak(BreakType.PageBreak);

            // 在指定位置插入段落
            body.ChildObjects.Insert(endIndex + 2, paragraph);

            // 将文档保存到指定路径
            document.SaveToFile("Insert a Page.docx", Spire.Doc.FileFormat.Docx);

            // 关闭文档并释放资源
            document.Close();
            document.Dispose();
        }
    }
}

使用 C# 删除 Word 文档中的页面

要删除 Word 文档中的指定页面,可以先确定该页面首个和最后一个段落在文档中的索引位置,再通过循环逐一移除这些内容。具体步骤如下:

  1. 创建一个 Document 对象。
  2. 使用 Document.LoadFromFile() 方法加载 Word 文档。
  3. 创建一个 FixedLayoutDocument 对象。
  4. 获取文档中第一页对应的 FixedLayoutPage 对象。
  5. 通过 FixedLayoutPage.Section 属性获取该页面所属的节。
  6. 确定该页面第一个段落在所属节中的索引位置。
  7. 确定该页面最后一个段落在所属节中的索引位置。
  8. 使用 for 循环逐一删除该页面对应的内容。
  9. 最后,使用 Document.SaveToFile() 方法保存修改后的文档。

完整示例代码如下:

代码语言:C#
复制
using Spire.Doc;
using Spire.Doc.Pages;
using Spire.Doc.Documents;

namespace SpireDocDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建一个新的文档对象
            Document document = new Document();

            // 从文件加载示例文档
            document.LoadFromFile("Sample.docx");

            // 创建一个固定版式文档对象
            FixedLayoutDocument layoutDoc = new FixedLayoutDocument(document);

            // 获取第二页
            FixedLayoutPage page = layoutDoc.Pages[1];

            // 获取该页面所属的节
            Section section = page.Section;

            // 获取第二页上的第一个段落
            Paragraph paragraphStart = page.Columns[0].Lines[0].Paragraph;
            int startIndex = 0;
            if (paragraphStart != null)
            {
                // 获取起始段落的索引
                startIndex = section.Body.ChildObjects.IndexOf(paragraphStart);
            }

            // 获取第二页上的最后一个段落
            Paragraph paragraphEnd = page.Columns[0].Lines[page.Columns[0].Lines.Count - 1].Paragraph;

            int endIndex = 0;
            if (paragraphEnd != null)
            {
                // 获取结束段落的索引
                endIndex = section.Body.ChildObjects.IndexOf(paragraphEnd);
            }

            // 删除指定范围内的所有内容
            for (int i = 0; i <= (endIndex - startIndex); i++)
            {
                section.Body.ChildObjects.RemoveAt(startIndex);
            }

            // 将文档保存到指定路径
            document.SaveToFile("删除页面.docx", Spire.Doc.FileFormat.Docx);

            // 关闭并释放原始文档
            document.Close();
            document.Dispose();
        }
    }
}

总结

本文介绍了如何使用 C# 删除 Word 文档中的指定页面。通过创建固定版式文档对象,可以获取目标页面所属的节、页面起始段落和结束段落,并根据段落在文档中的索引位置,逐一删除指定范围内的内容。最后,将修改后的文档保存到指定路径,并释放相关资源。

这种方法适用于通过编程方式删除 Word 文档中的指定页面,帮助简化文档内容并提高文档处理效率。需要注意的是,Word 页面由内容排版动态决定,删除页面对应的段落内容后,文档的分页布局也可能随之变化。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

目录
  • 环境配置
  • 使用 C# 在 Word 文档中添加页面
  • 使用 C# 在 Word 文档中插入页面
  • 使用 C# 删除 Word 文档中的页面
  • 总结
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档