I can successfully create/generate PDF file without any issue. But when I try to download it after generation, the execution gets redirected to,
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
On button's click event, I create PDF file which works as expected as below.
using System.Data;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Net;
protected void Button2_Click(object sender, EventArgs e)
{
try
{
string htmlContent = "<div> PDF Code </div>"; // you html code (for example table from your page)
Document document = new Document();
string FileName = Guid.NewGuid().ToString();
PdfWriter.GetInstance(document, new FileStream("C:\\...\\...\\PDF\\" + FileName + ".pdf", FileMode.Create));
document.Open();
HTMLWorker worker = new HTMLWorker(document);
worker.Parse(new StringReader(htmlContent));
document.Close();
//To download same PDF I write below code
Response.Clear();
string pdfPath = Server.MapPath(@"~\PDF\" + FileName + ".pdf");
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(pdfPath);
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
////Response.End();
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
This code doesn't throw any exception rather it gets redirected to above mention method. Please help.
What I want is with button's click event, I want to generate PDF and download the same. Tried so many things but getting no success.