Find and Replace in ElementCollection

  1. Add SautinSoft.Document from Nuget.
  2. Load a DOCX document.
  3. Insert the text "" in a paragraph with 0 elements.
  4. Replace each table by text "HERE WAS THE TABLE" .

This code example shows us how to find an empty paragraphs in document, replace all tables into paragraphs.

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:   

        /// <summary>
        /// Find an empty paragraphs in document, replace all tables into paragraphs.
        /// </summary>
        /// <remarks>
        /// Details:
        /// </remarks>
        static void FindAndReplace()
            string filePath = @"..\..\..\example.docx";
			string result1 = @"ResultEmptyParagraphs.docx";
			string result2 = @"ResultReplacedTables.docx";

            DocumentCore dc = DocumentCore.Load(filePath);
            foreach (Paragraph par in dc.Sections[0].GetChildElements(false,ElementType.Paragraph))
                if ( par.Inlines.Count == 0)
                    par.Inlines.Add(new Run(dc, "<empty paragraph>", new CharacterFormat() { BackgroundColor = Color.Black, FontColor = Color.White }));

            for (int i = 0; i < dc.Sections[0].Blocks.Count; i++)
                if (dc.Sections[0].Blocks[i] is Table)
                    dc.Sections[0].Blocks[i] = new Paragraph(dc,new Run(dc, "HERE WAS THE TABLE", new CharacterFormat() { BackgroundColor = Color.Yellow}));

            // Show the result.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(result1) { UseShellExecute = true });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(result2) { UseShellExecute = true });


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

Module Sample
    Sub Main()
    End Sub
    ''' Get your free 30-day key here:   
    ''' <summary>
    ''' Find an empty paragraphs in document, replace all tables into paragraphs.
    ''' </summary>
    ''' <remarks>
    ''' Details:
    ''' </remarks>
    Sub FindAndReplace()
        Dim filePath As String = "..\..\..\example.docx"
        Dim result1 As String = "ResultEmptyParagraphs.docx"
        Dim result2 As String = "ResultReplacedTables.docx"

        Dim dc As DocumentCore = DocumentCore.Load(filePath)
        For Each par As Paragraph In dc.Sections(0).GetChildElements(False, ElementType.Paragraph)
            If par.Inlines.Count = 0 Then
                par.Inlines.Add(New Run(dc, "<empty paragraph>", New CharacterFormat() With {
                        .BackgroundColor = Color.Black,
                        .FontColor = Color.White
            End If
        Next par

        Dim i As Integer = 0
        Do While i < dc.Sections(0).Blocks.Count
            If TypeOf dc.Sections(0).Blocks(i) Is Table Then
                dc.Sections(0).Blocks(i) = New Paragraph(dc, New Run(dc, "HERE WAS THE TABLE", New CharacterFormat() With {.BackgroundColor = Color.Yellow}))
            End If
            i += 1

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


If you need a new code example or have a question: email us at or ask at Online Chat (right-bottom corner of this page) or use the Form below:

Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.