# Replace Text with Document Elements

**RadWordsProcessing** lets you search for a string in a [RadFlowDocument](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/model/radflowdocument) instance and replace all matches with specified blocks or inlines.

You can search and replace text using [RadFlowDocumentEditor](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/radflowdocumenteditor). This article lists the available methods and describes how you can use them.

`RadFlowDocumentEditor` exposes the `ReplaceText()` 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](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/model/model) (tables or paragraphs) or [inlines](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/model/model) (runs, images, annotation marker).

> This functionality is available starting 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 matched text with a single Run containing New Content**

```C#
//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 matched text with a Run followed by an inline image**

```C#
//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 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 matched text with a single table block**

```C#
//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 matched text with multiple table and paragraph blocks**

```C#
//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);
```

## See Also

* [Find and Replace Text and Style](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/find-and-replace/find-and-replace-text)
* [RadFlowDocumentEditor](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/radflowdocumenteditor)
* [RadFlowDocument](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/model/radflowdocument)
* [Replace Placeholder Text with Images in Telerik WordsProcessing](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/replace-text-placeholder-with-image-telerik-wordsprocessing)
