Docx
DOCX, a part of Office Open XML, is a zipped, XML-based file format developed by Microsoft for representing word processing document and is one of the supported formats by RadWordsProcessing. It is the default target format for Microsoft Word since Microsoft Office 2007.
DocxFormatProvider is compliant with the latest Office Open XML standard - ECMA-376 4th edition, December 2012.
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.Model.RadFlowDocument document;
Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
using (Stream input = File.OpenRead("Sample.docx"))
{
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();
Telerik.Windows.Documents.Flow.Model.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.Model.RadFlowDocument document;
Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
using (Stream output = File.OpenWrite("Sample.docx"))
{
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();
Telerik.Windows.Documents.Flow.Model.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);