当前位置: 代码网 > it编程>编程语言>Asp.net > C#删除Word文档中的段落的方法示例

C#删除Word文档中的段落的方法示例

2024年09月06日 Asp.net 我要评论
免费.net word 库 - free spire.doc for .net。该库支持实现创建、编辑、转换word文档等多种操作,可以直接在visual studio中通过nuget搜索 &ldqu

免费.net word 库 - free spire.doc for .net。该库支持实现创建、编辑、转换word文档等多种操作,可以直接在visual studio中通过nuget搜索 “freespire.doc”,然后点击“安装”将其引用到程序中。或者通过该链接下载产品包,解压后再手动将dll文件添加引用至程序。

c# 删除word中的指定段落

通过 section.paragraphs 属性获取 paragraphcollection 对象后,再用 removeat(int index) 方法可以实现删除指定索引处的段落。具体代码如下:

using spire.doc;
 
namespace removeparagraphs
{
    internal class program
    {
        static void main(string[] args)
        {
            //加载word文档
            document document = new document();
            document.loadfromfile("南极洲.docx");
 
            //获取第一节
            section section = document.sections[0];
 
            //删除第四段
            section.paragraphs.removeat(3);
 
            //保存文档
            document.savetofile("删除指定段落.docx", fileformat.docx2016);
        }
    }
}

c# 删除word中的所有段落

paragraphcollection 类的 clear() 方法可以直接删除指定section中所有段落,要删除文档每一节中的所有段落,可以通过循环实现。具体代码如下:

using spire.doc;
 
namespace removeallparagraphs
{
    internal class program
    {
        static void main(string[] args)
        {
            //加载word文档
            document document = new document();
            document.loadfromfile("南极洲.docx");
 
            //遍历所有节
            foreach (section section in document.sections)
            {
                //删除段落
                section.paragraphs.clear();
            }
 
            //保存文档
            document.savetofile("删除所有段落.docx", fileformat.docx2016);
        }
    }
}

c# 删除word中的空白段落

删除空白段落需要先遍历每一节中的所有段落并判断其中是否包含内容,如果为空白行则通过documentobjectcollection.remove() 方法将其删除。具体代码如下:

using spire.doc;
using spire.doc.documents;
using system;
 
namespace removeemptylines
{
    class program
    {
 
        static void main(string[] args)
        {
 
            //加载word文档
            document doc = new document(); 
            doc.loadfromfile("南极洲1.docx");
 
            //遍历所有段落
            foreach (section section in doc.sections)
            {
                for (int i = 0; i < section.body.childobjects.count; i++)
                {
                    if (section.body.childobjects[i].documentobjecttype == documentobjecttype.paragraph)
                    {
                        //判断当前段落是否为空白段落
                        if (string.isnullorempty((section.body.childobjects[i] as paragraph).text.trim()))
                        {
                            //删除空白段落
                            section.body.childobjects.remove(section.body.childobjects[i]);
                            i--;
                        }
                    }
 
                }
            }
 
            //保存文档
            doc.savetofile("删除空白行.docx", fileformat.docx2016);
 
        }
    }
}

以上就是c#删除word文档中的段落的方法示例的详细内容,更多关于c#删除word中的段落的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com