Available for: UI for ASP.NET MVC | UI for ASP.NET AJAX | UI for Blazor | UI for WPF | UI for WinForms | UI for Xamarin | UI for WinUI | UI for ASP.NET Core | UI for .NET MAUI

New to Telerik Document Processing? Download free 30-day trial

Replace Text with Document Elements

RadWordsProcessing gives you the ability to search for a string in a RadFlowDocument instance and replace all matches with specified blocks or inlines.

You can search and replace text using RadFlowDocumentEditor. This article lists the available methods and describes how you can use them.

RadFlowDocumentEditor exposes the ReaplaceText() method to enable you to find and replace all instances of a specified string. You can choose between several overloads that allow you to replace the matched text with one or more blocks (tables or paragraphs) or inlines (runs, images, annotation marker).

This functionality is available with R3 2021.

Replace Text with One or more Inlines

  • ReplaceText(string oldText, InlineBase inline, bool matchCase = true, bool matchWholeWord = false): Replaces all occurrences of the specified string with a single inline. The last two parameters are optional. If these parameters are not set, the default values are true for matchCase and false for matchWholeWord.
  • ReplaceText(string oldText, IEnumerable<InlineBase> inlines, bool matchCase = true, bool matchWholeWord = false): Replaces all occurrences of the specified string with a list of inlines. The last two parameters are optional. If these parameters are not set, the default values are true for matchCase and false for matchWholeWord.
  • ReplaceText(Regex regex, InlineBase inline): Replaces all matches of the passed Regex with a single inline.
  • ReplaceText(Regex regex, IEnumerable<InlineBase> inlines): Replaces all matches of the passed Regex with multiple inlines.

Example 1: Replace text with a single inline

// Prepare Document  
RadFlowDocument document = new RadFlowDocument(); 
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); 
editor.InsertText("Content Before"); 
editor.InsertParagraph(); 
editor.InsertText("Replace"); 
editor.InsertParagraph(); 
editor.InsertText("Content After"); 
 
Run run = new Run(document); 
run.Text = "New Content"; 
 
// Replace  
editor.ReplaceText("Replace", run, true, true); 

Example 2: Replace text with multiple inlines

// Prepare Document 
RadFlowDocument document = new RadFlowDocument(); 
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); 
editor.InsertText("Content Before Replace Content After"); 
 
List<InlineBase> newContent = new List<InlineBase>(); 
 
Run run = new Run(document); 
run.Text = "New Content"; 
 
ImageInline image = new ImageInline(document); 
image.Image.Size = new System.Windows.Size(100, 100); 
 
newContent.Add(run); 
newContent.Add(image); 
 
// Replace 
editor.ReplaceText("Replace", newContent, true, true); 

Replace Text with One or more Blocks

  • ReplaceText(string oldText, BlockBase block, bool matchCase = true, bool matchWholeWord = false): Replaces all occurrences of the specified string with a single block. The last two parameters are optional. If these parameters are not set, the default values are true for matchCase and false for matchWholeWord.
  • ReplaceText(string oldText, IEnumerable<BlockBase > blocks, bool matchCase = true, bool matchWholeWord = false): Replaces all occurrences of the specified string with a list of blocks. The last two parameters are optional. If these parameters are not set, the default values are true for matchCase and false for matchWholeWord.
  • ReplaceText(Regex regex, BlockBase block): Replaces all matches of the passed Regex with a single block.
  • ReplaceText(Regex regex, IEnumerable<BlockBase> blocks): Replaces all matches of the passed Regex with multiple blocks.

Example 3: Replace text with a single block

// Prepare Document 
RadFlowDocument document = new RadFlowDocument(); 
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); 
editor.InsertText("Content Before"); 
editor.InsertParagraph(); 
editor.InsertText("Replace"); 
editor.InsertParagraph(); 
editor.InsertText("Content After"); 
 
Table table =  this.GetSampleTable(document); 
 
// Replace 
editor.ReplaceText("Replace", table, true, true); 

Example 4: Replace text with multiple blocks

// Prepare Document 
RadFlowDocument document = new RadFlowDocument(); 
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); 
editor.InsertText("Content Before"); 
editor.InsertParagraph(); 
editor.InsertText("Replace"); 
editor.InsertParagraph(); 
editor.InsertText("Content After"); 
 
List<BlockBase> newContent = new List<BlockBase>(); 
Table table =  this.GetSampleTable(document); 
newContent.Add(table); 
newContent.Add(new Paragraph(document)); 
 
Table table2 = this.GetSampleTable(document); 
newContent.Add(table); 
newContent.Add(new Paragraph(document)); 
 
// Replace 
editor.ReplaceText("Replace", newContent, true, true); 
In this article