This is a migrated thread and some comments may be shown as answers.

How to load .rtf in RadRichTextBox by coding

1 Answer 591 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Brieuc
Top achievements
Rank 1
Brieuc asked on 17 Jun 2016, 03:15 PM

Hi all,

I am working on a WPF application using Telerik UI for Silverlight (http://demos.telerik.com/silverlight/#RichTextBox/TelerikEditor) 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);
radRichTextBox.InsertFragment(docFrag);

-> 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);
                    try
                    {
                        paragraph.Inlines.Add(span);
                    }
                    catch(Exception e)
                    {
                        string x = e.StackTrace;
                    }
                }
            }
            section.Blocks.Add(paragraph);
            radRichTextBox.Document.Sections.Add(section);

-> 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 :)

1 Answer, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 21 Jun 2016, 11:57 AM
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.

Regards,
Tanya
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
RichTextBox
Asked by
Brieuc
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Share this question
or