1
Answer

pdf generation and downloading at button click event

can you please help me to generate and download pdf document on buton click event
 
below code is giving error as path is not legal formate
  1. protected void btnGenerateCertificate_Click(object sender, System.EventArgs e)  
  2. {  
  3. string imageURL = Server.MapPath("../images/logo.png");  
  4. iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL);  
  5.   
  6. //Resize image depend upon your need  
  7. jpg.ScaleToFit(25f, 25f);  
  8. //Give space before image  
  9. //jpg.SpacingBefore = 1f;  
  10. //Give some space after the image  
  11. jpg.SpacingAfter = 1f;  
  12. jpg.Alignment = Element.ALIGN_CENTER;  
  13.   
  14. //Header  
  15. Paragraph parag = new Paragraph("Road Construction Department"new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 15f, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLUE));  
  16. Paragraph paradate;  
  17. Paragraph subparag;  
  18. parag.SpacingAfter = 1f;  
  19. parag.Alignment = Element.ALIGN_CENTER;  
  20.   
  21. subparag = new Paragraph("Govt. of Jharkhand"new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 15f, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK));  
  22. //subparag.SpacingAfter = 1f;  
  23. subparag.Alignment = Element.ALIGN_CENTER;  
  24. string AppId = Session["ApplicantId"].ToString();  
  25. string datetime = DateTime.Now.ToString("dd-MMM-yyyy-H-mm-ss");  
  26. string filename = "Certificate_" + Session["ApplicantId"].ToString() + "_" + datetime + ".pdf";  
  27. FileStream file = new FileStream(Server.MapPath("~/Certificate/") + filename, FileMode.Create, System.IO.FileAccess.Write);  
  28. //save filename in database  
  29. string param = AppId + "~" + filename;  
  30. DataSet ds = BLobj.contractor_registration_Insert_Update_Certificate(connString, param);  
  31. StringWriter sw = new StringWriter();  
  32. HtmlTextWriter hw = new HtmlTextWriter(sw);  
  33. printableArea_IA_IB_IS.RenderControl(hw);  
  34. StringReader sr = new StringReader(sw.ToString());  
  35. Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);  
  36. PdfWriter.GetInstance(pdfDoc, file);  
  37. pdfDoc.Open();  
  38. pdfDoc.Add(jpg);  
  39. pdfDoc.Add(parag);  
  40. pdfDoc.Add(subparag);  
  41. HTMLWorker htmlparser = new HTMLWorker(pdfDoc);  
  42. MemoryStream ms = new MemoryStream();  
  43. htmlparser.Parse(sr);  
  44. pdfDoc.Close();  
  45. file.Close();  
  46. Response.Redirect("Summary_Application1.aspx");  
  47. }
Answers (1)