.NET Edit Word-Remove Word Paragraphs with C#, VB

Do you remember how miserable you write paper with an unfamiliar topic? At first, you write and then delete what you write and then rewrite what you deleted one second ago. Actually, no matter what we write, we will modify to make it more attractive after completing. When editing, some existing paragraphs will be removed and some new will be added. In this post, I will show a method about how to remove paragraphs in Word document with C#, VB .NET.

At beginning, we need some materials: word template with several paragraphs and a .NET Word component, Spire.Doc for .NET to realize this function very easily and quickly. (P.S The following screenshot presents part of Word template contents)

Next, follow steps to use the tool to delete one or more paragraphs in this Word template.

  1. Load this Word template. 
  2. Get paragraphs in specified section of document. Use ParagraphCollection.RemoveAt method provided by Spire.Doc for .NET and pass paragraph index overload to this method to remove specified paragraphs.
  3. Save the edited document with a new name.

Ensure that Spire.Doc has been downloaded and installed on your system and the dll has been added as reference in your project. Then, you can follow the code blow to realize the function to delete Word paragraphs.

CODE:

C#

using Spire.Doc;

using Spire.Doc.Documents;

 

namespace InsertWatermark

{

    class watermark

    {

        static void Main(string[] args)

        {

            //Load File

            Document document = new Document();

            document.LoadFromFile(@”E:\Work\Documents\WordDocuments\Healthcare Questionnaire.docx”);

 

            //Remove Paragraph 2

            document.Sections[0].Paragraphs.RemoveAt(1);

 

            //Remove Paragraph 3

            document.Sections[0].Paragraphs.RemoveAt(2);

 

            //Save and Launch

            document.SaveToFile(“RemovePara.docx”, FileFormat.Docx);

            System.Diagnostics.Process.Start(“RemovePara.docx”);

        }

    }

}

 

Visual Basic

Imports Spire.Doc

Imports Spire.Doc.Documents

 

Namespace InsertWatermark

    Friend Class watermark

        Shared Sub Main(ByVal args() As String)

            ‘Load File

            Dim document As New Document()

            document.LoadFromFile(“E:\Work\Documents\WordDocuments\Healthcare Questionnaire.docx”)

 

            ‘Remove Paragraph 2

            document.Sections(0).Paragraphs.RemoveAt(1)

 

            ‘Remove Paragraph 3

            document.Sections(0).Paragraphs.RemoveAt(2)

 

            ‘Save and Launch

            document.SaveToFile(“RemovePara.docx”, FileFormat.Docx)

            System.Diagnostics.Process.Start(“RemovePara.docx”)

        End Sub

    End Class

End Namespace

After running, we can find that paragraph 2 and 3 has been removed as screenshot shown blow.

Download Spire.Doc for .NET Here

Download Word Template in Post Here

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s