So first check previous article and add Action Link in list.cshtml for Create PDF.
- Create PDF : @Html.ActionLink("Create PDF", "CreatePdf", "Home")


- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using System.IO;
- using System.Web.Helpers;
- public FileStreamResult CreatePdf()
- {
- List all = new List();
- all = db.Employees.ToList();
- WebGrid grid = new WebGrid(source: all, canPage: false, canSort: false);
- string gridHtml = grid.GetHtml(
- columns: grid.Columns(
- grid.Column("Id", "EmployeeId"),
- grid.Column("Name", "Employee Name"),
- grid.Column("City", "City"),
- grid.Column("MobileNo", "Mobile No")
- )
- ).ToString();
- string exportData = String.Format("{0}{1}", "", gridHtml);
- var bytes = System.Text.Encoding.UTF8.GetBytes(exportData);
- using (var input = new MemoryStream(bytes))
- {
- var output = new MemoryStream();
- var document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 50, 50);
- var writer = PdfWriter.GetInstance(document, output);
- writer.CloseStream = false;
- document.Open();
- var xmlWorker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();
- xmlWorker.ParseXHtml(writer, document, input, System.Text.Encoding.UTF8);
- document.Close();
- output.Position = 0;
- return new FileStreamResult(output, "application/pdf");
- }
- }



Guest UserPosted Aug 8, 2015, 6:27 PM
Thank you it seemed to have worked. If I want to add a company logo on the top left of the page, is this possible?
Malvik BhavsarPosted Jul 12, 2015, 12:10 AM
Hello Rivesh, you need replace this line "string exportData = String.Format("{0}{1}", "", gridHtml);" with this "string exportData = String.Format("<html><head>{0}</head><body>{1}</body></html>", "<style>table td,th{ border: 1px solid; border-collapse: separate}</style>" , gridHtml);"
Guest UserPosted Jun 25, 2015, 7:18 PM
Hi Malvik, is there any way to add grid lines to the pdf document?