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

find the span and its text by another text

1 Answer 134 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
zj
Top achievements
Rank 1
zj asked on 04 Apr 2018, 08:37 AM

I had a xaml document Contains the following contents:

<t:TableRow>
        <t:TableCell ColumnSpan="1" PreferredWidth="Fixed,284.066680908203" RowSpan="1">
          <t:Paragraph StyleName="a">
            <t:ReadOnlyRangeStart AnnotationID="21" />
            <t:Span Text="the master Name: John" />
            <t:ReadOnlyRangeEnd AnnotationID="21" />
          </t:Paragraph>
          <t:Paragraph StyleName="a">
            <t:ReadOnlyRangeStart AnnotationID="22" />
            <t:Span ForeColor="#FFFFFFFF" Text="c4c030c8-c0d1-4fa3-8d06-92e34ee25072" />
            <t:ReadOnlyRangeEnd AnnotationID="22" />
          </t:Paragraph>
          <t:Paragraph StyleName="a">
            <t:ReadOnlyRangeStart AnnotationID="23" />
            <t:Span Text="Sign:" />
            <t:ReadOnlyRangeEnd AnnotationID="23" />
          </t:Paragraph>
          <t:Paragraph StyleName="a">
            <t:ReadOnlyRangeStart AnnotationID="25" />
            <t:Span Text="IdNo:" />
            <t:ReadOnlyRangeEnd AnnotationID="25" />
          </t:Paragraph>
          <t:Paragraph StyleName="a">
            <t:ReadOnlyRangeStart AnnotationID="26" />
            <t:Span Text="Date:" />
            <t:ReadOnlyRangeEnd AnnotationID="26" />
            <t:ReadOnlyRangeStart AnnotationID="27" />
            <t:Span  Text="     "  UnderlineDecoration="Line" />
            <t:ReadOnlyRangeEnd AnnotationID="27" />
            <t:ReadOnlyRangeStart AnnotationID="28" />
            <t:Span  Text="year" />
            <t:ReadOnlyRangeEnd AnnotationID="28" />
            <t:ReadOnlyRangeStart AnnotationID="29" />
            <t:Span  Text="     " UnderlineDecoration="Line" />
            <t:ReadOnlyRangeEnd AnnotationID="29" />
            <t:ReadOnlyRangeStart AnnotationID="30" />
            <t:Span   Text="month" />
            <t:ReadOnlyRangeEnd AnnotationID="30" />
            <t:ReadOnlyRangeStart AnnotationID="31" />
            <t:Span  Text="     " UnderlineDecoration="Line" />
            <t:ReadOnlyRangeEnd AnnotationID="31" />
            <t:ReadOnlyRangeStart AnnotationID="32" />
            <t:Span Text="day" />
            <t:ReadOnlyRangeEnd AnnotationID="32" />
          </t:Paragraph>
          <t:Paragraph />
          <t:Paragraph StyleName="a">
            <t:ReadOnlyRangeStart AnnotationID="33" />
            <t:Span Text="the slave  Name: slave" />
            <t:ReadOnlyRangeEnd AnnotationID="33" />
          </t:Paragraph>
          <t:Paragraph StyleName="a">
            <t:ReadOnlyRangeStart AnnotationID="34" />
            <t:Span ForeColor="#FFFFFFFF" Text="076b7ada-a564-454a-9933-3ca6f856876f" />
            <t:ReadOnlyRangeEnd AnnotationID="34" />
          </t:Paragraph>
          <t:Paragraph StyleName="a">
            <t:ReadOnlyRangeStart AnnotationID="35" />
            <t:Span Text="Sign:" />
            <t:ReadOnlyRangeEnd AnnotationID="35" />
          </t:Paragraph>
          <t:Paragraph StyleName="a">
            <t:ReadOnlyRangeStart AnnotationID="37" />
            <t:Span Text="IdNo:" />
            <t:ReadOnlyRangeEnd AnnotationID="37" />
          </t:Paragraph>
          <t:Paragraph StyleName="a">
            <t:ReadOnlyRangeStart AnnotationID="38" />
            <t:Span Text="Date:" />
            <t:ReadOnlyRangeEnd AnnotationID="38" />
            <t:ReadOnlyRangeStart AnnotationID="39" />
            <t:Span  Text="     " UnderlineDecoration="Line" />
            <t:ReadOnlyRangeEnd AnnotationID="39" />
            <t:ReadOnlyRangeStart AnnotationID="40" />
            <t:Span   Text="year" />
            <t:ReadOnlyRangeEnd AnnotationID="40" />
            <t:ReadOnlyRangeStart AnnotationID="41" />
            <t:Span  Text="     " UnderlineDecoration="Line" />
            <t:ReadOnlyRangeEnd AnnotationID="41" />
            <t:ReadOnlyRangeStart AnnotationID="42" />
            <t:Span  Text="month" />
            <t:ReadOnlyRangeEnd AnnotationID="42" />
            <t:ReadOnlyRangeStart AnnotationID="43" />
            <t:Span  Text="     " UnderlineDecoration="Line" />
            <t:ReadOnlyRangeEnd AnnotationID="43" />
            <t:ReadOnlyRangeStart AnnotationID="44" />
            <t:Span  Text="day" />
            <t:ReadOnlyRangeEnd AnnotationID="44" />
          </t:Paragraph>
        </t:TableCell>
        <t:TableCell ColumnSpan="1" PreferredWidth="Fixed,284.066680908203" RowSpan="1">
          <t:Paragraph StyleName="a">
            <t:Span Text="legal Name:" />
          </t:Paragraph>
          <t:Paragraph StyleName="a">
            <t:Span Text="Sign:" />
          </t:Paragraph>
          <t:Paragraph StyleName="a">
            <t:Span Text="IdNo:" />
          </t:Paragraph>
          <t:Paragraph StyleName="a">
            <t:Span Text="Date: " />
            <t:Span  Text="     " UnderlineDecoration="Line" />
            <t:Span  Text="year" />
            <t:Span  Text="  " UnderlineDecoration="Line" />
            <t:Span  Text="month" />
            <t:Span  Text="  " UnderlineDecoration="Line" />
            <t:Span  Text="day" />
          </t:Paragraph>
          <t:Paragraph FontSize="18.6666660308838" StyleName="a">
            <t:Paragraph.ParagraphSymbolPropertiesStyle>
              <s:SpanProperties FontSize="18.6666660308838" FontWeight="Bold" />
            </t:Paragraph.ParagraphSymbolPropertiesStyle>
          </t:Paragraph>
        </t:TableCell>
      </t:TableRow>
    </t:Table>

 

what i knew is the string "the master Name:" how can I get the next span with the text "c4c030c8-c0d1-4fa3-8d06-92e34ee25072" (the text is I want to get) 

 

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Tanya
Telerik team
answered on 06 Apr 2018, 12:12 PM
Hello,

You can achieve that by positioning the caret just after the known text and searching for the content of the next read-only range. In the following snippet, a document selection is created starting from the read-only range start and ending when the range ends so we can later extract the selected text:

private void button1_Click(object sender, RoutedEventArgs e)
{
    this.radRichTextBox.Document.Selection.Clear();
    DocumentTextSearch search = new DocumentTextSearch(this.radRichTextBox.Document);
    TextRange match = search.Find("the master Name");
    this.radRichTextBox.Document.CaretPosition.MoveToPosition(match.EndPosition);
 
    var currentInline = this.radRichTextBox.Document.CaretPosition.GetCurrentInline();
    while (!(currentInline is ReadOnlyRangeStart))
    {
        this.radRichTextBox.Document.CaretPosition.MoveToNextInline();
        currentInline = this.radRichTextBox.Document.CaretPosition.GetCurrentInline();
    }
 
    string content = this.ExtractTextInReadOnlyRange(currentInline as ReadOnlyRangeStart);
    MessageBox.Show(content);
}
 
private string ExtractTextInReadOnlyRange(ReadOnlyRangeStart rangeStart)
{
    this.radRichTextBox.Document.CaretPosition.MoveToEndOfDocumentElement(rangeStart);
    DocumentPosition startPosition = new DocumentPosition(this.radRichTextBox.Document.CaretPosition);
    DocumentPosition endPosition = new DocumentPosition(startPosition);
 
    InlineLayoutBox currentInline = this.radRichTextBox.Document.CaretPosition.GetCurrentInlineBox();
 
    string content = string.Empty;
    while (!(currentInline.AssociatedInline is ReadOnlyRangeEnd))
    {
        endPosition.MoveToNextInline();
        currentInline = endPosition.GetCurrentInlineBox();
    }
 
    this.radRichTextBox.Document.Selection.SetSelectionStart(startPosition);
    this.radRichTextBox.Document.Selection.AddSelectionEnd(endPosition);
 
    content = this.radRichTextBox.Document.Selection.GetSelectedText();
    this.radRichTextBox.Document.Selection.Clear();
 
    return content;
}

More information about the used API in the code above you can find in our documentation topics: Search, Positioning, Selection.

Hope this is helpful.

Regards,
Tanya
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
Tags
RichTextBox
Asked by
zj
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Share this question
or