This is a migrated thread and some comments may be shown as answers.

Find String in Document, return Block

1 Answer 307 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
Florian
Top achievements
Rank 1
Florian asked on 29 Jul 2015, 10:28 AM

Hi there.

 

I have a Document i want to Search for a Specific Variable.

I need to get the parent container/block of the text.

 

thx

1 Answer, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 31 Jul 2015, 01:37 PM
Hi Florian,

Are you expecting that the strings you're going to search for will be short and in a single paragraph?

This functionality is currently not available in the library and I logged it in our backlog for future consideration: Implement Find (Search) Functionality. You could, however, enumerate the Run elements in all paragraphs and append their text together. This will allow you to implement a text-based search and find the paragraph a particular string is in. 

Here is a simple example showing what I have in mind.
private Paragraph FindparagraphContainingText(RadFlowDocument document, string searchedText)
{
    foreach (var paragraph in document.EnumerateChildrenOfType<Paragraph>())
    {
        StringBuilder paragraphText = new StringBuilder();
 
        foreach (var inline in paragraph.Inlines)
        {
            Run run = inline as Run;
            if (run != null)
            {
                paragraphText.Append(run.Text);
            }
        }
 
        string text = paragraphText.ToString();
        if (text.Contains(searchedText))
        {
            return paragraph;
        }
    }
 
    return null;
}

I hope this helps.

Regards,
Petya
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
WordsProcessing
Asked by
Florian
Top achievements
Rank 1
Answers by
Petya
Telerik team
Share this question
or