Adhikar Patil

Adhikar Patil

  • NA
  • 481
  • 129.4k

How to Download Panel Data in PDF File Format

Apr 24 2018 6:53 AM
Hello This is My Source Code
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>  
  2. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head id="Head1" runat="server">  
  6. <title>Generate Certificate</title>  
  7. <link href="https://fonts.googleapis.com/css?family=Varela+Round" rel="stylesheet">  
  8. <meta name="keywords" content="">  
  9. <link href="Css/css/font-awesome.css" rel="stylesheet">  
  10. <link href="Css/css/bootstrap.min.css" rel="stylesheet">  
  11. <link href="Css/css/owl.carousel.css" rel="stylesheet">  
  12. <link href="Css/css/owl.theme.css" rel="stylesheet">  
  13. <link href="Css/css/style.default.css" rel="stylesheet">  
  14. <link href="Css/css/custom.css" rel="stylesheet">  
  15. <link href="Css/css/animate.css" rel="stylesheet">  
  16. <link rel="shortcut icon" href="favicon.png">  
  17. <link href="Css/Comman.css" type="text/css" rel="Stylesheet" />  
  18. <link href="Css/Style.css" type="text/css" rel="Stylesheet" />  
  19. <link href="Student/Css/Style.css" type="text/css" rel="Stylesheet" />  
  20. <script src="js/JScript.js" type="text/javascript"></script>  
  21. <script type="text/javascript">  
  22. function printDiv(divName) {  
  23. var printContents = document.getElementById("<%=divPanel.ClientID %>").innerHTML;  
  24. var originalContents = document.body.innerHTML;  
  25. document.body.innerHTML = printContents;  
  26. document.body.f  
  27. window.print();  
  28. document.body.innerHTML = originalContents;  
  29. document.getElementById("<%=divPanel.ClientID %>").style.font = "italic 17px Times New Roman";  
  30. }  
  31. </script>  
  32. <style type="text/css">  
  33. hr  
  34. {  
  35. border-top: 1px solid #ccc !important;  
  36. }  
  37. .panel  
  38. {  
  39. box-shadow: 0 2px 2px 0 rgba(0,0,0,0.14), 0 3px 1px -2px rgba(0,0,0,0.12), 0 1px 5px 0 rgba(0,0,0,0.2);  
  40. border: none !important;  
  41. }  
  42. </style>  
  43. </head>  
  44. <body>  
  45. <form id="form1" runat="server">  
  46. <asp:ScriptManager ID="sm" runat="server">  
  47. </asp:ScriptManager>  
  48. <div class="navbar navbar-default navbar-fixed-top" role="navigation" id="navbar">  
  49. <div class="header">  
  50. <div class="container">  
  51. <div class="col-lg-8 navbar-header">  
  52. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">  
  53. <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span><span  
  54. class="icon-bar"></span><span class="icon-bar"></span>  
  55. </button>  
  56. <a href="Default.aspx">  
  57. <h2>  
  58. tallyelearning.com</h2>  
  59. </a>  
  60. </div>  
  61. <div class="col-lg-4 navbar-collapse collapse pull-left" id="navigation">  
  62. <ul class="nav navbar-nav navbar-right list-inline">  
  63. <li class="active"><a href="Default.aspx">Home</a></li>  
  64. </ul>  
  65. </div>  
  66. <!--/.nav-collapse -->  
  67. </div>  
  68. </div>  
  69. </div>  
  70. <div class="container margintop70">  
  71. <asp:Panel ID="pnlSignUp" runat="server">  
  72. <div class="col-lg-3">  
  73. </div>  
  74. <div class="col-lg-8 margintop40 margin-bottom20 margintop40 pull-left" style="width: 45%">  
  75. <div class="panel">  
  76. <div class="panel-body panel-content">  
  77. <div class="col-lg-12 margintop20">  
  78. <h4>  
  79. Get Certificate</h4>  
  80. <label>  
  81. Continue with tallyelearning.com</label>  
  82. <hr />  
  83. </div>  
  84. <div class="fulldiv col-lg-12">  
  85. <div class="form-group fulldiv margintop20">  
  86. <div class="col-lg-12 noleftpadding">  
  87. <label>  
  88. First Name</label>  
  89. <asp:TextBox ID="txtFirstName" runat="server" CssClass="form-control" MaxLength="50"  
  90. onblur="validateFirstName(this,'txtFirstName');"></asp:TextBox>  
  91. </div>  
  92. <div class="col-lg-12 margintop10 noleftpadding">  
  93. <label>  
  94. Last Name</label>  
  95. <asp:TextBox ID="txtLastName" runat="server" CssClass="form-control" MaxLength="50"  
  96. onblur="validateLastName(this,'txtLastName');"></asp:TextBox>  
  97. </div>  
  98. </div>  
  99. <div class="form-group fulldiv margintop20">  
  100. <div class="col-lg-12 margintop10 noleftpadding">  
  101. <label>  
  102. Email Id</label>  
  103. <asp:TextBox ID="txtEmailId" runat="server" CssClass="form-control" MaxLength="100"  
  104. onblur="validateEmail(this,'txtEmailId');"></asp:TextBox>  
  105. </div>  
  106. <div class="col-lg-12 margintop10 noleftpadding">  
  107. <label>  
  108. Contact No</label>  
  109. <asp:TextBox ID="txtContact" runat="server" CssClass="form-control" MaxLength="10"  
  110. onblur="return ValidateMobNumber('txtContact')"></asp:TextBox>  
  111. </div>  
  112. </div>  
  113. <div class="fulldiv pull-left text-center margintop30 margin-bottom20">  
  114. <asp:LinkButton ID="lnkSignUp" OnClick="lnkSignUp_Click" runat="server" OnClientClick="return CheckBlank()"  
  115. CssClass="btn btn-theme flatbtn" Width="280px" Height="40px" Font-Size="15px"  
  116. Style="padding-top: 10px"><i class="fa fa-file-pdf-o"></i> Generate Certificate</asp:LinkButton>  
  117. </div>  
  118. </div>  
  119. </div>  
  120. </div>  
  121. </div>  
  122. </asp:Panel>  
  123. <div class="col-lg-12 margintop10">  
  124. <div class="col-lg-3">  
  125. </div>  
  126. <div id="divPanel" runat="server" class="col-lg-5" style="font-family: Times New Roman;  
  127. font-style: italic; font-size: 17px">  
  128. <asp:Panel ID="pnlCertificate" runat="server">  
  129. <div class="">  
  130. <div class="bg-white margintop10 fulldiv">  
  131. <div class="text-center">  
  132. <img src="Student/Images/1.jpg" width="100%" />  
  133. </div>  
  134. <div class="text-center">  
  135. This certificate is proudly presented to  
  136. </div>  
  137. <div class="text-center margin-bottom10">  
  138. <asp:Label ID="lblName" runat="server" Font-Bold="true" Font-Size="22px" CssClass="firebricktext text-capitalize"></asp:Label>  
  139. </div>  
  140. <div class="text-center margin-bottom10 margintop10">  
  141. Successfully completed in<br />  
  142. <asp:Label ID="lblCourseName" CssClass="text-capitalize" runat="server" Font-Bold="true"  
  143. Font-Size="22px"></asp:Label>  
  144. </div>  
  145. <div class="text-center margintop10">  
  146. <span>At Authorized training center</span>  
  147. </div>  
  148. <div class="text-center margin-bottom10">  
  149. <asp:Label ID="lblTrainingCenter" CssClass="text-capitalize" runat="server" Font-Bold="true"></asp:Label>  
  150. </div>  
  151. <div class="text-center margin-bottom10 margintop10">  
  152. <label>  
  153. He/She has awarded this certificate for distinctive<br />  
  154. performance in this programme</label>  
  155. </div>  
  156. <div style="margin-top: 30px; margin-bottom: -71px; float: left; width: 100%">  
  157. <div class="margin-bottom10 margintop10">  
  158. <div class="col-lg-5">  
  159. Grade :  
  160. <asp:Label ID="lblGrade" runat="server"></asp:Label>  
  161. </div>  
  162. <div class="col-lg-5 pull-right">  
  163. Date of Issue :  
  164. <asp:Label ID="lblDateofIssue" runat="server"></asp:Label>  
  165. </div>  
  166. </div>  
  167. <div class="margin-bottom10 margintop10 fulldiv pull-left">  
  168. <div class="col-lg-10">  
  169. Certificate No :  
  170. <asp:Label ID="lblCertificateNo" runat="server" Font-Size="18px"></asp:Label>  
  171. </div>  
  172. <div class="col-lg-1">  
  173. </div>  
  174. </div>  
  175. </div>  
  176. <div class="text-center">  
  177. <img src="Student/Images/21.jpg" width="100%" />  
  178. </div>  
  179. </div>  
  180. </div>  
  181. </asp:Panel>  
  182. </div>  
  183. <div class="col-lg-4">  
  184. </div>  
  185. <div class="col-lg-12 margintop10">  
  186. <div class="col-lg-2">  
  187. </div>  
  188. <div class="col-lg-7 text-center">  
  189. <asp:LinkButton ID="lnkPrintPDF" OnClick="lnkPrintPDF_Click" runat="server" CssClass="btn btn-theme"  
  190. Visible="false" OnClientClick="printDiv('divPanel')"> Print PDF </asp:LinkButton>  
  191. </div>  
  192. <div class="col-lg-3">  
  193. </div>  
  194. </div>  
  195. </div>  
  196. </div>  
  197. </form>  
  198. </body>  
  199. </html>  
AND
 
Here is MY Code Behind On Button Click Event
  1. protected void lnkPrintPDF_Click(Object sender, EventArgs e)  
  2. {  
  3. Response.Clear();  
  4. Response.Buffer = true;  
  5. Response.ClearContent();  
  6. Response.ClearHeaders();  
  7. Response.Charset = "";  
  8. string FileName = "Tally Certificate" + DateTime.Now + ".pdf";  
  9. StringWriter sw = new StringWriter();  
  10. HtmlTextWriter hw = new HtmlTextWriter(sw);  
  11. Response.Cache.SetCacheability(HttpCacheability.NoCache);  
  12. Response.ContentType = "application/pdf";  
  13. Response.AddHeader("Content-Disposition""attachment;filename=" + FileName);  
  14. pnlCertificate.Visible = true;  
  15. pnlCertificate.RenderControl(hw);  
  16. Response.Write(sw.ToString());  
  17. Response.End();  
  18. }  
So in this Button Click I am downlaod the file but when i open that pdf file i am getting error like Adobe Reader Can not open the file because it is either not a supported file type or because the file has been damaged. So, Please anyone tell me the solution.

Answers (1)