New to Telerik UI for WPF? Download free 30-day trial

Using TxtFormatProvider

TxtFormatProvider makes it easy to import and export RadDocument to/from plain text format, preserving the document structure.

To use TxtFormatProvider, you should add references to:

  • Telerik.Windows.Documents.dll

The TxtFormatProvider of RadRichTextBox resides in the Telerik.Windows.Documents.FormatProviders.Txt namespace, so you will need to add a using statement for it.

Import

In order to import a plain text document, you need to use the Import() method of TxtFormatProvider. Example 1 shows how to use TxtFormatProvider to import a document from a file.

Example 1: Import document from a file

TxtFormatProvider provider = new TxtFormatProvider(); 
using (Stream input = File.OpenRead("Sample.txt")) 
{ 
    RadDocument document = provider.Import(input); 
} 
And here is how you can import a document from string:

Example 2: Import document from a string

TxtFormatProvider provider = new TxtFormatProvider(); 
RadDocument document = provider.Import(input); 
The resulting RadDocument can be manipulated like any code-generated document.

Export

In order to export a document to plain text, you need to use the Export() method of TxtFormatProvider.

Example 3 shows how to use TxtFormatProvider to export RadDocument to a file.

Example 3: Export a document to a file

TxtFormatProvider provider = new TxtFormatProvider(); 
using (Stream output = File.OpenWrite("sample.txt")) 
{ 
    RadDocument document = CreateRadDocument(); 
    provider.Export(document, output); 
} 
You can also export the document to a string and preserve it in a database.

Example 4: Export a document to a string

TxtFormatProvider provider = new TxtFormatProvider(); 
RadDocument document = CreateRadDocument(); 
string output = provider.Export(document); 

However, the format providers cannot be used in XAML and you have to implement a logic that will call their Import() and Export() methods. This is something that you might not want to be concerned with when using RadRichTextBox in a data bound scenarios. For such cases, the DataProvider classes are used. They wrap the FormatProviders' functionality and allow its usage in XAML.

See Also

In this article
Not finding the help you need? Improve this article