zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Word控件Spire.Doc 【页面设置】教程(5) 如何在 C# 中删除分页符

c#教程 如何 删除 控件 分页 word doc
2023-09-11 14:14:49 时间

在 Word 文档中,用户可以添加新的分页符或删除现有的分页符。此示例显示如何使用 Spire.Doc 从 word 文档中删除分页符。Spire.Doc支持从.docx、.doc、RTF等格式的word文档中去除分页符。

Spire.Doc for.NET 最新下载icon-default.png?t=M85Bhttps://www.evget.com/product/3368/download

首先确保Spire.Doc for .NET已正确安装,然后通过以下路径在下载的 Bin 文件夹中添加 Spire.Doc.dll 作为参考:“.. \Spire.Doc\Bin\NET4.0\ Spire.Doc .dll”。以下是如何在 C# 中删除分页符的详细信息。

//Create a new word document and load from the file.
Document document = new Document();
document.LoadFromFile("sample.docx");

// Traverse every paragraph of the first section of the document
for (int j = 0; j < document.Sections[0].Paragraphs.Count; j++)
{
Paragraph p = document.Sections[0].Paragraphs[j];

// Traverse every child object of a paragraph
for (int i = 0; i < p.ChildObjects.Count; i++)
{
DocumentObject obj = p.ChildObjects[i];

//Find the page break object
if (obj.DocumentObjectType == DocumentObjectType.Break)
{
Break b = obj as Break;

// Remove the page break object from paragraph
p.ChildObjects.Remove(b);

//save the document to file.
document.SaveToFile("result.docx");

请查看有效截图

完整代码

using Spire.Doc;
using Spire.Doc.Documents;
namespace RemovePageBreak
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile("sample.docx", FileFormat.Docx);
for (int j = 0; j < document.Sections[0].Paragraphs.Count; j++)
{
Paragraph p = document.Sections[0].Paragraphs[j];
for (int i = 0; i < p.ChildObjects.Count; i++)
{
DocumentObject obj = p.ChildObjects[i];
if (obj.DocumentObjectType == DocumentObjectType.Break)
{
Break b = obj as Break;
p.ChildObjects.Remove(b);
}
}
}
document.SaveToFile("result.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("result.docx");
}
}
}