I want to display a static docx file using a RichTextBox in read only mode in a silverlight 4 user control running on a web site. This is just to display a legal disclaimer on a site. The file exists on the web server, not on the individual's local machine. I'm using VS 2010.
I looked at the examples and I tried using the code from the Document API example for the RichTextBox control. Every time I try to create a Stream object that can be used with the DocxFormatProvider Import method, I get the error that Null exception has occurred.
Is there a sample that shows how to load a docx file from the server (not using the client OpenFileDialog function)?
9 Answers, 1 is accepted
I have attached a demo project which loads a docx from the server into a RadRichTextBox. The docx is loaded from the ClientBin directory of the Web project and I have used WebClient to do that. I hope this helps you. Let us know if you need further assistance.
Sincerely yours,
Ivailo
the Telerik team
If I download the project and run it, it works just exactly like what I want.
I've tried to convert it to VB for my needs, but am having trouble with the "webClient.OpenReadcompleted" line. I get an error message that says: "Public Event OpenReadCompleted(sender As Object, e As System.Net.OpenReadCompletedEventARgs)" is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
Basically, here's what I'm trying to accomplish:
1) I'm developing a SilverLight website for my client using Telerik controls using SilverLight's Navigation structure.
2) Client has a MSWord document that describes their Guidelines.
3) I want to upload that that document to a folder on the website and have it display in a RichTextBox on one of the pages when the user navigates to the page.
4) The guidelines can change from year-to-year, so I want to just be able to upload a new verison of the .docx file to the folder on the host whenever the document changes.
Is this project the correct approach?
If so, hopefully I can get it converted to VB and working with my project and be on my merry way!
Thanks in advance,
Mike T.
Here's a VB version of the project that Ivailo supplied to me....
Actually, I was trying to supply a vb version, but I can't attach a zip file. The main issue you are probably having is in the MainPage.xaml.vb file because VB handles events differenly and C#. Here is that file:
Imports
System
Imports
System.Net
Imports
System.Windows.Controls
Imports
Telerik.Windows.Documents.FormatProviders.OpenXml.Docx
Partial
Public Class MainPage
Inherits UserControl
Private WithEvents webClient As WebClient
Public Sub New()
InitializeComponent()
webClient =
New WebClient
webClient.OpenReadAsync(
New Uri("Rad-RichTextBox-for-Silverlight.docx", UriKind.Relative))
End Sub
Private Sub webClient_OpenReadCompleted(ByVal sender As Object, ByVal e As OpenReadCompletedEventArgs) Handles webClient.OpenReadCompleted
richTextBox.Document =
New DocxFormatProvider().Import(e.Result)
e.Result.Close()
End Sub
End
Class
Let me try that again. Pasting the code looked fine when I was submitting, but it's unreadable as it display here. I'll try with just plain text:
Imports System
Imports System.Net
Imports System.Windows.Controls
Imports Telerik.Windows.Documents.FormatProviders.OpenXml.Docx
Partial Public Class MainPage
Inherits UserControl
Private WithEvents webClient As WebClient
Public Sub New()
InitializeComponent()
webClient = New WebClient
webClient.OpenReadAsync(New Uri("Rad-RichTextBox-for-Silverlight.docx", UriKind.Relative))
End Sub
Private Sub webClient_OpenReadCompleted(ByVal sender As Object, ByVal e As OpenReadCompletedEventArgs) Handles webClient.OpenReadCompleted
richTextBox.Document = New DocxFormatProvider().Import(e.Result)
e.Result.Close()
End Sub
End Class
Thanks a ton for your reply. That did get rid of errors that I was having.
The page displays, and I put borders around the richtextbox control so that I could see it.
BUT, the richtextbox is empty, no text displays.
This is the line with the filename:
webClient.OpenReadAsync(New Uri("guidelines.docx", UriKind.Relative))
I know that file exists. Do I need to qualify it more with some other kind of path details?
The only other difference I see is that your code "Inherits UserControl" and mine "Inherits Page" because I'm using navigation pages.
That wouldn't matter would it?
Does anything need to change in the .xaml?
Here's my declaration for the richtextbox control:
<telerik:RadRichTextBox Name="radRichTextBox1" Width="700" Background="White" BorderThickness="1" BorderBrush="Black" IsReadOnly="True" />
Thanks again, a ton, for your help!
Did you put the guidelines.docx file in the ClientBin folder?
But, even after putting it there, it still doesn't show.
I did notice that when I run the project from VS 2010, if the file is not in the ClientBin folder, the status bar on FireFox says "Waiting on Localhost...". If the file is there, it says: "Transferring data from Localhost...", but nothing ever shows up.
Terry,
Thanks so much for your help. I finally figured out what my problem was.
For some reason, the "Handles webClient.OpenReadCompleted" statement was missing from the OpenReadCompleted() event.
Added that and it started working perfectly.
Thanks again for all of your help!
Mike T.
Mike, we are very glad you resolved this issue!
Let us know if anything else pops up.
Greetings,
Mike
the Telerik team