0

I have a FlowDocument instance from WPF RichTextBox. I have to find all places in the FlowDocument where style is changed.

Incomming parameters:

  • doc of type FlowDocument - document for analysis
  • beginPoint and endPoint of type TextPointer - begin and the end of the range for analysis from the doc

Returning data:

  • TextPointer[] which represents the list of locations in the doc where changes of style detected

Sample of text for analysis: "Welcome to the real world!" There are four chages of style: "Welcome" (normal), "to" (bold), "the" (bold italic), "real" (italic), "world!" (normal)

Thank you!

Mitchel
  • 11
  • 2

1 Answers1

1

FlowDocument contains collection of Blocks. Each Block can be of Paragraph type. Paragraph contains Inlines properties. Every change of the style is an Inline instance that has ContentBegin and ContentEnd.

So final code for whole document should looks like below:

public TextPointer[] ExtractStyleChanges(FlowDocument doc)
{
    var result = new List<TextPointer>();
    foreach(var p in FlowDocument.Blocks.OfType<Paragraph>())
        foreach(var i in p.Inlines)
        {
            result.Add(i.ContentBegin);
        }
    return result.ToArray();            
}

This method can be upgraded with BeginPoint and EndPoint merkers.

Mitchel
  • 11
  • 2