Telerik blogs
DotNetT2 Light_1200x303

With the last release, R2 2021, we added a full-fledged Search functionality in the RadWordsProcessing library. Let’s take a deeper dive.

Looking for Text in Your RadFlowDocument?

Implementing a search functionality was one of the most-demanded features in the RadWordsProcessing library. Now you can check the RadFlowDocument for the occurrences of certain text even if the finding text is split into multiple Runs.

The result of a successful search is a list of all the occurrences of the searched text inside the document. As stated above, the text may be part of single or multiple runs and can contain only a part of them. Also, the search can be executed by using a regular expression (regex) string.

Every occurrence contains a collection of runs, the index of the first character in the searched text inside the first run, the index of the last character in the searched text inside the last run, and the text that is matched (in the case of regex string).

What’s New

The RadFlowDocumentEditor now provides two brand-new methods for finding occurrences of a specified text.

The first one finds all occurrences of the specified string and can respect match case and match whole word:

  • FindAll(string text, bool matchCase=true, bool matchWholeWord=false)

And the second one finds all occurrences of a matched text by the specified regex:

  • FindAll(Regex regex)

Both methods return a collection of FindResult instances.

For more detailed information on this topic and about the replace options, check the Find and Replace Text and Style help article.

Example

Using the already mentioned FindAll() method, we will get all the FindResults within the document:

RadFlowDocument document = ImportOrCreateDocument();
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
ReadOnlyCollection<FindResult> findResults = editor.FindAll("Word", matchCase: true, matchWholeWord: false);

When we already have the collection of the results, we could iterate them and print the matched content on the console:

for (int i = 0; i < findResults.Count; i++)
{
FindResult result = findResults[i];
if (i == 0)
{
Console.WriteLine($"FullMatchText: {result.FullMatchText}");
Console.WriteLine();
}
Console.WriteLine($"Result #{i + 1}");
string runText = result.Runs[0].Text;
int start = result.RelativeStartIndex;
int end = result.RelativeEndIndex;
string textBefore = runText.Substring(0, start);
string machedText = runText.Substring(start, end - start + 1);
string textAfter = runText.Substring(end + 1);
Console.WriteLine($"{textBefore}<<{machedText}>>{textAfter}");
Console.WriteLine();
}

Here’s what the first several results will look like on the console:

BlogPost WordsProcessing Search 01 - text results

Or something similar developed in a WPF project:

BlogPost WordsProcessing Search 02

The screenshot is taken from the Telerik UI for WPF Controls Demo. You can download and check the latest version of this demo on demos.telerik.com/wpf.

Try RadWordsProcessing Yourself

Get yourself a free trial of Telerik Document Processing today and start developing your apps better, faster and more easily.

Start My Trial

Share Your Feedback

Let’s continue to build the future of Telerik Document Processing together! So, don’t forget to share your thoughts as a comment below or let us know if you have any suggestions and/or need any features or components by visiting our Telerik Document Processing Feedback Portal.


profile_pic_cropped
About the Author

Martin Velikov

Martin is a Software Engineer, part of the Document Processing team in Sofia, Bulgaria since July 2019. He is passionate about new technologies and is always on the crest of a wave with the novelties. In his spare time, Martin likes travelling to new destinations and exploring new cultures, hanging out with friends, reading books, practicing sports, and more.

Related Posts

Comments

Comments are disabled in preview mode.