Ankit Kumar

Ankit Kumar

  • NA
  • 159
  • 46.6k

Problem with iTextSharp

Oct 11 2017 1:23 AM
Dear all,
 
i am using iTextSharp to export my C# gridview data to PDF but the problem is file is getting downloaded but it's not being downloaded with data and in proper pdf format.
Kindly guide me.
 
below is my action method and i am calling it from ajax request, There is no view for that action method, the data is coming in datatable but file is not getting created correctly.
 
Here is code
  1. public FileStreamResult exporttopdf(int empid, int branchid, int departmentid, int designationid, string selectedmonth, string year)  
  2. {  
  3. GridView gvExportData = new GridView();  
  4. DataTable Attendance = new DataTable();  
  5. MemoryStream workStream = new MemoryStream();  
  6. using (SqlConnection connection = DLConnection.getConnectionInstance())  
  7. {  
  8. if (year == "")  
  9. {  
  10. year = DateTime.Now.Year.ToString();  
  11. }  
  12. SqlParameter[] parameter = new SqlParameter[6];  
  13. parameter[0] = new SqlParameter("@employeeid", empid);  
  14. parameter[1] = new SqlParameter("@BranchId", branchid);  
  15. parameter[2] = new SqlParameter("@DepartmentId", departmentid);  
  16. parameter[3] = new SqlParameter("@DesignationId", designationid);  
  17. parameter[4] = new SqlParameter("@NameOfTheMonth", selectedmonth);  
  18. parameter[5] = new SqlParameter("@year", year);  
  19. SqlDataReader dr;  
  20. dr = SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, "Usp_getattendance", parameter);  
  21. Attendance.Load(dr);  
  22. }  
  23. SqlConnection sqlconnection = DLConnection.getConnectionInstance();  
  24. string employeenamequery = "select FirstName+' '+LastName from employee where employeeid='" + empid + "'";  
  25. string empname = SqlHelper.ExecuteScalar(sqlconnection, CommandType.Text, employeenamequery).ToString();  
  26. gvExportData.DataSource = Attendance;  
  27. gvExportData.DataBind();  
  28. Response.ClearContent();  
  29. Response.ContentType = "application/pdf";  
  30. Response.AddHeader("content-disposition""attachment;filename=" + empname + " Attendance.pdf");  
  31. Response.Charset = "UTF-8";  
  32. Response.ContentEncoding = Encoding.UTF8;  
  33. Response.Cache.SetCacheability(HttpCacheability.NoCache);  
  34. PdfManager PdfInstance = new PdfManager();  
  35. using (StringWriter sw = new StringWriter())  
  36. {  
  37. using (HtmlTextWriter hw = new HtmlTextWriter(sw))  
  38. {  
  39. gvExportData.RenderControl(hw);  
  40. StringReader sr = new StringReader(sw.ToString());  
  41. //string page = ddlPaperSize.SelectedValue.ToString();  
  42. using (iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(PageSize.A2, 10f, 10f, 30f, 30f))  
  43. {  
  44. HTMLWorker htmlparser = new HTMLWorker(pdfDoc);  
  45. PdfWriter writer = PdfWriter.GetInstance(pdfDoc, workStream);  
  46. //set the PageEvent of the pdfWriter instance to the instance of our PDFPage class  
  47. writer.PageEvent = PdfInstance;  
  48. pdfDoc.Open();  
  49. Paragraph ReportHeading = new Paragraph();  
  50. ReportHeading.Add("Monthly Attendance");  
  51. ReportHeading.Alignment = Element.ALIGN_CENTER;  
  52. ReportHeading.Font.Size = 25;  
  53. pdfDoc.Add(ReportHeading);  
  54. pdfDoc.Add(new Paragraph("\n"));  
  55. Phrase data = new Phrase();  
  56. htmlparser.Parse(sr);  
  57. byte[] byteInfo = workStream.ToArray();  
  58. workStream.Write(byteInfo, 0, byteInfo.Length);  
  59. workStream.Position = 0;  
  60. pdfDoc.Close();  
  61. Response.Write(pdfDoc);  
  62. Response.End();  
  63. gvExportData.Visible = false;  
  64. }  
  65. }  
  66. }  
  67. return new FileStreamResult(workStream, "application/pdf");  
  68. }

Answers (6)