Answered by:
How to Export the contents for each single row of the Webgrid to PDF in MVC Application

Question
-
User-1453200658 posted
Hi there,
I would like to provide to my users with the ability to export the contents for each single row of the
WebGrid MVC ASP NET
to a PDF file using the freePDF
library,iTextSharp
.This is the Complete View
@model List<MVCExportWebgrid.CustomerInfo> @{ ViewBag.Title = "Index"; var grid = new WebGrid(source: Model, canPage: false); } @* Here I will add some css for Looks webgrid better *@ <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> <h2>Customer Info</h2> <div> @grid.GetHtml( tableStyle:"gridtable", columns:grid.Columns( grid.Column("CustomerID","Customer ID"), grid.Column("CustomerName","Customer Name"), grid.Column("Address","Address"), grid.Column("City", "City"), grid.Column("PostalCode","Postal Code") ) ) </div>
My question is how do I insert a link or button for each row of the
WegGrid
to export the content of the single row to pdf?I consulted google search, but I only found examples on
MVC ASP NET
of exporting the entire contentWegGrid
to pdf and not exporting content of the single row to pdf.I tried to search posts, without any result either, maybe I didn't use the right words.
I have tried to insert on WebGrid
@Html.ActionLink("Print Details Row To PDF", "PrintViewToPdf", new { id=item.CustomerId })
But I have error
The name “item” does not exist in the current context
Any suggestion?
Sunday, January 31, 2021 5:52 PM
Answers
-
User1312693872 posted
Hi,Edwaed Sheriff Curtis
To pass the id to action, you should use this form instead:
grid.Column("test", format: item => Html.ActionLink("Export", "ExportPDF", new { id = item.CustomerID},new { target = "_blank" }))
Complete View:
//... your style <div> @grid.GetHtml( tableStyle: "gridtable", columns: grid.Columns( grid.Column("CustomerID", "Customer ID"), grid.Column("CustomerName", "Customer Name"), grid.Column("Address", "Address"), grid.Column("test", format: item => Html.ActionLink("Export", "ExportPDF", new { id = item.CustomerID},new { target = "_blank" })) ) ) </div>
Controller:( if you want to click and download at the same time, add 'attachment;')
public void ExportPDF(int id) { CustomerInfo rowtopdf = _db.CustomerInfos.Where(x => x.CustomerID == id).FirstOrDefault(); string HTMLContent = "<table>"; HTMLContent += "<tr><td>" + rowtopdf.CustomerID + "</td><td>" + rowtopdf.CustomerName+ "</td><td>" + rowtopdf.Address + "</td></tr>"; HTMLContent += "</table>"; Response.Clear(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "filename=" + "PDFfile.pdf");
//Response.AddHeader("content-disposition", "attachment;filename=" + "PDFfile.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.BinaryWrite(GetPDF(HTMLContent)); Response.End(); } public byte[] GetPDF(string pHTML) { byte[] bPDF = null; MemoryStream ms = new MemoryStream(); TextReader txtReader = new StringReader(pHTML); Document doc = new Document(PageSize.A4, 25, 25, 25, 25); PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms); HTMLWorker htmlWorker = new HTMLWorker(doc); doc.Open(); htmlWorker.StartDocument(); htmlWorker.Parse(txtReader); htmlWorker.EndDocument(); htmlWorker.Close(); doc.Close(); bPDF = ms.ToArray(); return bPDF; }Result:
Best Regards,
Jerry Cai
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, February 1, 2021 8:13 AM
All replies
-
User503812343 posted
On link redirect to a controller action where you will have to read specific data and create a PDF with required data only.
You can use paragraph or cells and another attributes
https://www.c-sharpcorner.com/UploadFile/d40a40/create-simple-pdf-file-using-itextsharp-library/
Monday, February 1, 2021 7:31 AM -
User-1453200658 posted
thanks for reply.
Your suggestion is C# ASP NET
I asking help for MVC ASP NET
Monday, February 1, 2021 8:11 AM -
User1312693872 posted
Hi,Edwaed Sheriff Curtis
To pass the id to action, you should use this form instead:
grid.Column("test", format: item => Html.ActionLink("Export", "ExportPDF", new { id = item.CustomerID},new { target = "_blank" }))
Complete View:
//... your style <div> @grid.GetHtml( tableStyle: "gridtable", columns: grid.Columns( grid.Column("CustomerID", "Customer ID"), grid.Column("CustomerName", "Customer Name"), grid.Column("Address", "Address"), grid.Column("test", format: item => Html.ActionLink("Export", "ExportPDF", new { id = item.CustomerID},new { target = "_blank" })) ) ) </div>
Controller:( if you want to click and download at the same time, add 'attachment;')
public void ExportPDF(int id) { CustomerInfo rowtopdf = _db.CustomerInfos.Where(x => x.CustomerID == id).FirstOrDefault(); string HTMLContent = "<table>"; HTMLContent += "<tr><td>" + rowtopdf.CustomerID + "</td><td>" + rowtopdf.CustomerName+ "</td><td>" + rowtopdf.Address + "</td></tr>"; HTMLContent += "</table>"; Response.Clear(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "filename=" + "PDFfile.pdf");
//Response.AddHeader("content-disposition", "attachment;filename=" + "PDFfile.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.BinaryWrite(GetPDF(HTMLContent)); Response.End(); } public byte[] GetPDF(string pHTML) { byte[] bPDF = null; MemoryStream ms = new MemoryStream(); TextReader txtReader = new StringReader(pHTML); Document doc = new Document(PageSize.A4, 25, 25, 25, 25); PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms); HTMLWorker htmlWorker = new HTMLWorker(doc); doc.Open(); htmlWorker.StartDocument(); htmlWorker.Parse(txtReader); htmlWorker.EndDocument(); htmlWorker.Close(); doc.Close(); bPDF = ms.ToArray(); return bPDF; }Result:
Best Regards,
Jerry Cai
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, February 1, 2021 8:13 AM