欢迎来到科站长!

C#教程

当前位置: 主页 > 软件编程 > C#教程

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

时间:2024-11-29 10:20:52|栏目:C#教程|点击:

免费.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#查看/写入日志到Windows事件查看器的操作方法

栏    目:C#教程

下一篇:C# System.Linq提供类似SQL语法的高效查询操作

本文标题:C#删除Word文档中的段落的方法示例

本文地址:https://fushidao.cc/ruanjianbiancheng/1284.html

广告投放 | 联系我们 | 版权申明

申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:257218569 | 邮箱:257218569@qq.com

Copyright © 2018-2025 科站长 版权所有冀ICP备14023439号