Click or drag to resize

ContentRangeReplace Method (String, CharacterFormat)

Replaces the current ContentRange's content with the specified text with specific formatting.

Namespace:  SautinSoft.Document
Assembly:  SautinSoft.Document (in SautinSoft.Document.dll) Version: 4.2.1.24 (4.2.1.24)
Syntax
public ContentRange Replace(
	string text,
	CharacterFormat format
)

Parameters

text
Type: SystemString
The text which should replace the current ContentRange's content.
format
Type: SautinSoft.DocumentCharacterFormat
The formatting of the text.

Return Value

Type: ContentRange
A ContentRange instance which contains newly inserted content.
Examples
Find and replace a text using ContentRange in C#
using System;
using System.IO;
using SautinSoft.Document;
using SautinSoft.Document.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            FindAndReplace();
        }
        /// <summary>
        /// Find and replace a specific text in an existing DOCX document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/examples/find-and-replace-text-in-docx-document-net-csharp-vb.php
        /// </remarks>
        public static void FindAndReplace()
        {
            // Path to the loadable document.
            string loadPath = @"..\..\example.docx";

            DocumentCore dc = DocumentCore.Load(loadPath);

            // Find "Bean" and Replace everywhere on "Joker"
            Regex regex = new Regex(@"Bean", RegexOptions.IgnoreCase);

            // Start:

            // Please note, Reverse() makes sure that action Replace() doesn't affect to Find().
            foreach (ContentRange item in dc.Content.Find(regex).Reverse())
            {
                item.Replace("Joker", new CharacterFormat() { BackgroundColor = Color.Yellow, FontName = "Arial", Size = 16.0 });
            }

            // End:

            // The code above finds and replaces the content in the whole document.
            // Let us say, you want to replace a text inside shape blocks only:

            // 1. Comment the code above from the line "Start" to the "End".
            // 2. Uncomment this code:
            //foreach (Shape shp in dc.GetChildElements(true, ElementType.Shape).Reverse())
            //{
            //    foreach (ContentRange item in shp.Content.Find(regex).Reverse())
            //    {
            //        item.Replace("Joker", new CharacterFormat() { BackgroundColor = Color.Yellow, FontName = "Arial", Size = 16.0 });
            //    }
            //}

            // Save the document as DOCX format.
            string savePath = Path.ChangeExtension(loadPath, ".replaced.docx");
            dc.Save(savePath, SaveOptions.DocxDefault);

            // Open the original and result documents for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(loadPath) { UseShellExecute = true });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(savePath) { UseShellExecute = true });
        }
    }
}
Find and replace a text using ContentRange in VB.Net
Imports System
Imports System.IO
Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Text.RegularExpressions

Namespace Sample
    Friend Class Sample
        Shared Sub Main(ByVal args() As String)
            FindAndReplace()
        End Sub
        ''' <summary>
        ''' Find and replace a specific text in an existing DOCX document.
        ''' </summary>
        ''' <remarks>
        ''' Details: https://sautinsoft.com/products/document/examples/find-and-replace-text-in-docx-document-net-csharp-vb.php
        ''' </remarks>
        Public Shared Sub FindAndReplace()
            ' Path to the loadable document.
            Dim loadPath As String = "..\example.docx"

            Dim dc As DocumentCore = DocumentCore.Load(loadPath)

            ' Find "Bean" and Replace everywhere on "Joker"
            Dim regex As New Regex("Bean", RegexOptions.IgnoreCase)

            ' Start:

            ' Please note, Reverse() makes sure that action Replace() doesn't affect to Find().
            For Each item As ContentRange In dc.Content.Find(regex).Reverse()
                item.Replace("Joker", New CharacterFormat() With {
                .BackgroundColor = Color.Yellow,
                .FontName = "Arial",
                .Size = 16.0
                   })
            Next item

            ' End:

            ' The code above finds and replaces the content in the whole document.
            ' Let us say, you want to replace a text inside shape blocks only:

            ' 1. Comment the code above from the line "Start" to the "End".
            ' 2. Uncomment this code:
            'For Each shp As Shape In dc.GetChildElements(True, ElementType.Shape).Reverse()
            'For Each item As ContentRange In shp.Content.Find(regex).Reverse()
            'item.Replace("Joker", New CharacterFormat() With {
            '.BackgroundColor = Color.Yellow,
            '.FontName = "Arial",
            '.Size = 16.0
            '       })
            'Next item
            'Next shp

            ' Save the document as DOCX format.
            Dim savePath As String = Path.ChangeExtension(loadPath, ".replaced.docx")
            dc.Save(savePath, SaveOptions.DocxDefault)

            ' Open the original and result documents for demonstration purposes.
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(loadPath) With {.UseShellExecute = True})
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(savePath) With {.UseShellExecute = True})
        End Sub
    End Class
End Namespace
See Also