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

Rich Text to Plain Text

4 Answers 170 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 07 Feb 2013, 12:46 PM
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? 

Thanks!

~ Steve

4 Answers, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 12 Feb 2013, 09:59 AM
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.
 
Regards,
Petya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Steve
Top achievements
Rank 1
answered on 13 Mar 2013, 03:50 PM
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. 

Thanks!

0
Iva Toteva
Telerik team
answered on 18 Mar 2013, 05:07 PM
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);
pos2.MoveToNextInline();
pos2.MoveToNextInline();
 
documentMap.InitMap(pos1, pos2);
 
TextRange textRange = documentMap.MapFromIndexInText(2, 5);
 
this.editor.Document.Selection.AddSelectionStart(textRange.StartPosition);
this.editor.Document.Selection.AddSelectionEnd(textRange.EndPosition);

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.

0
Andrew Syrov
Top achievements
Rank 1
answered on 15 Apr 2013, 05:47 PM
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.
Tags
RichTextBox
Asked by
Steve
Top achievements
Rank 1
Answers by
Petya
Telerik team
Steve
Top achievements
Rank 1
Iva Toteva
Telerik team
Andrew Syrov
Top achievements
Rank 1
Share this question
or