Free Support Forum - aspose.com

Aspose.PDF Document- HttpResponse and disposing

Hello,
i have recently purchased aspose.pdf OEM…
now, i have a doubt… i’ve searched the documentation, but couldn’t find…
- I’m in asp.net web application
- I have an object Aspose.Pdf.Document
- I call the “save” overloaded method, which saves the document straight to to my HttpResponse

I suppose that Aspose.Pdf.Document is disposable… so, my code is:

  1. HttpContext.Current.Response.Clear();
  2. HttpContext.Current.Response.ClearHeaders();
  3. HttpContext.Current.Response.ClearContent();
  4. string fileName = “pippo.pdf”;
  5. Aspose.Pdf.PdfSaveOptions opzioniSalva = new Aspose.Pdf.PdfSaveOptions();
  6. Aspose.Pdf.ContentDisposition tipoContenuto = Aspose.Pdf.ContentDisposition.Attachment;
  7. documento.Save(Response, fileName, tipoContenuto, opzioniSalva);
  8. HttpContext.Current.Response.AppendHeader(“Content-disposition”, “Attachment; filename=”" + fileName + “”");
  9. HttpContext.Current.Response.ContentType = “Application”;
  10. documento.Dispose();
  11. HttpContext.Current.Response.End();

Now… when i call the save (command #7) it looks like the method internally flush and close the response… so, lines 8-9-10-11 are not executed…
My question is:
is the object somehow internally disposed, inside the “save” method? or… should i rather save the document to a MemoryStream, allowing me to dispose the PdfDocument (and the stream) BEFORE i end the response?

Many thanks

Andrea.

Hi Andrea,


Thanks for using our products.

Please note that once the Document.Save or Pdf.Save methods are called, the Pdf and Document objects get disposed. Please try saving the resultant file into MemoryStream object and write the MemoryStream contents to HttpResponse object. Please check out the code snippet shared over Add Image code example for better understanding.

In the event of any further query, please feel free to contact. We are sorry for your inconvenience.
codewarior:
Please note that once the Document.Save or Pdf.Save methods are called, the Pdf and Document objects get disposed.

so, you say that the document it's disposed inside the "save" method call? i don't have to dispose it manually?

codewarior:

Please try saving the resultant file into MemoryStream object and write the MemoryStream contents to HttpResponse object.

sorry.... i don't understand... i already tried to save it to a memory stream, then i write the stream to the http response, then i dispose the document, dispose the stream, and close the response.... it works fine..

codewarior:

Please check out the code snippet shared over Add Image code example for better understanding.

i know that kind of code, i often use it for other objects, but BEFORE the response end, i always call the dispose method of the object...

i can confirm you that, after the save method, the object has not been disposet yet

edit: i mean, after the save document to memorystream

any news? i gotta deploy the app…

thanks

liscio:
i can confirm you that, after the save method, the object has not been disposet yet

edit: i mean, after the save document to memorystream

Hi,

Yes you are correct. When you are saving the resultant PDF in MemorStream object, you need to close the MemorStream instance. However please note that when you call Document.Save or Pdf.Save method, the Pdf object is closed. In case I am still missing something or you have any further query, please feel free to contact.

We are sorry for the inconvenience.


yeah, i know how to dispose the stream… but, after saving the document to stream, the DOCUMENT is not disposed… should i dispose the DOCUMENT too?

Hi Andrea,

Thanks for your inquiry.

You are right, after calling the DocumentObject.Save method, the Document object does not dispose. You need to call DocumentObject.Dispose method explicitly for disposing the document object.

Please feel free to contact support in case you need any further assistance.

Thanks & Regards,

rashid.ali:
You are right, after calling the DocumentObject.Save method, the Document object does not dispose. You need to call DocumentObject.Dispose method explicitly for disposing the document object.

hi...
i know it, it's easy to see, in debug...
In fact, the original, main question, was:
If I call the "Save" to HttpResponse (it's the 4th overload, if i'm right) the response is ENDED inside YOUR save method... so, is the Document object disposed internally in that specific "Save" method?
Because, since the HttpReponse ends inside YOUR method, i CANNOT dispose the object, after calling that method... because the server-side code ENDS with the response... if you look at my code, in the top of this thread, the code lines following the "save" method, are NOT executed, so the dispose of the Document object it's not called.

Is it clear?

Hi Andrea,


Thanks for sharing your comments.

Please note that I have logged this problem as PDFNEWNET-33671 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. Please be patient and spare us little time. We are sorry for your inconvenience.

Hi Andrea,


Thanks for your patience.

We have further worked on previously reported issue and a new property is added int PdfSaveOptions:
bool CloseResponse { get; set ; } and it will become available with the release of Aspose.Pdf for .NET 7.4.0.

This property defines that either the response be automatically closed after Save() method call. (Close, If CloseResponse == true)
By default CloseResponse = true.

Please make following changes in your code to prevent reponse from closing:

[C#]

HttpContext.Current.Response.Clear();<o:p></o:p>

HttpContext.Current.Response.ClearHeaders();

HttpContext.Current.Response.ClearContent();

string fileName = "pippo.pdf";

Aspose.Pdf.PdfSaveOptions opzioniSalva = new Aspose.Pdf.PdfSaveOptions();

opzioniSalva.CloseResponse = false;

Aspose.Pdf.ContentDisposition tipoContenuto = Aspose.Pdf.ContentDisposition.Attachment;

documento.Save(Response, fileName, tipoContenuto, opzioniSalva);

HttpContext.Current.Response.AppendHeader("Content-disposition", "Attachment; filename=\"" + fileName + "\"");

HttpContext.Current.Response.ContentType = "Application";

documento.Dispose();

HttpContext.Current.Response.End();

The issues you have found earlier (filed as PDFNEWNET-33671) have been fixed in Aspose.Pdf for .NET 7.4.0.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.