Read/save stream document

Hi,
I was trying to read a document content from a stream, do some replacement, and then save the document to another stream (because the original stream is un-expandable). Here is my code segment:

string mFilePath = MapPath(".\test.docx");

byte[] Content = `System.IO`.File.ReadAllBytes(mFilePath);

System.IO.Stream docStream = new `System.IO`.MemoryStream(Content);

Aspose.Words.Document doc = new Aspose.Words.Document(docStream);

// doc.Range.Replace()

System.IO.Stream ResultDoc = new `System.IO`.MemoryStream();
doc.Save(ResultDoc, SaveFormat.Docx);

Int32 length = ResultDoc.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(ResultDoc.Length);
Byte[] newContent1 = new Byte[length];
int nRead = ResultDoc.Read(newContent1, 0, length);
string mFilePath = MapPath(".\\test.docx");
byte[] Content = System.IO.File.ReadAllBytes(mFilePath);
System.IO.Stream docStream = new System.IO.MemoryStream(Content);
Aspose.Words.Document doc = new Aspose.Words.Document(docStream);
// doc.Range.Replace()
System.IO.Stream ResultDoc = new System.IO.MemoryStream();
doc.Save(ResultDoc, SaveFormat.Docx);
Int32 length = ResultDoc.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(ResultDoc.Length);
Byte[] newContent1 = new Byte[length];
int nRead = ResultDoc.Read(newContent1, 0, length);

The arry newContent1 is supposed to have the same content as the array Content but I kept getting nothing. I have attached the test document. Please help.
Thanks.

I changed it to use expandable stream and now it is like:

string mFilePath = MapPath(".\\test.docx");
byte[] Content = File.ReadAllBytes(mFilePath);
Stream docStream = new MemoryStream();
docStream.Write(Content, 0,
Content.Length);
Document doc = new Document(docStream);
// doc.Range.Replace()
doc.Save(docStream, SaveFormat.Docx);
Int32 length = docStream.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(docStream.Length);
Byte[] newContent1 = new Byte[length];
int nRead = docStream.Read(newContent1, 0, length);

My purpose was to convert the stream to byte array newContent1, which I think should be the same as the array Content because the docStream was not changed. The length is way less then the Int32.MaxValue.
But in the result, the byte array newContent1 was different with the array Content. What happened in the statement doc.Save(docStream, SaveFormat.Docx)?
Thanks.

Hi Donghu,

Thanks for your inquiry. Please rewind the stream position back to zero so it is ready for next reader. Please add the following line of code:

docStream.Position = 0;
int nRead = docStream.Read(newContent1, 0, length);

I hope, this helps.

Best regards,

Thanks for your reply. I added the code:

docStream.Position = 0;

Now the array newContent1 does have the same length of data as the array Content. But the actual data in the two arrays are different.
If I save the doc to a local word document and then read it to an array, like

doc.Save("result.docx");
byte[] result = File.ReadAllBytes("result.docx")

Then the result seems to be good. I am confused… In my application, I don’t want to the save to local step.

Hi Donghu,

Thanks for your inquiry. I think, you can use the following simple code to be able to reload document to a new Document instance by avoiding a save to disk operation.

Document docx = new Document(@"C:\Temp\test.docx");
// do some editing
docx.Range.Replace("test", "good", false, false);
MemoryStream docxStream = new MemoryStream();
docx.Save(docxStream, SaveFormat.Docx); 
Document outDocx = new Document(docxStream);
outDocx.Save(@"C:\Temp\out.docx"); 

Best regards,

Thanks for your reply.
What I wanted to do is to, after editing the document, save the binary content to database. So converting the memorystream to byte array is a must. I just couldn’t get it to work. :frowning:

Hi,

Thanks for your inquiry. You can use MemoryStream.ToArray method to get byte array of stream as follows:

byte[] byteArray = docxStream.ToArray();

Moreover, please refer to the following article to learn how to load and save a document to database

I hope, this helps.

Best regards,

I actually tried the ToArray() method and didn’t have luck.

Please look at the following code:

byte[] docArray = File.ReadAllBytes(mFilePath);
Stream docStream = new MemoryStream(docArray);
Document doc = new Document(docStream);

After many trials, I have found that, for .docx document, the content in docStream is different from the array docArray. However, everything works fine for Word 97-2003 document (the .doc file).

As for the “How to load and save a document to database” at

It has the same problem. It only works for .doc file, not .docx file. I think this needs to be fixed.

BTW, I have tried the Aspose.Words.dll from both the net2.0 and net3.5_ClientProfile folder. Is there a net4.0 or net4.5 version available?

Thanks.

Hi,

Thanks for the additional information. To ensure a timely and accurate response, it would be great if you please create a standalone/runnable simple application (for example a Console Application project) that helps me reproduce your problem on my end and attach it here for testing. As soon as you get this simple application ready for me, I’ll start investigation into your issue and log an issue in our issue tracking system.

Secondly, Aspose.Words does not currently offer DLL specifically written for .Net Framework 4.0 or 4.5. Please use the assembly from .NET 2.0 or net3.5_ClientProfile folder.

Best regards,

Hi,
I have attached a simple Console Application project so you can reproduce my problem.
When it runs, it provides two options:

  1. MS Word 97 - 2003 Document
  2. MS Word Document.

If you enter 1, it runs through without any problem and generates a test_doc_Out.doc document.
But when you enter 2, it shows errors.
Please try it and let me know if it helps for your investigation.
Thanks.

Hi,

Thanks for your inquiry. Please do the following change in your code to fix this issue:

byte[] Content = File.ReadAllBytes(mFilePath);
Stream docStream = new MemoryStream();
docStream.Write(Content, 0, Content.Length);
Document doc = new Document(docStream);
// doc.Range.Replace()
docStream = new MemoryStream();
if (choice == "1")
    doc.Save(docStream, SaveFormat.Doc);
else
    doc.Save(docStream, SaveFormat.Docx);
Int32 length = docStream.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(docStream.Length);

I hope, this helps.

Best regards,

It worked. Thanks a lot.