Find and Replace Text and Style
RadWordsProcessing gives you the ability to search for a string in a RadFlowDocument instance and replace all matches. The library also allows you to replace the styling of the matches alone.
You can search and replace text or styling using RadFlowDocumentEditor. This article lists the available methods and describes how you can use them. This feature is available since R2 2021 release version.
Find Text
RadFlowDocumentEditor exposes the FindAll() method to enable you to find all instances of a string. You can choose between the following overloads:
| Method | Description |
|---|---|
FindAll(string text, bool matchCase=true, bool matchWholeWord=false) | Finds all occurrences of the specified string. Default values: matchCase = true, matchWholeWord = false. |
FindAll(Regex regex) | Finds all matches of the passed Regex. |
Both methods return a collection of FindResult instances, which in turn expose the following properties:
| Property | Description |
|---|---|
Runs | Gets a collection of Runs that contains the searched text. |
RelativeStartIndex | Gets the index of the first character in the searched text inside the first Run. |
RelativeEndIndex | Gets the index of the last character in the searched text inside the last Run. |
FullMatchText | Gets the matched text. |
Example 1 shows how to create a RadFlowDocumentEditor instance and use it to find all matches of the word "code".
Example 1: Find text
RadFlowDocument flowDocument;
DocxFormatProvider docxFormatProvider = new DocxFormatProvider();
using (Stream input = File.OpenRead("input.docx"))
{
flowDocument = docxFormatProvider.Import(input, TimeSpan.FromSeconds(10));
}
RadFlowDocumentEditor radFlowDocumentEditor = new RadFlowDocumentEditor(flowDocument);
ReadOnlyCollection<Telerik.Windows.Documents.Flow.TextSearch.FindResult> findResults = radFlowDocumentEditor.FindAll("text to search", matchCase: true, matchWholeWord: true);
Replace Text
To find all instances of a string and replace it with another one, you can use the ReplaceText() method of RadFlowDocumentEditor. The method features the following overloads:
| Method | Description |
|---|---|
ReplaceText(string oldText, string newText, bool matchCase=true, bool matchWholeWord=false) | Replaces all occurrences of a string with another string. Default values: matchCase = true, matchWholeWord = false. |
ReplaceText(Regex regex, string newText) | Replaces all matches of the specified Regex with the new text. |
Example 2 shows how to create a RadFlowDocumentEditor instance and use it to replace all matches of the word "code" with the phrase "source code".
Example 2: Replace text
radFlowDocumentEditor.ReplaceText("old text", "new text", matchCase: true, matchWholeWord: true);
Replace Styling
RadFlowDocumentEditor gives you the ability to format all occurrences of a string in a document. This can be achieved by using one of the overloads of the ReplaceStyling() method:
| Method | Description |
|---|---|
ReplaceStyling(string searchedText, Action<CharacterProperties> replacePropertiesAction) | Applies character property changes to all matches with the casing of the string. |
ReplaceStyling(string searchedText, bool matchCase, bool matchWholeWord, Action<CharacterProperties> replacePropertiesAction) | Applies character property changes to all matches. Accepts Boolean parameters to control case matching and whole-word matching. |
ReplaceStyling(Regex regex, Action<CharacterProperties> replacePropertiesAction) | Applies character property changes to all matches of the Regex. |
Example 3 shows how to apply a red highlight color to all occurrences of the word "alert".
Example 3: Replace character properties
radFlowDocumentEditor.ReplaceStyling("text", new Action<CharacterProperties>((properties) =>
{
properties.HighlightColor.LocalValue = Colors.Red;
}));