Edit this page

Import/Export

RadRichTextEditor allows you to export and import its content. This is useful in case you want to save the user's input into a data base and then load it from there, or if you want to save/load the content of RadRichTextEditor to/from a file. To import and export you have to use a specific class that implements the IDocumentFormatProvider. You can find built-in classes, that implement this interface, for each of the supported formats. Currently, RadRichTextEditor can export and import the following formats:

  • XAML - to import/export XAML documents you have to use the Telerik.WinForms.Documents.FormatProviders.Xaml.XamlFormatProvider class.

  • DOCX - to import/export DOCX documents you have to use the Telerik.WinForms.Documents.FormatProviders.OpenXml.Docx.DocxFormatProvider class.

  • HTML - to import/export HTML documents you have to use the Telerik.WinForms.Documents.FormatProviders.Html.HtmlFormatProvider class.

  • RTF- to import/export RTF documents you have to use the Telerik.WinForms.Documents.FormatProviders.Rtf.RtfFormatProvider class.

  • Plain text- to import/export plain text documents you have to use the Telerik.WinForms.Documents.FormatProviders.Txt.TxtFormatProvider class.

  • PDF - to export documents to PDF you have to use the Telerik.WinForms.Documents.FormatProviders.Pdf.PdfFormatProvider class.

Specifics

RadRichTextEditor's format providers can import/export a wide variety of features supported by the control. However, features which are not yet supported are stripped on import. Such examples are content controls like text boxes, check boxes, etc. which can be inserted in the document in some rich text editors such as Microsoft Word or can be included in the HTML.

Overall, here are listed the specifics you need to know when choosing the appropriate format provider for you rrequirements:

XamlFormatProvider

As the XAML format is the closest to RadDocument's structure, all supported features are imported/exported without the need of additional handling.

DocxFormatProvider and RtfFormatProvider

Content controls such as Text Box, Combo Box and Date Picker that can be included are currently ignored.

HtmlFormatProvider

As HTML's concept of headers and footers is different, when exporting with RadRichTextEditor headers and footers are ignored. When it comes to importing, the <header> and <footer> tags are included in the content of the imported document, but are not interpreted as Header and Footer of RadDocument.

Moreover, the HtmlFormatProvider ignores the concept of Paged layout mode as a whole. Page breaks inserted in HTML for printing purposes are also ignored.

TxtFormatProvider

Imports and exports only plain text.

PdfFormatProvider

The current version of RadRichTextEditor can only export to PDF, but not import.

Examples

Here are some examples on how to export and import.

note

The "Export to String" and "Import from String" examples are only valid for the text-based format providers (Html, Xaml, Rtf and TxtFormatProvider).
The "Export to File" and "Import from File" are applicable to each of the format providers (save for PDF import). To use them with the desired format just replace the format provider and change the settings of the SaveFileDialog or the OpenFileDialog .

Export to String

public string ExportToXAML(RadDocument document)
{
    XamlFormatProvider provider = new XamlFormatProvider();
    return provider.Export(document);
}

Public Function ExportToXAML(ByVal document As RadDocument) As String
    Dim provider As New XamlFormatProvider()
    Return provider.Export(document)
End Function

Export to File

public void ExportToDocx(RadDocument document)
{
    DocxFormatProvider provider = new DocxFormatProvider();
    SaveFileDialog saveDialog = new SaveFileDialog();
    saveDialog.DefaultExt = ".docx";
    saveDialog.Filter = "Documents|*.docx";
    DialogResult  dialogResult = saveDialog.ShowDialog();
    if (dialogResult == System.Windows.Forms.DialogResult.OK)
    {
        using (Stream output = saveDialog.OpenFile())
        {
            provider.Export(document, output);
            MessageBox.Show("Saved Successfuly!");
        }
    }
}

Public Sub ExportToDocx(ByVal document As RadDocument)
    Dim provider As New DocxFormatProvider()
    Dim saveDialog As New SaveFileDialog()
    saveDialog.DefaultExt = ".docx"
    saveDialog.Filter = "Documents|*.docx"
    Dim dialogResult As DialogResult = saveDialog.ShowDialog()
    If dialogResult = System.Windows.Forms.DialogResult.OK Then
        Using output As Stream = saveDialog.OpenFile()
            provider.Export(document, output)
            MessageBox.Show("Saved Successfuly!")
        End Using
    End If
End Sub

Import from String

public RadDocument ImportXaml(string content)
{
    XamlFormatProvider provider = new XamlFormatProvider();
    return provider.Import(content);
}

Public Function ImportXaml(ByVal content As String) As RadDocument
    Dim provider As New XamlFormatProvider()
    Return provider.Import(content)
End Function

Import from File

public RadDocument ImportDocx()
{
    RadDocument document = null;
    IDocumentFormatProvider provider = new DocxFormatProvider();
    OpenFileDialog openDialog = new OpenFileDialog();
    openDialog.Filter = "Documents|*.docx";
    openDialog.Multiselect = false;
    DialogResult dialogResult = openDialog.ShowDialog();
    if (dialogResult == System.Windows.Forms.DialogResult.OK)
    {
        using (Stream stream = openDialog.OpenFile())
        {
            document = provider.Import(stream);
        }
    }
    return document;
}

Public Function ImportDocx() As RadDocument
    Dim document As RadDocument = Nothing
    Dim provider As IDocumentFormatProvider = New DocxFormatProvider()
    Dim openDialog As New OpenFileDialog()
    openDialog.Filter = "Documents|*.docx"
    openDialog.Multiselect = False
    Dim dialogResult As DialogResult = openDialog.ShowDialog()
    If dialogResult = System.Windows.Forms.DialogResult.OK Then
        Using stream As Stream = openDialog.OpenFile()
            document = provider.Import(stream)
        End Using
    End If
    Return document
End Function

caution

When importing a document, it is converted to RadDocument following the rules of the model that it defines. Content that has no parallel in RadDocument is ignored and can no longer be restored.
When exporting the same document, the resulting docx/HTML/XAML/RTF will be different than the initial one.

See Also