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

Using TxtFormatProvider To Get Plain Text From Html

1 Answer 675 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Randy
Top achievements
Rank 1
Randy asked on 20 Oct 2017, 04:02 PM

My application has a RadRichTextBox to collect styled text from users, working great. The RadRichTextBox has an associated HtmlDataProvider and I'm storing the Html property as a string value on a Message view model. However, in another area of my application, I want to load and display the text entered in this RadRichTextBox as plain text, as a property on a different view model, unrelated to the original Message view model, without any styling added by the user. Is there a way to accomplish this without storing a second string property on the Message view model, i.e. is there a way to remove the html markup from the MessageText property?

The RadRichTextBox definition and associated HtmlDataProvider:

<telerik:HtmlDataProvider x:Name="HtmlProvider" RichTextBox="{Binding ElementName=htmlRichTextBox}" Html="{Binding MessageText, Mode=TwoWay}" />
<telerik:RadRichTextBox x:Name="htmlRichTextBox" IsSpellCheckingEnabled="False" Height="300"/>

1 Answer, 1 is accepted

Sort by
0
Polya
Telerik team
answered on 25 Oct 2017, 02:13 PM
Hi Randy,

Each of the format providers is responsible to translate the RadDocument model to its particular format. HtmlFormatProvider exports to html and imports from html, TxtFormatProvider exports/imports to/from plain text. 

That said, as you've suggested, one approach would be to add additional property in your view model that will hold the exported RadRichTextBox document in plain text using the TxtFormatProvider. 

Another apporach is to import the html using the HtmlFormatProvider.Import() method and then export it using a TxtFormatProvider:
HtmlFormatProvider htmlProvider = new HtmlFormatProvider();
RadDocument document = htmlProvider.Import(<your_html_data>);
 
TxtFormatProvider txtProvider = new TxtFormatProvider();
string output = txtProvider.Export(document);

Lastly, if you wish to convert the html from your original property (generated using the HtmlFormatProvider) to plain text, you could use some of the approaches descibed here: https://stackoverflow.com/questions/286813/how-do-you-convert-html-to-plain-text, using either regular expressions or third party tools.

Hope this helps.

Regards,
Polya
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 allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
RichTextBox
Asked by
Randy
Top achievements
Rank 1
Answers by
Polya
Telerik team
Share this question
or