We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Converting RichTextBoxAdv document to .doc is saving as blank pages?

I'm using a RichTextBoxAdv to display .doc/.docx files for editing within my Silverlight application. After manipulation is complete, I'm attempting to export that edited copy back to my file system (or server).
In debug mode, when stepping through the code I'm successfully converting the document into a MemoryStream object. Or so it seems.
However, when I attempt to extract the byte array of the object from that memory stream and upload that byte array as a document to the server it ends up being a blank .doc/.docx document of several blank pages.

Am I doing something wrong in the conversion? Here's my code:

 wordDoc = xmlDocContainer.FindName("wordDocRichTextBox") as Syncfusion.Windows.Tools.Controls.RichTextBoxAdv;
                DocxExporting.ConvertToDocument(wordDoc.Document, stream, "." + extension);

Then I'm extracting the byte array for future upload to the server and holding it in memory here:

 private static bool OpenFile(Stream stream, string ext)
        {
            bool result = false;
            try
            {
                App.FilesToUpload = new List<KTFileUpload>();
                App.FilesToUpload.Add(new KTFileUpload() { File = new byte[stream.Length], Ext = ext.TrimStart('.') });
                int numBytesToRead = (int)stream.Length;
                int numBytesRead = 0;
                while (numBytesToRead > 0)
                {
                    // Read may return anything from 0 to numBytesToRead.
                    int n = stream.Read(App.FilesToUpload[0].File, numBytesRead, numBytesToRead);

                    // Break when the end of the file is reached.
                    if (n == 0)
                        break;

                    numBytesRead += n;
                    numBytesToRead -= n;
                }
                result = true;
            }
            catch(Exception ex) //once we setup logging capability grab the exception.
            {
                App.ShowErrorDialog("Unable to open file. File may be locked or too large.");
            }

            return result;
        }

3 Replies

MV Michael Velasquez January 17, 2014 04:13 PM UTC

Just some quick further information:

When I finish the OpenFile method, the byte array that I've built from the stream is 49152 positions in length, yet every position is empty with a value of "0".


MV Michael Velasquez January 17, 2014 05:27 PM UTC

It actually looks like I needed an FileStream rather than simply a MemoryStream.

Thanks.


SK Sathish K Syncfusion Team January 21, 2014 12:17 PM UTC

Hi Michael,

 

Thank you for your interest in Syncfusion products.

 

When the code “DocxExporting.ConvertToDocument(wordDoc.Document, stream, "." + extension);” gets executed, the file pointer is moved to the end of file location. So while trying to read the stream data it starts reading from end of file location. So only you are unable to read the stream and get the value 0.

To overcome this problem just set stream position to 0 using the code, “stream.Position = 0”. Here is the modified code snippet.

 

while (numBytesToRead > 0)
{

//Modified code.

stream.Position = 0;
// Read may return anything from 0 to numBytesToRead.
int n = stream.Read(App.FilesToUpload[0].File, numBytesRead, numBytesToRead);
//Your remaining code

}

 

For your reference, we have prepared a sample to illustrate the same. Please find the attached sample and let us know if this helps you.

 

Please let us know if you need any further assistance.

 

Regards,

Sathish



Sample_f065af06.zip

Loader.
Live Chat Icon For mobile
Up arrow icon