Initialize RichTextBox with docx file

10 posts, 0 answers
  1. Terry
    Terry avatar
    11 posts
    Member since:
    Jul 2010

    Posted 10 Aug 2010 Link to this post

    I am trying to do something that I think should be pretty simple, but I'm not having any success.  

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

  2. Ivailo Karamanolev
    Admin
    Ivailo Karamanolev avatar
    276 posts
    Member since:
    Sep 2012

    Posted 12 Aug 2010 Link to this post

    Hello Terry,

    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

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

  3. Download a free trial of UI for WPF
  4. Mike
    Mike avatar
    15 posts
    Member since:
    Jul 2010

    Posted 20 Aug 2010 Link to this post

    Any chance that someone has (or can) converted this project to VB?

    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.

  5. Terry
    Terry avatar
    11 posts
    Member since:
    Jul 2010

    Posted 20 Aug 2010 Link to this post

    Mike,

    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

     

  6. Terry
    Terry avatar
    11 posts
    Member since:
    Jul 2010

    Posted 20 Aug 2010 Link to this post

    Mike,

    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

  7. Mike
    Mike avatar
    15 posts
    Member since:
    Jul 2010

    Posted 20 Aug 2010 Link to this post

    Terry,
    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:
    Snippet

    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:
    Snippet
    <telerik:RadRichTextBox Name="radRichTextBox1" Width="700" Background="White" BorderThickness="1" BorderBrush="Black" IsReadOnly="True" />


    Thanks again, a ton, for your help!

  8. Terry
    Terry avatar
    11 posts
    Member since:
    Jul 2010

    Posted 20 Aug 2010 Link to this post

    Mike,

    Did you put the guidelines.docx file in the ClientBin folder?

  9. Mike
    Mike avatar
    15 posts
    Member since:
    Jul 2010

    Posted 20 Aug 2010 Link to this post

    Doh!  At first I didn't.

    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.

  10. Mike
    Mike avatar
    15 posts
    Member since:
    Jul 2010

    Posted 22 Aug 2010 Link to this post

    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.

  11. Mike
    Admin
    Mike avatar
    640 posts
    Member since:
    Sep 2012

    Posted 24 Aug 2010 Link to this post

    Thanks Terry,
    Mike, we are very glad you resolved this issue!
    Let us know if anything else pops up.

    Greetings,
    Mike
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Back to Top
Download a free trial of UI for WPF