Add, insert and remove paragraphs in Word document with Document SDK

Document SDK allows to create Word documents (DOC, DOCX) in C# and VB.NET. Samples below show how to create simple document with 3 paragraphs, add/remove or insert paragraphs into the document.

C#

using System;
using System.Collections.Generic;
using System.Text;
using Bytescout.Document;
 
namespace AddInsertRemoveParagraph
{
    class Program
    {
        static void Main(string[] args)
        {
            string outputName = "output.docx";
            string outputCopyName = "output-to-reopen.docx";
 
            using (Document doc = new Document(DocumentFormat.OpenXml))
            {
                // create simple document with 3 paragraphs
 
                // adds first paragraph
                Paragraph p = doc.AddParagraph();
                p.Text = "First";
 
                // inserts new paragraph before first paragraph
                p = doc.InsertParagraph(p);
                p.Text = "NO! I'm first!";
 
                p = doc.AddParagraph();
                p.Text = "Yeah, I'm the last one...";
 
                doc.Save(outputName);
 
                // make a copy to avoid access conflicts with Word
                doc.Save(outputCopyName);
 
                System.Diagnostics.Process.Start(outputName);
            }
 
            // now reopen the file and add/remove paragraphs
            using (Document reopened = new Document())
            {
                reopened.Open(outputCopyName);
                // removes "NO! I'm first!"
                reopened.RemoveParagraph(0);
 
                // removes last paragraph
                reopened.RemoveParagraph(1);
 
                Paragraph p = reopened.AddParagraph();
                p.Text = "Second";
 
                p = reopened.AddParagraph();
                p.Text = "Third";
 
                reopened.Save(outputCopyName);
                System.Diagnostics.Process.Start(outputCopyName);
            }
        }
    }
}

 

VB.NET

Imports Bytescout.Document
 
Module Module1
 
    Sub Main()
        Dim outputName As String = "output.docx"
        Dim outputCopyName As String = "output-to-reopen.docx"
 
        Using doc As New Document(DocumentFormat.OpenXml)
            ' create simple document with 3 paragraphs
 
            ' adds first paragraph
            Dim p As Paragraph = doc.AddParagraph()
            p.Text = "First"
 
            ' inserts new paragraph before first paragraph
            p = doc.InsertParagraph(p)
            p.Text = "NO! I'm first!"
 
            p = doc.AddParagraph()
            p.Text = "Yeah, I'm the last one..."
 
            doc.Save(outputName)
 
            ' make a copy to avoid access conflicts with Word
            doc.Save(outputCopyName)
 
            System.Diagnostics.Process.Start(outputName)
        End Using
 
        ' now reopen the file and add/remove paragraphs
        Using reopened As New Document()
            reopened.Open(outputCopyName)
            ' removes "NO! I'm first!"
            reopened.RemoveParagraph(0)
 
            ' removes last paragraph
            reopened.RemoveParagraph(1)
 
            Dim p As Paragraph = reopened.AddParagraph()
            p.Text = "Second"
 
            p = reopened.AddParagraph()
            p.Text = "Third"
 
            reopened.Save(outputCopyName)
            System.Diagnostics.Process.Start(outputCopyName)
        End Using
    End Sub
 
End Module
Filed in: Document SDK