Free Support Forum - aspose.com

Weird issue when loading document through stream

Hello,

I have encountered a very weird issue when opening an ASPOSE document using a stream. when I use the code below and re-save the byte array, my file becomes corrupted. If I comment out the line below the stream (the Aspose document) then the file works great. I initially was loading this file through a manifest stream using an embedded resource but now I have changed my tactics to move the process. I am just posting this to see if anyone else can duplicate the process.


// initialize the output buffer
byte[] buffer = null;
using (FileStream stream = File.OpenRead(templateFilePath))
{
Aspose.Words.Document doc = new Aspose.Words.Document(stream);

buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
}

using (System.IO.FileStream st = System.IO.File.Create(savePath))
{
st.Write(buffer, 0, buffer.Length);
st.Close();
}

Hi Nizar,


Thanks for your inquiry. Your code does instantiate a Document object but it doesn’t use it anywhere. I would suggest you please read the following articles:

http://www.aspose.com/docs/display/wordsnet/Opening+from+a+Stream
http://www.aspose.com/docs/display/wordsnet/Saving+to+a+Stream

I hope, this helps.

Best Regards,

Thank you and I understand I am not using the document for anything. The actual original code does a doc.MailMerge.Execute(dtDataSource)


I simplified the code because of the issue I was experiencing. I just posted this to see if you can duplicate the issue with the supplied code (file becomes corrupt just by opening the stream with Aspose).

Hi Nizar,


Thanks for your inquiry. But, you will observe the same problem if you remove the statement initializing Aspose.Words’ Document object from your code. Alternatively, you can achieve the same by using the following code snippet:

using (MemoryStream stream
= new MemoryStream())

{

Document srcDoc = new Document(@"C:\Temp\ConferenceAttendanceReporting.doc");

srcDoc.Save(stream, SaveFormat.Doc);

stream.Position = 0;

Document dstDoc = new Document(stream);

dstDoc.Save(@"C:\Temp\out.doc");

stream.Close();

}


Please let me know if I can be of any further assistance.

Best Regards,