Free Support Forum - aspose.com

Saving workbook as AsposePdf using memory stream throws ObjectDisposedException

Hi,

I'm using Aspose.Cells v4.4.0.24 and Aspose.Pdf v3.6.1.0.

When I try to save a workbook as a pdf file using a memory stream I get the following exception: System.ObjectDisposedException: Cannot access a closed Stream.

Here is part of the stack trace:

[ObjectDisposedException: Cannot access a closed Stream.]
   System.IO.__Error.StreamIsClosed() +56
   System.IO.MemoryStream.Seek(Int64 offset, SeekOrigin loc) +2030368
   Aspose.Cells.Workbook.Save(Stream stream, FileFormatType fileFormatType) +230

Here is the code I'm running:

Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();

MemoryStream modelStream = new MemoryStream();

workbook.Open(@"c:\Test_File.xls", FileFormatType.Excel2003);

workbook.Save(modelStream, Aspose.Cells.FileFormatType.AsposePdf);

Attached you'll find the xls file I've been using for this.

Thanks,

Ryan


Hi Ryan,

Could you try the attached version of Aspose.Cells. I have tested the code using your template file and it works fine.

Thank you.

Thanks that fixed the issue with the closed memory stream! However, now when I add a graph to the XLS file and try the same code I get another exception:

[ArgumentException: Parameter is not valid.]
System.Drawing.Bitmap..ctor(String filename) +393925
Aspose.Pdf.Figure.ኙ.ኚ(٪ ቱ, Int32 ኛ, ImageFileType ٮ, String ٯ, String ٰ, String ٱ, Int32 ٲ, Int32 ٳ, Boolean ٴ, Boolean ٵ, Boolean ኜ, Byte[] ٶ, Stream ٷ) +3256
Aspose.Pdf.Figure.٪.٬(Image ٭, ImageFileType ٮ, String ٯ, String ٰ, String ٱ, Int32 ٲ, Int32 ٳ, Boolean ٴ, Boolean ٵ, Byte[] ٶ, Stream ٷ) +496
Aspose.Pdf.Xml.ቪ.ቯ(Pdf ӟ, Image ٭) +1078
Aspose.Pdf.Xml.ᖷ.ᖹ(Pdf ༲, Paragraph ո, Single ټ) +261
Aspose.Pdf.Xml.ᙬ.᙮(Pdf ӟ, Section ӧ, Table Ө, Row ө, Cell Ӣ, Ԁ ӫ) +4391
Aspose.Pdf.Xml.ᙡ.ᙤ(Pdf ӟ, Section ӧ, Table Ө, Row ө, Ԁ ӫ) +2063
Aspose.Pdf.Xml.ᖓ.ᖕ(Pdf ӟ, Section ӧ, Table Ө, Ԁ ӫ, Boolean ӭ) +12493
Aspose.Pdf.Xml.ᖇ.ᖊ(Pdf ӟ, Section Ӡ, Ԁ ӫ) +7711
Aspose.Pdf.Xml.ݏ.ၳ(Pdf ӟ) +3129
Aspose.Pdf.Xml.᣽.ᤂ(ԁ ӯ, Pdf ӟ) +756
Aspose.Pdf.Pdf.Save(Stream stream) +440

Attached is the updated xls file.

Here is all of the code I'm using for this test:

Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();

MemoryStream modelStream = new MemoryStream();

MemoryStream pdfStream = new MemoryStream();

workbook.Open(@"c:\Test_File.xls", FileFormatType.Excel2003);

workbook.Save(modelStream, Aspose.Cells.FileFormatType.AsposePdf);

modelStream.Seek(0, SeekOrigin.Begin);

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(modelStream);

Aspose.Pdf.Pdf pdf1 = new Aspose.Pdf.Pdf();

pdf1.BindXML(xmlDoc, null);

pdf1.Save(pdfStream); //Exception caused here!

Hi,

It's caused by that the image file could not be found in the memory stream. We save them to the temp directory. Please save them to disk then convert it to pdf file. We will look into this issue.

Workbook workbook = new Workbook();
workbook.Open(@"F:\FileTemp\Test_File.xls");

workbook.Save(@"F:\FileTemp\dest.xml", FileFormatType.AsposePdf);
Console.WriteLine(DateTime.Now);
Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();
pdf.IsTruetypeFontMapCached = true;
pdf.BindXML(@"F:\FileTemp\dest.xml", null);
Console.WriteLine(DateTime.Now);pdf.Save(@"F:\FileTemp\dest.pdf");

Hmmm, so do you have any best practices on using this approach with multiple concurrent users? How will I ensure that my images from one user won't get overwritten by images from another user? Can I control the image names?

Hi,

We will look into this issue. Thanks for your patience.

Hi,

Please try this fix.

We have found the problem of converting xml stream to pdf file.

As your metioned muti threads, we will look into it.

Thanks - you guys are awsome!