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

Using RtfFormatProvider

RtfFormatProvider makes it easy to import and export RadDocument to/from RTF format, preserving the entire document structure and formatting.

To use RtfFormatProvider, you should reference the Telerik.WinControls.RichTextEditor.dll assembly and add the following namespace:

  • Telerik.WinForms.Documents.FormatProviders.Rtf

Import

In order to import an RTF document, you need to use the Import method of RtfFormatProvider.

The code from Example 1 shows how to use RtfFormatProvider to import an RTF document from a file.

Import Document from a File

RtfFormatProvider provider = new RtfFormatProvider();
using (Stream input = File.OpenRead(@"..\..\RichTextEditor\ImportExport\Sample.rtf"))
{
    this.radRichTextEditor1.Document = provider.Import(input);
}

Dim provider As RtfFormatProvider = New RtfFormatProvider()
Using input As Stream = File.OpenRead("..\..\RichTextEditor\ImportExport\Sample.rtf")
    Me.radRichTextEditor1.Document = provider.Import(input)
End Using

And here is how you can import a document from string containing the RTF document:

Import Document from a String

RtfFormatProvider provider = new RtfFormatProvider();
this.radRichTextEditor1.Document = provider.Import(input);

Dim provider As RtfFormatProvider = New RtfFormatProvider()
Me.radRichTextEditor1.Document = provider.Import(input)

The resulting RadDocument can be used like any code-generated document.

Export

In order to export a document to RTF, you need to use the Export method of RtfFormatProvider.

The example below shows how to use RtfFormatProvider to export RadDocument to a file.

Export a Document to a File

RtfFormatProvider provider = new RtfFormatProvider();
using (Stream output = File.Create("Sample.rtf"))
{
    RadDocument document = this.radRichTextEditor1.Document;
    provider.Export(document, output);
}

Dim provider As RtfFormatProvider = New RtfFormatProvider()
Using output As Stream = File.OpenWrite("Sample.rtf")
    Dim document As RadDocument = Me.radRichTextEditor1.Document
    provider.Export(document, output)
End Using

You can also export the document to a string and preserve it in a database.

Export a Document to a String

RtfFormatProvider provider = new RtfFormatProvider();
RadDocument document = this.radRichTextEditor1.Document;
string output = provider.Export(document);

Dim provider As RtfFormatProvider = New RtfFormatProvider()
Dim document As RadDocument = Me.radRichTextEditor1.Document
Dim output As String = provider.Export(document)

The resulting documents can be opened in any application that supports RTF documents.

See Also

In this article