Find and Replace in ElementCollection


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:   
            // https://sautinsoft.com/start-for-free/

            FindAndReplace();
        }
        /// <summary>
        /// Find an empty paragraphs in document, replace all tables into paragraphs.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/elementcollection-find-replace.php
        /// </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 }));
                }
            }
            dc.Save(result1);

            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}));
                }
            }
			dc.Save(result2);

            // 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 });
        }
    }
}

Download

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

Module Sample
    Sub Main()
        FindAndReplace()
    End Sub
    ''' Get your free 30-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' Find an empty paragraphs in document, replace all tables into paragraphs.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/elementcollection-find-replace.php
    ''' </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
        dc.Save(result1)

        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
        Loop
        dc.Save(result2)

        ' 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

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) 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.