In this article, I will explain you how to export HTML table student details to PDF file. For this demo purpose I am using iTextSharp XMLWorkerHelper library.
Click here to download iTextSharp XMLWorkerHelper library.
Once you download the library file. Extract the file and import those 2 DLL file in your project
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <form id="form1" runat="server"> <div id = "Studenttable"> <table style="border:solid 1px;"> <tr> <th>Student Id</th> <th>Student Name</th> <th>Student Branch</th> </tr> <tr> <td>1</td> <td>John Smith</td> <td>Computer Engineering</td> </tr> <tr> <td>2</td> <td>David Jones</td> <td>Civil Engineering</td> </tr> <tr> <td>3</td> <td>Michael Johnson</td> <td>Chemical Engineering</td> </tr> <tr> <td>4</td> <td>Chris Lee</td> <td>Electrical Engineering</td> </tr> <tr> <td>5</td> <td>Mike Brown</td> <td>Computer Engineering</td> </tr> </table></div> <br /> <asp:HiddenField ID="hfGridHtml1" runat="server" OnValueChanged="hfGridHtml_ValueChanged" /> <asp:Button ID="btnExport" runat="server" Text="Export To PDF File" OnClick="ExportToPDF" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> $(function () { $("[id*=btnExport]").click(function () { $("[id*=hfGridHtml1]").val($("#Studenttable").html()); }); }); </script> </form> |
1 2 3 4 5 6 7 8 9 10 11 | StringReader sr = new StringReader(Request.Form[hfGridHtml1.UniqueID]); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr); pdfDoc.Close(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=StudentDetails.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Write(pdfDoc); Response.End(); |
© 2020 Tech Study. All rights reserved | Developed by Tech Study| Privacy Policy | Sitemap