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

在 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");
}
}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值