Regarding Aspx tp Pdf Conversion

Last Reply 8 days ago By dharmendr

Posted 8 days ago

Hi,

 

I am trying to convert aspx page to pdf. The example given by you is working only for first time. On sebsequent runs, it is ging me an error. 

Exception thrown: 'System.Threading.ThreadAbortException' in mscorlib.dll

protected void btnExport_Click(object sender, EventArgs e)
{
try
{
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();

//Response.Write(pdfDoc);
HttpContext.Current.Response.Write(pdfDoc);
Response.Flush();
Response.End();
//HttpContext.Current.ApplicationInstance.CompleteRequest();
//HttpApplication.CompleteRequest();
}
catch (Exception ex)
{
if (ex is System.Threading.ThreadAbortException)
{
//do nothing
System.Diagnostics.Debug.WriteLine("In exception");
}

}
//HttpContext.Current.Response.End();
//HttpContext.Current.Response.Flush(); // Sends all currently buffered output to the client.
//HttpContext.Current.Response.SuppressContent = true; // Gets or sets a value indicating whether to send HTTP content to the client.
//HttpContext.Current.ApplicationInstance.CompleteRequest(); // Causes ASP.NET to bypass all events and filtering in the HTTP pipeline chain of execution and directly execute the EndRequest event.
}

 

You are viewing reply posted by: dharmendr 8 days ago.
Posted 8 days ago
amitv says:
Have you tried to execute the program twice or refresh the page.

Already checked with what you explained. It is working.

try to create another project and check the code.