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.
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.
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.
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");
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.
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
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.
Hi,
I have attached a simple Console Application project so you can reproduce my problem.
When it runs, it provides two options:
MS Word 97 - 2003 Document
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.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.