Click or drag to resize

ContentRangeReplace Method (ContentRange)

Replaces the content of the current ContentRange's with a content specified with the range parameter.

Namespace:  SautinSoft.Document
Assembly:  SautinSoft.Document (in SautinSoft.Document.dll) Version: 3.8.6.28 (3.8.6.28)
Syntax
public ContentRange Replace(
	ContentRange range
)

Parameters

range
Type: SautinSoft.DocumentContentRange
The ContentRange which specifies content which should replace the current ContentRange.

Return Value

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

namespace Sample
{
    class Sample
    {

        static void Main(string[] args)
        {
            FindAndReplace();
        }

        /// <summary>
        /// Find and replace a text using ContentRange.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/examples/find-replace-content-net-csharp-vb.php
        /// </remarks>
        public static void FindAndReplace()
        {
            // Path to a loadable document.
            string loadPath = @"..\..\critique.docx";

            // Load a document intoDocumentCore.
            DocumentCore dc = DocumentCore.Load(loadPath);

            Regex regex = new Regex(@"bean", RegexOptions.IgnoreCase);

            //Find "Bean" and Replace everywhere on "Joker :-)"
            // Please note, Reverse() makes sure that action replace not affects to Find().
            foreach (ContentRange item in dc.Content.Find(regex).Reverse())
            {
                item.Replace("Joker");
            }

            // Save our document into PDF format.
            string savePath = "Replaced.pdf";
            dc.Save(savePath, new PdfSaveOptions());

            // Open the result 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.IO
Imports SautinSoft.Document
Imports System.Linq
Imports System.Text.RegularExpressions

Module Sample
    Sub Main()
        FindAndReplace()
    End Sub

    ''' <summary>
    ''' Find and replace a text using ContentRange.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/examples/find-replace-content-net-csharp-vb.php
    ''' </remarks>
    Sub FindAndReplace()
        ' Path to a loadable document.
        Dim loadPath As String = "..\critique.docx"

        ' Load a document intoDocumentCore.
        Dim dc As DocumentCore = DocumentCore.Load(loadPath)

        Dim regex As New Regex("bean", RegexOptions.IgnoreCase)

        'Find "Bean" and Replace everywhere on "Joker :-)"
        ' Please note, Reverse() makes sure that action replace not affects to Find().
        For Each item As ContentRange In dc.Content.Find(regex).Reverse()
            item.Replace("Joker")
        Next item

        ' Save our document into PDF format.
        Dim savePath As String = "Replaced.pdf"
        dc.Save(savePath, New PdfSaveOptions())

        ' Open the result 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 Module
See Also