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

Using DocxFormatProvider

DocxFormatProvider makes it easy to import and export RadRichTextEditor to/from DOCX format, preserving the entire document structure and formatting.

All you have to do in order to use DocxFormatProvider is to reference the Telerik.WinControls.RichTextEditor.dll assembly and add the following namespace:

  • Telerik.Windows.Documents.FormatProviders.OpenXml

Import

In order to import a .docx file, you need to use the Import() method of DocxFormatProvider. The code example shows how to use DocxFormatProvider to import a Docx document from a file.

Import Document from a File

DocxFormatProvider provider = new DocxFormatProvider();
using (FileStream inputStream = File.OpenRead(@"..\..\RichTextEditor\ImportExport\Sample.docx"))
{
    this.radRichTextEditor1.Document = provider.Import(inputStream);
}

Dim provider As DocxFormatProvider = New DocxFormatProvider()
Using inputStream As FileStream = File.OpenRead("..\..\RichTextEditor\ImportExport\Sample.docx")
    Me.radRichTextEditor1.Document = provider.Import(inputStream)
End Using

And here is how you can import a document from byte array containing the Docx document:

Import Document from a Byte Array

DocxFormatProvider provider = new DocxFormatProvider();
byte[] input = File.ReadAllBytes(@"..\..\RichTextEditor\ImportExport\Sample.docx");
this.radRichTextEditor1.Document = provider.Import(input);

Dim provider As DocxFormatProvider = New DocxFormatProvider()
Dim input As Byte() = File.ReadAllBytes("..\..\RichTextEditor\ImportExport\Sample.docx")
Me.radRichTextEditor1.Document = provider.Import(input)

Export

In order to export a document to DOCX, you need to use the Export() method of DocxFormatProvider. The example shows how to use DocxFormatProvider to export RadDocument to a file.

Export Document to a File

DocxFormatProvider provider = new DocxFormatProvider();
using (FileStream output = File.OpenWrite("Sample.docx"))
{
    RadDocument document = this.radRichTextEditor1.Document;
    provider.Export(document, output);
}

Dim provider As DocxFormatProvider = New DocxFormatProvider()
Using output As FileStream = File.OpenWrite("Sample.docx")
    Dim document As RadDocument = Me.radRichTextEditor1.Document
    provider.Export(document, output)
End Using

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

Export Document to a Byte Array

DocxFormatProvider provider = new DocxFormatProvider();
RadDocument document = this.radRichTextEditor1.Document;
byte[] output = provider.Export(document);

Dim provider As DocxFormatProvider = New DocxFormatProvider()
Dim document As RadDocument = Me.radRichTextEditor1.Document
Dim output As Byte() = provider.Export(document)

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

See Also

In this article