Replace Text with Document Elements
RadWordsProcessing lets you 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 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 (tables or paragraphs) or inlines (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 aretrueformatchCaseandfalseformatchWholeWord.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 aretrueformatchCaseandfalseformatchWholeWord.ReplaceText(Regex regex, InlineBase inline): Replaces all matches of the passedRegexwith a single inline.ReplaceText(Regex regex, IEnumerable<InlineBase> inlines): Replaces all matches of the passedRegexwith 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 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 aretrueformatchCaseandfalseformatchWholeWord.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 aretrueformatchCaseandfalseformatchWholeWord.ReplaceText(Regex regex, BlockBase block): Replaces all matches of the passedRegexwith a single block.ReplaceText(Regex regex, IEnumerable<BlockBase> blocks): Replaces all matches of the passedRegexwith 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);