Import/Export

RadRichTextBox 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 RadRichTextBox 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 RadRichTextBox can export and import the following formats:

  • XAML - to import/export XAML documents you have to use the XamlFormatProvider class.

  • DOCX - to import/export DOCX documents you have to use the DocxFormatProvider class.

  • HTML - to import/export HTML documents you have to use the HtmlFormatProvider class.

  • RTF- to import/export RTF documents you have to use the RtfFormatProvider class.

  • Plain text - to import/export plain text documents you have to use the TxtFormatProvider class.

  • PDF - to export documents to PDF you have to use the PdfFormatProvider class.

Each FormatProvider class is located in a separate assembly matching its name. For example, the XamlFormatProvider class is in the Telerik.Windows.Documents.FormatProviders.Xaml.dll assembly. The only exception is the TxtFormatProvider, which is included in Telerik.Windows.Documents.

Specifics

RadRichTextBox'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 requirements:

XamlFormatProvider

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

DocxFormatProvider and RtfFormatProvider

InlineUIContainers are ignored when exporting with DocxFormatProvider and RtfFormatProvider, as RadRichtextBox's concept of controls is different from that of Microsoft Word.

The same applies for importing documents containing content controls such as Text Box, Combo Box and Date Picker.

HtmlFormatProvider

As HTML's concept of headers and footers is different, when exporting with RadRichTextBox__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 Pagedlayout mode as a whole. Page breaks inserted in HTML for printing purposes are also ignored.

InlineUIContainers are exported as comments. Read more here.

TxtFormatProvider

Imports and exports only plain text.

PdfFormatProvider

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

InlineUIContainers can be exported as images or can be ignored. Read more here.

Examples

Here are some examples on how to export and import.

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

C#

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

VB.NET

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

Export to File

C#

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

VB.NET

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 Boolean = saveDialog.ShowDialog()
    If dialogResult = True Then
        Using output As Stream = saveDialog.OpenFile()
            provider.Export(document, output)
            MessageBox.Show("Saved Successfuly!")
        End Using
    End If
End Sub

Import from String

C#

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

VB.NET

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

Import from File

C#

public RadDocument ImportDocx()
{
    RadDocument document = null;
    IDocumentFormatProvider provider = new DocxFormatProvider();
    OpenFileDialog openDialog = new OpenFileDialog();
    openDialog.Filter = "Documents|*.docx";
    openDialog.Multiselect = false;
    bool? dialogResult = openDialog.ShowDialog();
    if (dialogResult == true)
    {
        using (Stream stream = openDialog.File.OpenRead())
        {
            document = provider.Import(stream);
        }
    }            
    return document;
}

VB.NET

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 System.Nullable(Of Boolean) = openDialog.ShowDialog()
    If dialogResult = True Then
        Using stream As FileStream = openDialog.File.OpenRead()
            document = provider.Import(stream)
        End Using
    End If
    Return document
End Function

See Also