Hi comunidadmexi...,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Namespaces
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
NorthwindEntities entities = new NorthwindEntities();
return View(from customer in entities.Customers.Take(5)
select customer);
}
[HttpPost]
[ValidateInput(false)]
public FileResult PrintViewToPdf(string GridHtml)
{
using (MemoryStream stream = new MemoryStream())
{
StringReader sr = new StringReader(GridHtml);
Document pdfDoc = new Document(PageSize.A5, 10f, 10f, 10f, 10f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
return File(stream.ToArray(), "application/pdf", "Grid.pdf");
}
}
}
View
@model IEnumerable<Customer>
@{
Layout = null;
WebGrid webGrid = new WebGrid(source: Model, canSort: false, canPage: false);
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
table.gridtable {
font-family: verdana,arial,sans-serif;
font-size: 11px;
color: #333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table.gridtable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table.gridtable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
</head>
<body>
<div id="Grid">
@webGrid.GetHtml(
htmlAttributes: new { @id = "WebGrid" },
tableStyle: "gridtable",
columns: webGrid.Columns(
webGrid.Column("CustomerID", "Customer Id"),
webGrid.Column("ContactName", "Customer Name"),
webGrid.Column("City", "City"),
webGrid.Column("Country", "Country"),
webGrid.Column("", "", format: @<button id="btnPrint">Print Details Row To PDF</button>)))
</div>
<br />
<br />
@using (Html.BeginForm("PrintViewToPdf", "Home", FormMethod.Post))
{
<input type="hidden" name="GridHtml" />
}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=btnPrint]").click(function () {
var row = $(this).closest('tr');
var td = $(row).find('td');
var table = "<table style='font-family: verdana,arial,sans-serif;font-size: 11px;color: #333333;border: 1px solid #666666;'>";
table += "<thead><tr>";
table += "<th style='background-color: #dedede;'>Customer Id</th>";
table += "<th style='background-color: #dedede;'>Customer Name</th>";
table += "<th style='background-color: #dedede;'>City</th>";
table += "<th style='background-color: #dedede;'>Country</th>";
table += "</tr></thead>";
table += "<tbody><tr>";
for (var i = 0; i < td.length - 1; i++) {
table += "<td style='background-color: #ffffff;'>" + td[i].innerHTML + "</td>";
}
table += "</tr></tbody></table>";
$("input[name='GridHtml']").val(table);
$('form').submit();
});
});
</script>
</body>
</html>
Screenshot