Import/Export
The 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 the RadRichTextBox to/from a file. To import and export you have to use a specific class that implements the Telerik.WinControls.RichTextBox.FormatProviders.IDocumentFormatProvider. You can find built-in classes, that implement this interface, for each of the supported formats. Currently the RadRichTextBox can export and import the following formats:
XAML - to import/export XAML documents you have to use the XamlFormatProvider class from the Telerik.WinControls.RichTextBox.FileFormats.Xaml namespace.
DOCX - to import/export DOCX documents you have to use the DocxFormatProvider class from the Telerik.WinControls.RichTextBox.FileFormats.OpenXml.Docx namespace.
HTML - to import/export HTML documents you have to use the HtmlFormatProvider class from the Telerik.WinControls.RichTextBox.FileFormats.Html namespace.
RTF - to import/export RTF documents you have to use the RtfFormatProvider class from the Telerik.WinControls.RichTextBox.FileFormats.Rtf namespace.
Plain text - to import/export plain text documents you have to use the TxtFormatProvider class from the Telerik.WinControls.RichTextBox.FormatProviders.Txt namespace.
PDF - to export documents to PDF you have to use the PdfFormatProvider class from the Telerik.WinControls.RichTextBox.FileFormats.Pdf namespace.
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 When importing, simply set the returned document to the RadRichTextBox1.Document .
Export to String
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
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
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
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 (FileStream stream = new FileStream(openDialog.FileName, FileMode.Open))
{
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 New FileStream(openDialog.FileName, FileMode.Open)
document = provider.Import(stream)
End Using
End If
Return document
End Function