I am saving an Aspose.Words document as a PDF bytes and later, reading the PDF’s bytes into a MemoryStream so a new Aspose Document() object can be instantiated. However, when I instantiate the Document using the constructor that accepts a Stream, it instantiates the Document with three pages in the Document.Pages property, but all three pages are null.
//get bytes from file/ database location
using(var stream = new MemoryStream(bytes))
{
var document = new Document(stream, false);
}
How do we get the pages to not be null?
Hi Kieth,
Thanks for your inquiry. We will appreciate it if you please share your complete code and document here. We will look into it and guide you accordingly.
We are sorry for the inconvenience caused.
Best Regards,
I’ve narrowed down what does and does not cause this issue.
We have code that looks like this:
-----
Aspose.Pdf.Document document;
byte[] pdfBytes = /* get PDF bytes from file */
using(var stream = new MemoryStream(pdfBytes))
{
document = new Aspose.Pdf.Document(stream, false);
}
------
In this case, the stream is disposed after the document is created, and I get the null pages.
If I instead write this:
-------
Aspose.Pdf.Document document;
byte[] pdfBytes = /* get PDF bytes from file */
document = new Aspose.Pdf.Document(stream, false);
-------
In this case, everything works fine.
However, we need to make sure that we clean up unmanaged resources. Will Aspose dispose of the stream when the document is disposed?
Hi Kieth,
Thanks for sharing the details.
In order to release all resources, you may consider closing Stream object and also use doc.Dispose(); method to release all resources occupied by Document instance.
codewarior:
Hi Kieth,
Thanks for sharing the details.
In order to release all resources, you may consider closing Stream object and also use doc.Dispose(); method to release all resources occupied by Document instance.
So, I tested this, and Document does not dispose of the stream when the document is disposed.
I could wait until I'm completely done with the Document to close the stream, but this begs the question- why do I have to do this? It might be a while before I am done with the document, and that stream could be a file, meaning the file is locked for that duration. Is there a way I can get the Document to read the entire stream so I can safely dispose of it before I am done using the Document?
Hi Kieth,
Can you please share some sample application/project, so that we can test the scenario in our environment. We are sorry for this inconvenience.