Application

I am using the data from the RadGrid here to export.

export

Steps:

Download and add reference to iTextSharp.dll files and others to the project.

Code at the .cs file,

  1. privatevoidExport_ToPDF_InHtmlFormat()
  2. {#region--Exporting RadGrid Data in Html format to PDF--
  3. try {
  4. string _Name = string.Empty;
  5. string _ShippingAddress = string.Empty;
  6. string _CityName = string.Empty;
  7. string _PinCode = string.Empty;
  8. string _StateName = string.Empty;
  9. string _ContactNo = string.Empty;
  10. // Formatting the body of the Document
  11. string body = "";
  12. body = "<html><body style='margin-left:20px;'><img src='http://www.swashconvergence.com/images/logo-kencloud.png'/></body></html>";
  13. body += "<br />";
  14. body += "<h1>KenPartner Contact Details</h1>";
  15. body += "<br />";
  16. // Looping through the Grid Data
  17. //RadGridTotalCOD.AllowPaging = false;
  18. foreach(GridDataItem item inRadGridTotalCOD.MasterTableView.Items)
  19. {
  20. // retrieving data from the grid
  21. _Name = item["ApplicantName"].Text;
  22. _ShippingAddress = item["ShippingAddress"].Text;
  23. _CityName = item["CityName"].Text;
  24. _PinCode = item["Pincode"].Text;
  25. _StateName = item["StateName"].Text;
  26. _ContactNo = item["ContactNo"].Text;
  27. // Configuring the Body of PDF
  28. body += "<br /><br />";
  29. body += "<h1>To, </h1>";
  30. body += "<br /><h1>" + _Name + "</h1> ";
  31. body += "<br /><h1>" + _ShippingAddress + "</h1> ";
  32. body += "<br /><h1>" + _CityName + " " + "-" + " " + _PinCode + " " + "</h1>";
  33. body += "<br /><h1>" + _StateName + "</h1> ";
  34. body += "<br /><h1>Contact No" + " " + "-" + " " + _ContactNo + "</h1> ";
  35. body += "<br /><br />";
  36. }
  37. // Export ToPDf
  38. using(StringWritersw = newStringWriter()) {
  39. using(HtmlTextWriterhtw = newHtmlTextWriter(sw)) {
  40. htw.Write(body);
  41. StringReadersr = newStringReader(sw.ToString());
  42. DocumentpdfDoc = newDocument(PageSize.A2, 10 f, 10 f, 10 f, 0 f);
  43. HTMLWorkerhtmlparser = newHTMLWorker(pdfDoc);
  44. PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
  45. // Open The Pdf
  46. pdfDoc.Open();
  47. htmlparser.Parse(sr);
  48. pdfDoc.Close();
  49. // Current Date
  50. stringCurrentDate = DateTime.Now.ToString("dd/MMM/yyyy");
  51. Response.ContentType = "application/pdf";
  52. // Give the Dacument Name
  53. Response.AddHeader("content-disposition", "attachment;filename=MyContact " + CurrentDate + ".pdf");
  54. Response.Cache.SetCacheability(HttpCacheability.NoCache);
  55. Response.Write(pdfDoc);
  56. Response.End();
  57. }
  58. }
  59. } catch (ArgumentNullExceptionExc)
  60. {
  61. LblErrorMsg.Text = "Application Error : " + Exc.Message;
  62. } catch (InvalidCastExceptionExc)
  63. {
  64. LblErrorMsg.Text = "Application Error : " + Exc.Message;
  65. } catch (InvalidOperationExceptionExc)
  66. {
  67. LblErrorMsg.Text = "Application Error : " + Exc.Message;
  68. } catch (FormatExceptionExc)
  69. {
  70. LblErrorMsg.Text = "Application Error : " + Exc.Message;
  71. } catch (ExceptionExc)
  72. {
  73. LblErrorMsg.Text = "Application Error : " + Exc.Message;
  74. }
  75. #endregion
  76. }