Using DocxFormatProvider
DocxFormatProvider makes it easy to import and export RadFlowDocument to/from docx format, preserving the entire document structure and formatting.
All you have to do in order to use DocxFormatProvider is add references to the assemblies listed below:
- Telerik.Windows.Documents.Core.dll
- Telerik.Windows.Documents.Flow.dll
- Telerik.Windows.Zip.dll
Import
In order to import a docx document, you need to use the Import() method of DocxFormatProvider.
The code in Example 1 shows how to use DocxFormatProvider to import a docx document from a file.
Example 1: Import document from a file
Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
using (Stream input = File.OpenRead("Sample.docx"))
{
RadFlowDocument document = provider.Import(input);
}
And here is how you can import a document from byte array containing the docx document:
Example 2: Import document from a byte array
Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
RadFlowDocument document = provider.Import(input);
The resulting RadFlowDocument can be manipulated like any code-generated document.
Export
In order to export a document to docx, you need to use the Export() method of DocxFormatProvider.
Example 3 shows how to use DocxFormatProvider to export RadFlowDocument to a file.
Example 3: Export a document to a file
Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
using (Stream output = File.OpenWrite("Sample.docx"))
{
RadFlowDocument document = CreateRadFlowDocument(); // CreateRadFlowDocument() is a custom method that creates a simple instance of RadFlowDocument. You can replace it with the instance you would like to export.
provider.Export(document, output);
}
You can also export the document to a byte array and preserve it in a database.
Example 4: Export a document to a byte array
Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
RadFlowDocument document = CreateRadFlowDocument(); // CreateRadFlowDocument() is a custom method that creates a simple instance of RadFlowDocument. You can replace it with the instance you would like to export.
byte[] output = provider.Export(document);