Calling Save() on a Aspose.Word.Document throws a System.NotSupportedException

Hi there.

I’m having some issues with using the Aspose.Word.Document class agains a particular Word document.

Briefly summarized, what I do is create a Document based on a MemoryStream, call Range.Replace on the document and call Save() with a reference back to the original MemoryStream.

With certain Word documents, the call to Save() causes a System.NotSupportedException(See the end of this message). The Exception proviedes me with little information as to why the exception occurs, and thus I’m finding quite tricky to figure out what is wrong with the document.

If I instead save the Document into a new MemoryStream, the Save() call exectues without error. However, I imaginge that if I do this, there will in effect be two Streams in memory at the same time and thus doubling the amount off memory used.

I’ve attached a Zip file containing a Visual Studio project that illustrates the problem, including two Word documents (On that fails, and one that does not) There are comments in the code that describes the problem.

Regards

Stian

System.NotSupportedException was unhandled
Message="Memory stream is not expandable."
Source="mscorlib"
StackTrace:
at `System.IO`.__Error.MemoryStreamNotExpandable()
at System.IO.MemoryStream.set_Capacity(Int32 value)
at System.IO.MemoryStream.EnsureCapacity(Int32 value)
at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at x6c95d9cf46ff5f25.x0d299f323d241756.x3ad8e53785c39acd(Stream x23cda4cfdf81f2cf, Stream xedc894794186020d)
at xb92b7270f78a4e8d.xd8c3135513b9115b.x25b763a23176450f(MemoryStream x23cda4cfdf81f2cf, Boolean x1e38b61ec7dc13de, Int32& xc256571e5bb4bd01)
at xb92b7270f78a4e8d.xd8c3135513b9115b.x25b763a23176450f(MemoryStream x23cda4cfdf81f2cf, Boolean x1e38b61ec7dc13de)
at xb92b7270f78a4e8d.xd8c3135513b9115b.xe32d771fa0f8a73f(xe7be411416cfcd54 x630baaeb4d769680, xd686a7cfdb7bddb2 x5e4607c99a2c3e96)
at xb92b7270f78a4e8d.xd8c3135513b9115b.x0acd3c2012ea2ee8(Stream xcf18e5243f8d5fd3)
at x9e260ffa1ac41ffa.x163859bfa28558c4.x8cac5adfe79bc025(x8556eed81191af11 x5ac1382edb7bf2c2)
at Aspose.Words.Document.xf381a641001e6830(Stream xcf18e5243f8d5fd3, String xafe2f3653ee64ebc, SaveFormat xd003f66121eb36a0)
at Aspose.Words.Document.Save(Stream stream, SaveFormat saveFormat)
at ConsoleApplication1.Program.RunTestBasedOnMemoryStream(String fileName) in D:\Visual Studio 2005\Projects\AsposeSupport\ConsoleApplication1\Program.cs:line 50
at ConsoleApplication1.Program.Main(String[] args) in D:\Visual Studio 2005\Projects\AsposeSupport\ConsoleApplication1\Program.cs:line 19
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Hi
Thanks for your request. Try to use the following code.

// get file bytes array.
byte[] file = File.ReadAllBytes(@"225_96594_stian\test_file_that_fails.doc");
using (MemoryStream stream = new MemoryStream(file))
{
    Document doc = new Document(stream);
    doc.Range.Replace("{document_title}", "min tittel", false, false);
    doc.Save(stream, SaveFormat.Doc);
    // save stream to file.
    FileStream strm = new FileStream(@"225_96594_stian\out.doc", FileMode.OpenOrCreate);
    stream.WriteTo(strm);
}

Also I have used the latest version of Aspose.Words. You can get the latest version here:
https://releases.aspose.com/words/net
Best regards