Rich Text to Plain Text

5 posts, 0 answers
  1. Steve
    Steve avatar
    2 posts
    Member since:
    Aug 2012

    Posted 07 Feb 2013 Link to this post

    I have a requirement that I need to know the indexes of strings in the plain text version of a file (say position 100-150). These strings are highlighted when the user views by adding a different color to the background. My application takes a RTF document type and stores in a database, also it strips out the RTF and stores a plain text version. 

    My goal is to be able to move back and forth between the Plain Text version and the rich text version by still maintaining the plain text indexes (and the highlights). So I need a way (when viewing the RTF) to map the plain text indexes to the RTF document. 

    Also, I have the ability to add new highlights to the RTF version and need to propagate that back down to indexes in the plain text version. 

    Could the RichTextBox help here? 


    ~ Steve
  2. Petya
    Petya avatar
    975 posts

    Posted 12 Feb 2013 Link to this post

    Hi Steve,

    RadRichTextBox's root element RadDocument does not work with numeral indices, but with DocumentPositions. You can find more about positioning here

    Generally, DocumentPosition is associated with the RadDocument instance its in. Thus, you can't save a position, export/import and try to access the same position, because the RadDocument instance in the editor will be different.

    May I ask you why you need to keep your documents as plain text? RadRichTextBox has the ability to export its documents to string by preserving the rich formatting at the same time. If you choose that approach no additional preprocessing will be necessary. More about the format providers can be found here

    Do not hesitate to get back to us if you have other comments or questions.
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Steve
    Steve avatar
    2 posts
    Member since:
    Aug 2012

    Posted 13 Mar 2013 Link to this post

    Let me ask the question a more direct way to make things simpler. 

    I have an RTF document which I currently strip out all the RTF and make it a plain text file. I have strings that are meaningful in the document that I mark off by their character position in the plain text document. I show these strings today to the user in the viewer by highlighting those character positions and adding some tags to show the highlight. 

    My requirement now is to show those same highlights in the original RTF document using your RichTextViewer. So what I have to work with is a full RTF document and some character position's that I need to show. 

    Is there a way to get at the RTF text character position (Like it looks in plain text), so I can add a highlight, but still maintain the RTF formatting?

    Also, are there any other controls that would help us in this? I really just need to show the plain text index highlights in the RTF document. 


  5. Iva Toteva
    Iva Toteva avatar
    1319 posts

    Posted 18 Mar 2013 Link to this post

    Hi Steve,

    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 = new DocumentTextMap(this.editor.Document);
    DocumentPosition pos1 = new DocumentPosition(this.editor.Document);
    DocumentPosition pos2 = new DocumentPosition(this.editor.Document);
    documentMap.InitMap(pos1, 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!

    Kind regards,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Andrew Syrov
    Andrew Syrov avatar
    2 posts
    Member since:
    Jan 2010

    Posted 15 Apr 2013 Link to this post

    This code provided fails for me. Specifically .MoveToNextInline() sometimes returns false and therefore MapFromIndexInText return null, or Assert fails. Documentation on MoveToNextInline has little details on why it returns false and how to move to next element in this case.
Back to Top
DevCraft banner