Madhav Sharma

Madhav Sharma

  • 766
  • 899
  • 39.3k

How to fix column width by itext pdf in webgrid

Nov 28 2017 6:18 AM
How to fix column width by itext pdf in webgrid
 
https://ibb.co/gFHnnm
  1. public FileStreamResult GETPdf(FilterModel model)  
  2. {  
  3. var userContactId = (int?)Session["UserContactId"];  
  4. var userCompanyId = (int?)Session["UserCompanyId"];  
  5. var contact = Session["Contact"as Contact;  
  6. var contactName = contact.FirstName;  
  7. model.Page = model.Page == 0 ? 1 : model.Page;  
  8. model.PageSize = model.PageSize == 0 ? 1000 : model.PageSize;  
  9. model.SearchText = string.IsNullOrEmpty(model.SearchText) ? model.SearchText : model.SearchText.Trim();  
  10. model.Sort = string.IsNullOrEmpty(model.Sort) ? "dateEntered" : model.Sort;  
  11. model.Sortdir = string.IsNullOrEmpty(model.Sortdir) ? "desc" : model.Sortdir;  
  12. var temp1 = Session["DateEnter1"];  
  13. var temp2 = Session["DateEnter2"];  
  14. var sd = temp1.ToString();  
  15. var ed = temp2.ToString();  
  16. var todate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).ToString("yyyy-MM-dd");  
  17. model.sdate = string.IsNullOrEmpty(model.sdate) ? sd : todate;  
  18. model.edate = string.IsNullOrEmpty(model.edate) ? ed : todate;  
  19. var tickets = cwContact.getExportToPDFTickets(contact, userCompanyId, true, model);  
  20. var totalTicket = cwContact.totalTicket;  
  21. model.TicketReportDataModel = tickets;  
  22. WebGrid grid = new WebGrid(model.TicketReportDataModel, rowsPerPage: model.PageSize, canPage: true);  
  23. string gridData = grid.GetHtml(  
  24. columns: grid.Columns(  
  25. grid.Column(columnName: "ticketNo", header: "Ticket#", canSort: false),  
  26. grid.Column(columnName: "Summary", header: "Title", canSort: false),  
  27. grid.Column(columnName: "Contact_Name", header: "Contact", canSort: false),  
  28. grid.Column(columnName: "SR_Status", header: "Status", canSort: false),  
  29. grid.Column(columnName: "Site_Name", header: "Site Name", canSort: false),  
  30. grid.Column(columnName: "SR_Type_Description", header: "Type", canSort: false),  
  31. grid.Column(columnName: "Company", header: "Company", canSort: false),  
  32. grid.Column(columnName: "Board", header: "Board", canSort: false),  
  33. grid.Column(columnName: "Discussion", header: "Discussion", canSort: false),  
  34. grid.Column(columnName: "Date_Entered", header: "Date Entered", canSort: false),  
  35. grid.Column(columnName: "DateResponded", header: "Date Respond", canSort: false),  
  36. grid.Column(columnName: "Date_Resolved", header: "Date Resolved", canSort: false),  
  37. grid.Column(columnName: "Date_Closed", header: "Date Closed", canSort: false)  
  38. )).ToString();  
  39. string exportData = String.Format("<html><head>{0}</head><body>{1}</body></html>","<style>table{ border-spacing: 10px; border-collapse: collapse; border: solid; }" + "td{border-color: #191970; border-width: 1px 1px 0 0;  
  40. border-style: solid; margin: 0; padding: 4px;  
  41. background-color: #E0FFFF;}</style>", gridData);  
  42. var bytes = System.Text.Encoding.UTF8.GetBytes(exportData.ToString());  
  43. using (var input = new MemoryStream(bytes))  
  44. {  
  45. var output = new MemoryStream();  
  46. var document = new iTextSharp.text.Document(PageSize.A4, 10, 10, 10, 10);  
  47. var writer = PdfWriter.GetInstance(document, output);  
  48. writer.CloseStream = false;  
  49. document.Open();  
  50. var xmlWorker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();  
  51. xmlWorker.ParseXHtml(writer, document, input, System.Text.Encoding.UTF8);  
  52. document.Close();  
  53. output.Position = 0;  
  54. //return new FileStreamResult(output, "application/pdf");  
  55. return File(output, "application/pdf""TicketList.pdf");  
  56. }  
  57. }  

Answers (1)