We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

ObjectDisposedException

I am trying to load and save a Pdf document using a stream. I am unclear on the requirements around disposing of the stream that was used to create the document after the document is created. I get inconsistent behavior for different types of streams. For example, the code below that uses a FileStream works without any issues. However, the second example that uses a MemoryStream throws an ObjectDisposedException (“Cannot access a closed stream”) when the file is being saved. If I move the save code inside of the using statement, things work ok. Why are they different? Do I need to avoid disposing of the MemoryStream until the Pdf Document object is disposed? Will disposing of the Pdf Document also dispose my stream? Thanks.

    public void DisposableTestFileStream()
    {
        Document document;
        using (var stream = File.OpenRead("Test.pdf"))
        {
            document = new Document(stream);
        }
        document.Save("Result.pdf");
    }

    public void DisposableTestMemoryStream()
    {
        Document document;
        var bytes = File.ReadAllBytes("Test.pdf");
        using (var stream = new MemoryStream(bytes))
        {
            document = new Document(stream);
        }
        document.Save("Result.pdf");
    }

@mdesousa75

Thanks for contacting support.

When you use FileStream in using statement, you access stream of a file from physical location at the disk and Document.Save() method does not throw any exception (even outside the using statement), because file is present at physical location.

Whereas, while working with MemoryStream object, it reads a file and stores it in RAM. So after when its object is disposed, the reserved memory also gets free, which is why Document.Save() method raises an exception.

Yes, in order to work with MemoryStream, you need to use Document.Save() method as long as stream is available and not disposed.

Furthermore, disposing the document will dispose all the objects which were used in it (e.g Fonts, Annotations, Paragraphs, etc) but it will not dispose the stream through which it has been initialized. In case of any further assistance, please feel free to contact us.

Ok, thanks. I’ll have to say that working with the Aspose.Pdf.Document object as an IDisposable can be very difficult… and the additional overhead of having to keep the MemoryStream around after the document is created makes things even harder.
It would be very nice if the Document object were not disposable… just like Aspose.Words.Document. It is much easier.
At a minimum, it would be nice if the Aspose.Pdf.Document constructor could consume the stream and allow me to dispose it… I’m actually not sure why it would still need to reference it after it has created its internal model.

Thanks!

@mdesousa75

Thanks for writing back.

We have communicated your comments to our product team and as soon as we receive some feedback from their side, we will let you know. Please spare us little time.