C#/VB.NET: Remove Paragraphs in a Word Document

When processing a Word document, you may need to remove some paragraphs. For example, after you copied contents from the Internet with a lot of redundant paragraphs to your document, you need to delete the extra paragraphs and keep only those that are useful. The deletion can be easily achieved by Spire.Doc for .NET by programming with no need for other software. This article will show you the detailed steps of removing paragraphs in a Word document using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Delete a Specific Paragraph in a Word Document

Spire.Doc for .NET provides a method RemoveAt() under ParagraphCollection to remove paragraphs.

The detailed steps of removing a specific paragraph are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the first section using Document.Section[] property.
  • Remove the 4th paragraph using Section.Paragraphs.RemoveAt() method.
  • Save the document using Document.SaveToFile() method.
  • C#
  • VB.NET
using System;
using Spire.Doc;

namespace RemoveParagraphs
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of Document class
            Document document = new Document();

            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //Get the first section
            Section section = document.Sections[0];

            //Remove the first paragraph in the section
            section.Paragraphs.RemoveAt(3);
            
            //Save the document
            document.SaveToFile("RemoveParagraphs.docx", FileFormat.Docx2013);
        }
    }
}

C#/VB.NET: Remove Paragraphs in a Word Document

Delete All Paragraphs in a Word Document

To remove all paragraphs, you can use the method Clear() under ParagraphCollection provided by Spire.Doc for .NET.

The detailed steps are as follows:

  • Create an object of Document class.
  • Load a Word Document using Document.LoadFromFile() method.
  • Loop through all sections, and remove all paragraphs in each section using Section.Paragraphs.Clear() method.
  • Save the document using Document.SaveToFile() method.
  • C#
  • VB.NET
using System;
using Spire.Doc;

namespace RemoveAllParagraphs
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of Document class
            Document document = new Document();

            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //Loop through all sections
            foreach (Section section in document.Sections)
            {
                //Remove all paragraphs in the section
                section.Paragraphs.Clear();
            }

            //Save the document
            document.SaveToFile("RemoveAllParagraphs.docx", FileFormat.Docx2013);
        }
    }
}

C#/VB.NET: Remove Paragraphs in a Word Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.