Insert Element in ElementCollection using C# and .NET

  1. Add SautinSoft.Document from Nuget.
  2. Load a DOCX document.
  3. Insert a Run element into the each paragraph.
  4. Save the document as new.

This code example shows us how to insert a new text element at the start of each paragraph.

Insert Element

Complete code

using System;
using SautinSoft.Document;
using SautinSoft.Document.Tables;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/

            InsertParagraphCount();
        }
        /// <summary>
        /// Inserts a new Run (Text element) at the start of each paragraph.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/elementcollection-insert.php
        /// </remarks>
        static void InsertParagraphCount()
        {
            string filePath = @"..\..\..\example.docx";
            DocumentCore dc = DocumentCore.Load(filePath);
            int paragraphNum = 1;
            foreach (Element el in dc.Sections[0].GetChildElements(false))
            {
                if (el is Paragraph)
                {
                    // Insert a new Run into Paragraph.InlineCollection 'Inlines'.
                    // InlineCollection is descendant of the base abstract class ElementCollection.
                    (el as Paragraph).Inlines.Insert(0, new Run(dc, "Paragraph " + paragraphNum.ToString() + " - ", new CharacterFormat() { BackgroundColor = Color.Orange, FontColor = Color.White }));
                    paragraphNum++;
                }
            }
            dc.Save("Result.docx");

            // Show the result.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Result.docx") { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports SautinSoft.Document
Imports SautinSoft.Document.Tables

Module Sample
    Sub Main()
        InsertParagraphCount()
    End Sub
    ''' Get your free 30-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' Inserts a new Run (Text element) at the start of each paragraph.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/elementcollection-insert.php
    ''' </remarks>
    Sub InsertParagraphCount()
        Dim filePath As String = "..\..\..\example.docx"
        Dim dc As DocumentCore = DocumentCore.Load(filePath)
        Dim paragraphNum As Integer = 1
        For Each el As Element In dc.Sections(0).GetChildElements(False)
            If TypeOf el Is Paragraph Then
                ' Insert a new Run into Paragraph.InlineCollection 'Inlines'.
                ' InlineCollection is descendant of the base abstract class ElementCollection.
                TryCast(el, Paragraph).Inlines.Insert(0, New Run(dc, "Paragraph " & paragraphNum.ToString() & " - ", New CharacterFormat() With {
                        .BackgroundColor = Color.Orange,
                        .FontColor = Color.White
                    }))
                paragraphNum += 1
            End If
        Next el
        dc.Save("Result.docx")

        ' Show the result.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("Result.docx") With {.UseShellExecute = True})
    End Sub
End Module

Download

If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page).