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

RadRichTextBox import and export into a file.docx using byte()

10 Answers 683 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Anthony
Top achievements
Rank 1
Anthony asked on 25 Nov 2010, 07:40 AM
As you know I can export the content of RadDocument just 

Dim doc As Byte() = provider.Export(RadRichTextBox.Document)
After I created a wcf service and hosted it. This service gets doc as a parameter and saves its content into a docx file

 Using FileStream As New IO.FileStream("C:\\Temp\\1.docx", IO.FileMode.CreateNew)
            FileStream.Write(file, 0, file.Length)
            FileStream.Close()
        End Using
 Using FileStream As New IO.FileStream("C:\\Temp\\1.docx", IO.FileMode.CreateNew)
            FileStream.Write(file, 0, file.Length)
            FileStream.Close()
        End Using
Using FileStream As New IO.FileStream("C:\\Temp\\1.docx", IO.FileMode.CreateNew)
      FileStream.Write(doc, 0, file.Length)
      FileStream.Close()
End Using

10 Answers, 1 is accepted

Sort by
0
Anthony
Top achievements
Rank 1
answered on 25 Nov 2010, 07:46 AM
It works good... But when using the same wcf service I am trying to get this file as a byte array and return result to a silverlight 4 application....


        Dim bFile As Byte()
        Using FileStream As New IO.FileStream("C:\\Temp\\1.docx", IO.FileMode.Open)
            bFile = New Byte(FileStream.Length) {}
            FileStream.Read(bFile, 0, bFile.Length)
            FileStream.Close()
        End Using

        Return bFile

        Dim bFile As Byte()
        Using FileStream As New IO.FileStream("C:\\Temp\\1.docx", IO.FileMode.Open)
            bFile = New Byte(FileStream.Length) {}
            FileStream.Read(bFile, 0, bFile.Length)
            FileStream.Close()
        End Using

        Return bFile
Dim bFile As Byte()
Using FileStream As New IO.FileStream("C:\\Temp\\1.docx", IO.FileMode.Open)
    bFile = New Byte(FileStream.Length) {}
    FileStream.Read(bFile, 0, bFile.Length)
    FileStream.Close()
End Using
 
Return bFile
I get a byte array in the silverlight, but when I try to convert the array into a RadDocument it calls an exception
Dim provider As New Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.DocxFormatProvider()
 
Dim doc As Telerik.Windows.Documents.Model.RadDocument = provider.Import(bFile)
I don't understand why it happens??????????
0
Anthony
Top achievements
Rank 1
answered on 25 Nov 2010, 07:51 AM
To sum up, I send a RadDocument context as a byte array using wcf and I get it back to silverlight 4 as a byre array.... but I can't do RadDocument = my byte array..... why???? and how I can set RadDocument = *.docx file.... I need to read a docx file and set it in RadRichTextBox????
0
Stanescu Mihai
Top achievements
Rank 1
answered on 25 Nov 2010, 08:35 AM
Hi Anthony, 

I myself am using Stream and it works, 

Try something like this:

Stream stream = dlg.File.OpenRead(); - dlg is an openfiledialog.
                this.richTextBox.Document.LayoutMode = DocumentLayoutMode.Flow;
                this.richTextBox.Document = new DocxFormatProvider().Import(stream);
                
Have no ideea why it doesn't work with FileStream.
0
Ivailo Karamanolev
Telerik team
answered on 25 Nov 2010, 10:02 AM
Hello Stanescu Mihai,

You can make sure all the bytes reach the Silverlight client correctly, and if that's the case, can you please share with us the specifics of the exception you're getting? And yes, using the DocxFormatProvider.Import method to import byte[] is indeed correct so the issue must lie either in something wrong (or unsupported) in the document or in the path between the .docx file and the format provider.

Sincerely yours,
Ivailo
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Anthony
Top achievements
Rank 1
answered on 26 Nov 2010, 02:17 AM
Dim bArray1 As Byte()
Dim ofd As OpenFileDialog = New OpenFileDialog()
If ofd.ShowDialog() Then
    Dim provider1 As Telerik.Windows.Documents.FormatProviders.IDocumentFormatProvider = _
                     Telerik.Windows.Documents.FormatProviders.DocumentFormatProvidersManager.GetProviderByExtension(ofd.File.Extension.ToLower())
                If provider1 IsNot Nothing Then
                    Dim stream As System.IO.Stream = ofd.File.OpenRead()
                    bArray1 = New Byte(stream.Length) {}
                    Dim count As Integer = stream.Read(bArray1, 0, stream.Length)
                    'TextArea.Document = provider1.Import(stream)
 
 
                    TextArea.Document = provider1.Import(bArray1)
                    stream.Close()
                End If
            End If
0
Anthony
Top achievements
Rank 1
answered on 26 Nov 2010, 02:20 AM
When I use this code trying to open a docx file in RadRichTextBox, it call an exception 
System.InvalidOperationException was unhandled by user code
  Message=Invalid data.
System.InvalidOperationException was unhandled by user code
  Message=Invalid data.
it happens when I just use byte(), but using stream it works properly.... Unfortunally, I have to use byte() ... any idea how to make it use byte() ????
0
Anthony
Top achievements
Rank 1
answered on 26 Nov 2010, 03:00 AM
it seems to me that I understand why it happens.... when you get a byte array using provider.Export from your RadRichTextBox.Document

you have one byte.Lenght, but when you save this byte array on the disc and get it again using
Dim stream As System.IO.Stream = ofd.File.OpenRead()
                   bArray1 = New Byte(stream.Length) {}
                   Dim count As Integer = stream.Read(bArray1, 0, stream.Length)

these two byte arrays have different size.... the last one's size bigger than the first one in 1 byte....... what to do???



0
Stanescu Mihai
Top achievements
Rank 1
answered on 26 Nov 2010, 06:55 AM
Well you can convert the bytearray to stream and them import that stream into the document,

    Try this:
                 Stream s = new MemoryStream(byteArray);    //C#
0
Marcel
Top achievements
Rank 1
answered on 04 Sep 2011, 06:43 PM
Hi, 

I have been having a similar problem, in which i have obtained the bytes for a docx and i'm trying to import them. So i have used:

  Stream s = new MemoryStream(byteArray);    
RadDocument document =  new DocxFormatProvider.Import(s); 

But I get the exception "Dir not found" and i dont understand why? 
0
Iva Toteva
Telerik team
answered on 07 Sep 2011, 01:43 PM
Hi Marcellino,

Please find the answer to your question in the other forum thread you posted it.

Best wishes,
Iva
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
RichTextBox
Asked by
Anthony
Top achievements
Rank 1
Answers by
Anthony
Top achievements
Rank 1
Stanescu Mihai
Top achievements
Rank 1
Ivailo Karamanolev
Telerik team
Marcel
Top achievements
Rank 1
Iva Toteva
Telerik team
Share this question
or