Hello,
I'm actually following http://www.c-sharpcorner.com/article/basic-pdf-export-using-jquery-in-asp-net-mvc-razor/ article to bind data from controllers action to HTML table and then export data from HTML table to iTextSharp pdf viewer. Below i'm binding controllers action method data to html table.
- @model List<ExportDataAsPDFAppl.Controllers.Employees>
- @using (Html.BeginForm("exportPDF", "Home", FormMethod.Post))
- {
- <div>
- <input type="hidden" name="gridHTML" />
- <input type="submit" id="btnExportHTML" name="btnExportHTML" class="btn btn-success" value="Export Pdf" />
- div>
- <div id="tblEmpData">
- <table class="table table-striped table-condensed">
- <thead>
- <tr>
- <th>IDth>
- <th>FirstNameth>
- <th>LastNameth>
- <th>Genderth>
- <th>Salaryth>
- tr>
- thead>
- <tbody>
- @foreach (var item in Model)
- {
- <tr>
- <td>@item.IDtd>
- <td>@item.FirstNametd>
- <td>@item.LastNametd>
- <td>@item.Gendertd>
- <td>@item.Salarytd>
- tr>
- }
- tbody>
- table>
- div>
- }
In btnExportHTML jQuery click function, hidden variable gridHTML is binded with HTML table.
Below is actionMethod for button click.
- [HttpPost]
- [ValidateInput(false)]
- private FileResult exportPDF(string gridHTML)
- {
- StringReader reader = null;
- Document pdfDoc = null;
- PdfWriter write = null;
- try
- {
- using (MemoryStream memorystream = new MemoryStream())
- {
- reader = new StringReader(gridHTML);
- pdfDoc = new Document(PageSize.A4, 5f, 5f, 5f, 5f);
- write = PdfWriter.GetInstance(pdfDoc, memorystream);
- pdfDoc.Open();
- XMLWorkerHelper.GetInstance().ParseXHtml(write, pdfDoc, reader);
- pdfDoc.Close();
- return File(memorystream.ToArray(), "application/pdf", Convert.ToString(DateTime.Now + ".pdf"));
- }
- }
- finally
- {
- reader.Dispose();
- pdfDoc.Dispose();
- write.Dispose();
- }
- }

Ketan HiraparaRank #7,540 · 239 reputationPosted Nov 27, 2017, 7:40 PM EST
L A180 reputationPosted Nov 28, 2017, 12:38 PM EST
Ketan HiraparaRank #7,540 · 239 reputationPosted Nov 28, 2017, 4:41 PM EST
L A180 reputationPosted Dec 1, 2017, 1:25 PM EST
L A180 reputationPosted Dec 3, 2017, 1:52 PM EST
Ketan HiraparaRank #7,540 · 239 reputationPosted Dec 4, 2017, 2:49 AM EST
Mythili Saravanana78 reputationPosted Jul 17, 2018, 12:47 AM EDT