Using TxtFormatProvider
TxtFormatProvider makes it easy to import and export RadFlowDocument to/from plain text format, preserving the document structure.
All you have to do in order to use TxtFormatProvider is add references to the assemblies listed below:
- Telerik.Windows.Documents.Core.dll
- Telerik.Windows.Documents.Flow.dll
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"))
{
//RadFlowDocument document = provider.Import(input); //This method is obsolete since Q4 2024.
RadFlowDocument document = provider.Import(input, TimeSpan.FromSeconds(10));
}
And here is how you can import a document from string:
Example 2: Import document from a string
TxtFormatProvider provider = new TxtFormatProvider();
//RadFlowDocument document = provider.Import(input); //This method is obsolete since Q4 2024.
RadFlowDocument document = provider.Import(input, TimeSpan.FromSeconds(10));
The resulting RadFlowDocument 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 RadFlowDocument to a file.
Example 3: Export a document to a file
TxtFormatProvider provider = new TxtFormatProvider();
using (Stream output = File.OpenWrite("sample.txt"))
{
RadFlowDocument document = CreateRadFlowDocument();
//provider.Export(document, output); //This method is obsolete since Q4 2024.
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 document to a string
TxtFormatProvider provider = new TxtFormatProvider();
RadFlowDocument document = CreateRadFlowDocument();
//string output = provider.Export(document); //This method is obsolete since Q4 2024.
string output = provider.Export(document, TimeSpan.FromSeconds(10));