How to load .rtf in RadRichTextBox by coding

2 posts, 0 answers
  1. Brieuc
    Brieuc avatar
    6 posts
    Member since:
    Jun 2016

    Posted 17 Jun 2016 Link to this post

    Hi all,

    I am working on a WPF application using Telerik UI for Silverlight ( and coded in C# .NET.
    I took the code of the previous link  and set it up to fit my needs.
    I am now facing to an issue : How to directly load by coding a .rtf document into a RadRichTextBox ?

    I tried two ways :
    RtfFormatProvider provider = new RtfFormatProvider();
    RadDocument = null;
    using (FileStream stream = new FileStream(FileName, FileMode.Open))
        document = provider.Import(stream);
    DocumentFragment docFrag = new DocumentFragment(document);

    -> But that way causes an error because the “Import” method returns a RadFlowDocument whereas I am expecting a RadDocument.

    RtfFormatProvider provider = new RtfFormatProvider();
                Telerik.Windows.Documents.Model.Section section = new Telerik.Windows.Documents.Model.Section();
                Telerik.Windows.Documents.Model.Paragraph paragraph = new Telerik.Windows.Documents.Model.Paragraph();
                string line;

                using (StreamReader stream = new StreamReader(FileName))
                    while ((line = stream.ReadLine()) != null)
                        Telerik.Windows.Documents.Model.Span span = new Telerik.Windows.Documents.Model.Span(line);
                        catch(Exception e)
                            string x = e.StackTrace;

    -> There, even if there is no error, the problem is that nothing is shown in the editor.

    The most strange is that, when I run the program without trying to load any document at the opening, the window opens perfectly and all the buttons react with the right behavior (see attachment “RadRichTextBoxEditor.png”) but I am not allowed to load a .rtf document whereas it is allowed on the link provided at the top of this topic. (see attachment “notSupportedRTF.png”)

    Any help or tip will be much appreciated :)

  2. Tanya
    Tanya avatar
    577 posts

    Posted 21 Jun 2016 Link to this post

    Hello Brieuc,

    The RtfFormatProvider class whose Import() method returns an instance of type RadFlowDocument is related to our RadWordsProcessing library. From the description, it seems like there is a wrong reference or namespace added. In order to use the RtfFormatProvider of RadRichTextBox, you will need to add a reference to the Telerik.Windows.Documents.FormatProviders.Rtf assembly and a using directive to the Telerik.Windows.Documents.FormatProviders.Rtf namespace. Most probably you cannot import an RTF document due to missing required reference. Please, check the Getting Started topic that lists all the necessary references for the control to work properly.

    Here is how I changed the first code snippet you shared in order to import a document in RadRichTextBox:
    Telerik.Windows.Documents.FormatProviders.Rtf.RtfFormatProvider provider = new Telerik.Windows.Documents.FormatProviders.Rtf.RtfFormatProvider();
    using (FileStream stream = new FileStream(FileName, FileMode.Open))
        this.radRichTextBox.Document = provider.Import(stream);

    If you need more information on the import/export functionality of RadRichTextBox, you could refer to the related documentation section.

    As to the second approach you tried, we strongly recommend using the methods of the RadDocumentEditor class instead the direct model of RadRichTextBox when modifying a document that is already shown in the control. The editor will trigger important updates and will ensure that the modifications will be correctly respected.

    I noticed that you have tried to achieve the goal using the DocumentFragment class. It is usually used when you need to insert a document into another document. More information about its usage is available in the Formatting API article

    Hope this helps.

    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top