System.OutOfMemoryException executing doc.Save(...- Aspose.Words.SaveFormat.Doc)

I’ve been using Aspose.Words .NET for some time now in our application and have recently run into a major issue. I’m generating large documents containing some rather large images that are scaled using the Aspose.Words image scaling functionality. By large document I mean 302 pages per the doc.PageCount property.
I’m not running into problems building the document. Rather, my OutOfMemoryException occurs when doc.Save(…, Aspose.Words.SaveFormat.Doc) is called.
If I switch the SaveFormat to .Pdf the document is generated… with a few formatting issues.
I need to deliver the generated documentation from our application in .Doc format so .Pdf is not an option.
Below is the exception stack trace:

A first chance exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll
System.Transactions Critical: 0 : http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/UnhandledUnhandled exceptionVoyager.vshost.exeSystem.OutOfMemoryException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089Exception of type 'System.OutOfMemoryException' was thrown. 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 System.IO.BinaryWriter.Write(Byte[] buffer)
at x1a62aaf14e3c5909.x681808f408a7efac.DoWrite(BinaryWriter writer)
at x1a62aaf14e3c5909.xddf6304144fd3863.x6210059f049f0d48(BinaryWriter xbdfb620b7167944b)
at x1a62aaf14e3c5909.xfbb3f4be330f4086.xbf2311da8da339cb(BinaryWriter xc83188e30d5f47a5)
at x1a62aaf14e3c5909.xc2d70347f7484382.x5ed090cee2aa9cab(BinaryWriter xc83188e30d5f47a5)
at x1a62aaf14e3c5909.x57b6730bb29ba4a9.x5ed090cee2aa9cab(BinaryWriter xc83188e30d5f47a5)
at x1a62aaf14e3c5909.x7e738ecc9d58b06d.x6210059f049f0d48(BinaryWriter xf4128eeeb72b0b1c, BinaryWriter xc83188e30d5f47a5, Boolean x9c3d5a1ae48b1ea8)
at x9e260ffa1ac41ffa.x163859bfa28558c4.x64048ad4d39c9f2a()
at x9e260ffa1ac41ffa.x163859bfa28558c4.x73a0dd74a28af617()
at x9e260ffa1ac41ffa.x163859bfa28558c4.x8cac5adfe79bc025(x8556eed81191af11 x5ac1382edb7bf2c2)
at Aspose.Words.Document.xf381a641001e6830(Stream xcf18e5243f8d5fd3, String xafe2f3653ee64ebc, SaveOptions xc27f01f21f67608c)
at Aspose.Words.Document.Save(String fileName, SaveOptions saveOptions)
at Aspose.Words.Document.Save(String fileName, SaveFormat saveFormat)
at Voyager.Utils.DocumentUtils.GeneratePreInstallationSurveyWordDoc(Site site, String directory) in C:\Projects\Voyager\Voyager\Utils\DocumentUtils.cs:line 795
at Voyager.UI.Forms.DocumentGenerationForm.GeneratePreInstallationSurveySet() in C:\Projects\Voyager\Voyager\UI\Forms\DocumentGenerationForm.cs:line 80
at Voyager.UI.Forms.DocumentGenerationForm.btnGenerate_Click(Object sender, EventArgs e) in C:\Projects\Voyager\Voyager\UI\Forms\DocumentGenerationForm.cs:line 62
at System.Windows.Forms.Control.OnClick(EventArgs e)
at Infragistics.Win.UltraControlBase.OnClick(EventArgs e)
at Infragistics.Win.Misc.UltraButtonBase.OnClick(EventArgs e)
at Infragistics.Win.Misc.UltraButton.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message & amp; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message & amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message & amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message & amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG & amp; msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at Voyager.UI.Panels.TreePanel.menuItemSiteDocumentation_Click(Object sender, EventArgs e) in C:\Projects\Voyager\Voyager\UI\Panels\TreePanel.cs:line 769
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message & amp; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message & amp; m)
at System.Windows.Forms.ScrollableControl.WndProc(Message & amp; m)
at System.Windows.Forms.ToolStrip.WndProc(Message & amp; m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message & amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message & amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message & amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG & amp; msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Voyager.Program.Main() in C: \Projects\ Voyager\ Voyager\ Program.cs: line 40
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
at System.Activator.CreateInstance(ActivationContext activationContext)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
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 System.IO.BinaryWriter.Write(Byte[] buffer)
at x1a62aaf14e3c5909.x681808f408a7efac.DoWrite(BinaryWriter writer)
at x1a62aaf14e3c5909.xddf6304144fd3863.x6210059f049f0d48(BinaryWriter xbdfb620b7167944b)
at x1a62aaf14e3c5909.xfbb3f4be330f4086.xbf2311da8da339cb(BinaryWriter xc83188e30d5f47a5)
at x1a62aaf14e3c5909.xc2d70347f7484382.x5ed090cee2aa9cab(BinaryWriter xc83188e30d5f47a5)
at x1a62aaf14e3c5909.x57b6730bb29ba4a9.x5ed090cee2aa9cab(BinaryWriter xc83188e30d5f47a5)
at x1a62aaf14e3c5909.x7e738ecc9d58b06d.x6210059f049f0d48(BinaryWriter xf4128eeeb72b0b1c, BinaryWriter xc83188e30d5f47a5, Boolean x9c3d5a1ae48b1ea8)
at x9e260ffa1ac41ffa.x163859bfa28558c4.x64048ad4d39c9f2a()
at x9e260ffa1ac41ffa.x163859bfa28558c4.x73a0dd74a28af617()
at x9e260ffa1ac41ffa.x163859bfa28558c4.x8cac5adfe79bc025(x8556eed81191af11 x5ac1382edb7bf2c2)
at Aspose.Words.Document.xf381a641001e6830(Stream xcf18e5243f8d5fd3, String xafe2f3653ee64ebc, SaveOptions xc27f01f21f67608c)
at Aspose.Words.Document.Save(String fileName, SaveOptions saveOptions)
at Aspose.Words.Document.Save(String fileName, SaveFormat saveFormat)
at Voyager.Utils.DocumentUtils.GeneratePreInstallationSurveyWordDoc(Site site, String directory) in C:\Projects\Voyager\Voyager\Utils\DocumentUtils.cs:line 795
at Voyager.UI.Forms.DocumentGenerationForm.GeneratePreInstallationSurveySet() in C:\Projects\Voyager\Voyager\UI\Forms\DocumentGenerationForm.cs:line 80
at Voyager.UI.Forms.DocumentGenerationForm.btnGenerate_Click(Object sender, EventArgs e) in C:\Projects\Voyager\Voyager\UI\Forms\DocumentGenerationForm.cs:line 62
at System.Windows.Forms.Control.OnClick(EventArgs e)
at Infragistics.Win.UltraControlBase.OnClick(EventArgs e)
at Infragistics.Win.Misc.UltraButtonBase.OnClick(EventArgs e)
at Infragistics.Win.Misc.UltraButton.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message & amp; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message & amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message & amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message & amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG & amp; msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at Voyager.UI.Panels.TreePanel.menuItemSiteDocumentation_Click(Object sender, EventArgs e) in C:\Projects\Voyager\Voyager\UI\Panels\TreePanel.cs:line 769
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message & amp; m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message & amp; m)
at System.Windows.Forms.ScrollableControl.WndProc(Message & amp; m)
at System.Windows.Forms.ToolStrip.WndProc(Message & amp; m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message & amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message & amp; m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message & amp; m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG & amp; msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Voyager.Program.Main() in C: \Projects\ Voyager\ Voyager\ Program.cs: line 40
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
at System.Activator.CreateInstance(ActivationContext activationContext)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

I forgot to mention that I’m running version 9.3.0.0 of the .net 2.0 library.
Steve E.

Hello

Thanks for your inquiry. Could you please create simple application, which will demonstrate the problem on my side? I will check your code and provide you more information.
Best regards,