The scenario you are explaining seems possible using the DocumentTextMap
method and more precisely its MapFromIndexInText()
method. The following snippet will create a map and select part of the text in the editor with length 5, starting from the 2nd document position.
DocumentTextMap documentMap =
DocumentPosition pos1 =
DocumentPosition pos2 =
TextRange textRange = documentMap.MapFromIndexInText(2, 5);
What you should be aware in this regard is that if the RTF document contains images, they will too be counted as positions and the logic might not work as expected.
Also, please note that some symbols are counted as more that 1 position, for example paragraph end symbol is considered 2 characters ("\r\n") and will actually be selected by the snippet from above if the second character is included in the selection.
In order to adjust the behavior in these cases, you can implement your custom DocumentTextMap starting from the default implementation. Please find attach a zip with the default implementation of DocumentTextMap, slightly modified to use only public methods. You can further extend it to suit your needs.
I hope this will be helpful!