在 Word 文档中添加、插入和删除页面对于管理和呈现内容至关重要。通过添加新页面,可以使文档结构更清晰,内容更有条理。插入页面可以在需要时分隔不同部分,突出重点或使内容更易阅读。删除页面则可以帮助简化文档,去除不必要的信息或错误内容。本文将介绍如何使用 Spire.Doc for .NET 在 C# 项目中添加、插入和删除 Word 文档中的页面。
安装 Spire.Doc for .NET
首先,您需要将 Spire.Doc for.NET 包含的 DLL 文件作为引用添加到您的 .NET 项目中。DLL 文件可以从此链接下载,也可以通过 NuGet 安装。
PM> Install-Package Spire.Doc
C# 在 Word 文档中添加页面
在 Word 文档末尾添加新页面的步骤是首先获取最后一个章节,然后在该章节的最后一个段落末尾插入一个分页符,这样做可以确保后续添加的内容会出现在新的页面上。以下是详细步骤:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载示例 Word 文档。
- 使用 Document.LastSection.Body 获取文档的最后一个节的正文部分。
- 通过调用 Paragraph.AppendBreak(BreakType.PageBreak) 方法添加一个分页符。
- 创建一个新的段落样式 ParagraphStyle 对象。
- 使用 Document.Styles.Add() 方法将新段落样式添加到文档样式集合中。
- 创建一个新的段落 Paragraph 对象并设置文本内容。
- 使用 Paragraph.ApplyStyle(ParagraphStyle.Name) 方法应用之前创建的段落样式到新段落。
- 使用 Body.ChildObjects.Add(Paragraph) 方法添加新段落到文档中。
- 使用 Document.SaveToFile() 方法保存结果文档。
- C#
// 创建一个新的文档对象
Document document = new Document();
// 从文件加载示例文档
document.LoadFromFile("示例.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 = "微软雅黑";
paragraphStyle.CharacterFormat.FontSize = 12;
// 将段落样式添加到文档的样式集合中
document.Styles.Add(paragraphStyle);
// 创建新的段落并设置文本内容
Paragraph paragraph = new Paragraph(document);
paragraph.AppendText("非常感谢您使用我们的 Spire.Doc for .NET 产品。试用版除了会在生成的结果文档中添加红色水印,而且仅支持转换前 10 页到其它格式。当您购买并应用 license 后,会成功移除这些水印信息并解除功能限制。");
// 应用段落样式
paragraph.ApplyStyle(paragraphStyle.Name);
// 将段落添加到正文的内容集合中
body.ChildObjects.Add(paragraph);
// 创建另一个新的段落并设置文本内容
paragraph = new Paragraph(document);
paragraph.AppendText("为了更完整的试用我们的产品,我们免费提供一个月临时 license 给我们的每一位客户。请发送邮件到 sales @e-iceblue.com 并在邮件内备注下面信息,我们会在一个工作日内将license发送给您。");
// 应用段落样式
paragraph.ApplyStyle(paragraphStyle.Name);
// 将段落添加到正文的内容集合中
body.ChildObjects.Add(paragraph);
// 保存文档到指定路径
document.SaveToFile("添加一个页面.docx", FileFormat.Docx);
// 关闭文档
document.Close();
// 释放文档对象的资源
document.Dispose();
C# 在 Word 文档中插入页面
在插入新页面之前,首先需要确定指定页面内容在节中的结束位置索引,然后在该位置之后逐个添加新页面的内容到文档中。最后,为了将内容与后续页面分隔开来,添加一个分页符是必要的。详细步骤如下:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载示例 Word 文档。
- 使用 Document.LastSection.Body 获取文档的最后一个节的正文部分。
- 通过调用 Paragraph.AppendBreak(BreakType.PageBreak) 方法添加一个分页符。
- 创建一个新的段落样式 ParagraphStyle 对象。
- 使用 Document.Styles.Add() 方法将新段落样式添加到文档样式集合中。
- 创建一个新的段落 Paragraph 对象并设置文本内容。
- 使用 Paragraph.ApplyStyle(ParagraphStyle.Name) 方法应用之前创建的段落样式到新段落。
- 使用 Body.ChildObjects.Add(Paragraph) 方法添加新段落到文档中。
- 使用 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("示例.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 = "微软雅黑";
paragraphStyle.CharacterFormat.FontSize = 12;
// 将段落样式添加到文档的样式集合中
document.Styles.Add(paragraphStyle);
// 创建新的段落并设置文本内容
Paragraph paragraph = new Paragraph(document);
paragraph.AppendText("非常感谢您使用我们的 Spire.Doc for .NET 产品。试用版除了会在生成的结果文档中添加红色水印,而且仅支持转换前 10 页到其它格式。当您购买并应用 license 后,会成功移除这些水印信息并解除功能限制。");
// 应用段落样式
paragraph.ApplyStyle(paragraphStyle.Name);
// 在指定位置插入段落
body.ChildObjects.Insert(endIndex + 1, paragraph);
// 创建另一个新的段落
paragraph = new Paragraph(document);
paragraph.AppendText("为了更完整的试用我们的产品,我们免费提供一个月临时 license 给我们的每一位客户。请发送邮件到 sales @e-iceblue.com 并在邮件内备注下面信息,我们会在一个工作日内将license发送给您。");
// 应用段落样式
paragraph.ApplyStyle(paragraphStyle.Name);
// 添加分页符
paragraph.AppendBreak(BreakType.PageBreak);
// 在指定位置插入段落
body.ChildObjects.Insert(endIndex + 2, paragraph);
// 保存文档到指定路径
document.SaveToFile("在指定的页面后插入新的一页.docx", Spire.Doc.FileFormat.Docx);
// 关闭并释放原文档
document.Close();
document.Dispose();
}
}
}
C# 从 Word 文档中删除页面
要删除一个页面的内容,首先需要确定该页面的起始元素和结束元素在文档中的索引位置,然后可以使用循环程序逐个移除这些元素,从而删除整个页面的内容。详细步骤如下:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载示例 Word 文档。
- 创建一个 FixedLayoutDocument 对象。
- 获取文档中的第一个页面 FixedLayoutPage 对象。
- 使用 FixedLayoutPage.Section 属性获取页面所在的节。
- 获取页面第一个段落在节中的索引位置。
- 获取页面最后一个段落在节中的索引位置。
- 使用一个 for 循环逐个移除页面的内容。
- 使用 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("示例.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();
}
}
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。