Using RtfFormatProvider
RtfFormatProvider allows you to import and export a RadFlowDocument to/from RTF format. The provider preserves the entire document structure and formatting.
To use RtfFormatProvider, add references to the following packages:
- Telerik.Windows.Documents.Core
- Telerik.Windows.Documents.Flow
Import
To import an RTF document, use the Import() method of RtfFormatProvider.
The following code shows how to use RtfFormatProvider to import an RTF document from a file.
Example 1: Open a Sample.rtf file stream and import it into a RadFlowDocument
Telerik.Documents.Flow.Model.RadFlowDocument document;
Telerik.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider provider = new Telerik.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider();
using (Stream input = File.OpenRead("Sample.rtf"))
{
document = provider.Import(input, TimeSpan.FromSeconds(10));
}
You can also import a document from a string containing the RTF document:
Example 2: Read RTF markup from a string and import it into a RadFlowDocument
Telerik.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider provider = new Telerik.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider();
string input = File.ReadAllText("sample.rtf");
Telerik.Documents.Flow.Model.RadFlowDocument document = provider.Import(input, TimeSpan.FromSeconds(10));
The resulting RadFlowDocument can be used like any code-generated document.
Export
To export a document to RTF, use the Export() method of RtfFormatProvider.
The following code shows how to use RtfFormatProvider to export a RadFlowDocument to a file.
Example 3: Export a RadFlowDocument to a sample.rtf file stream
Telerik.Documents.Flow.Model.RadFlowDocument document;
Telerik.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider provider = new Telerik.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider();
using (Stream output = File.Create("sample.rtf"))
{
document = CreateRadFlowDocument();
provider.Export(document, output, TimeSpan.FromSeconds(10));
}
You can also export the document to a string and preserve it in a database.
Example 4: Export a RadFlowDocument directly to an RTF string
Telerik.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider provider = new Telerik.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider();
Telerik.Documents.Flow.Model.RadFlowDocument document = CreateRadFlowDocument();
string output = provider.Export(document, TimeSpan.FromSeconds(10));
The resulting documents can be opened in any application that supports RTF documents.