0
Reply

Changing columns to rows using gridview and ITextsharp in mv

Rivash Krishoondutt

Rivash Krishoondutt

Jun 29 2015 5:43 PM
538

I am presently trying to switch from columns and use rows when displaying my headings on pdf as I am creating a leave application form. Any help would be appreciated I am presently a student. Thanks this is my code in the Controller.

public FileStreamResult ExporttoPdfLeave()

        {

            List<EmployeeLeaveViewModel> viewModelList = new List<EmployeeLeaveViewModel>();

            var model = _employeeleaveRepository.GetAllEmployeeLeave().ToList();

 

            foreach (var items in model)

            {

                EmployeeLeaveViewModel objEmployeeLeaveViewModel = new EmployeeLeaveViewModel();

                objEmployeeLeaveViewModel.emp_first_name = items.Employee_Details.emp_first_name;

                objEmployeeLeaveViewModel.emp_surname = items.Employee_Details.emp_surname;

               

                objEmployeeLeaveViewModel.emp_tel_no = items.Employee_Details.emp_tel_no;

                objEmployeeLeaveViewModel.persal_no = items.persal_no;

                objEmployeeLeaveViewModel.no_of_days = items.no_of_days;

                objEmployeeLeaveViewModel.c_start_date = items.c_start_date;

                objEmployeeLeaveViewModel.end_date = items.end_date;

                viewModelList.Add(objEmployeeLeaveViewModel);

            }

 

            WebGrid grid = new WebGrid(source: viewModelList, canPage: false, canSort: false);

            string gridHTML = grid.GetHtml(

 

               

               columns: grid.Columns

                (

                grid.Column("persal_no", "Persal number"),

                 grid.Column("emp_first_name", "Name"),

                 grid.Column("emp_surname", "Surname"),

                 grid.Column("duly_completed"," Duly completed"),

                 grid.Column("correct_leave_form", "Correct leave form"),

                 grid.Column("correct_leave_type", "Correct leave type"),

                 grid.Column("eight_week_cycle", "Eight week cycle"),

                 grid.Column("med_cert", " Medical certificate"),

                 grid.Column("med_cert_completed", " Medical certificate completed"),

                 grid.Column("pattern", "Pattern"),

                 grid.Column("counselling", "Counselling"),

                 grid.Column("no_of_days", "No of days"),

                  grid.Column("c_start_date", "Start date"),

                  grid.Column("end_date", "End date"),

                  grid.Column("emp_tel_no", "Contact number")

                )).ToString();

 

            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; border-style: solid; margin: 0; padding: 4px; background-color: #E0FFFF;}</style>", 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");

            }

        }

 

 

    }