I have created a WCF-Service which inserts content into bookmarks in a word document.
In short, the service performs the following actions (some code is intentionally excluded from the example):
// convert incoming array of bytes (the word document) to an aspose document
var fromMemoryStream = new MemoryStream(docByteArray);
var asposeDocument = new Document(fromMemoryStream);
// insert content into bookmarks
…
// save the aspose document to a memory stream. Save as a docx document
var toMemoryStream = new MemoryStream();
asposeDocument.Save(toMemoryStream, SaveFormat.Docx);
// return the array of bytes
return toMemoryStream.GetBuffer();
My client application receives the returned byte array and writes it to disk. Then it opens the document in Word 2007. Word informs me that there is something wrong with the contents of the document, and it cannot be displayed. It then asks me if I want to fix the contents. I click yes, and the document is displayed.
Is there a bug in the save method when I try to save a document using the docx format? The application is working properly when I work with ordinary word documents in the .doc format. Even when I don’t insert the content into the bookmarks, but just converts the byte array to and from an Aspose word Document object, Word gives me the error message. Therefore, it seems to me that Aspose is somehow making the docx file corrupt.
I hope you are able to help me in solving this issue.
Thanks for your inquiry. Could you please attach your input and output documents here for testing? I will check the issue and provide you more information.
Best regards,
Thank you for your quick reply. I have sent you some example documents in an email. I hope you are able to find out what is going wrong with the docx document.
Thank you for additional information. The problem is causes by GetBuffer method. It adds extra empty bytes at the end of the array. The solution is very simple. Just use ToArray method instead of GetBuffer. Please see the following code:
// Return the array of bytes
return toMemoryStream.ToArray();
// After load stream data
Document mydocx = new Document(StreamData)
// I must create a temporary
MemoryStream myStream = new MemoryStream()
// Do everything...
// At the Save, I did
mydocx.Save(myStream, docx) //Importance!
// and get the save data
StreamData = myStream;
// I get no error from "StreamData"
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.