0

Unsure how to underline the heading "Leave Applications". I am using itextsharp to export a webgrid to pdf. I would like to insert a heading which needs to be underlined, and an image if possible, the image will be used for the company logo. My code is as follows:

public FileStreamResult ExporttoPdf()
{
    List<EmployeeLeaveApplicationView> viewModelList = new List<EmployeeLeaveApplicationView>();
    var model = _employeeleaveapplication.GetAllEmployeeLeaveApplications().ToList();

    foreach (var items in model)
    {
        EmployeeLeaveApplicationView objEmployeeLeaveViewModel = new EmployeeLeaveApplicationView();
        //objEmployeeLeaveViewModel.emp_first_name = items.Employee_DetailView.emp_first_name;
        //objEmployeeLeaveViewModel.emp_tel_no = items.Employee_Details.emp_tel_no;
        objEmployeeLeaveViewModel.persal_no = items.persal_no;
        objEmployeeLeaveViewModel.leave_type_id = items.leave_type_id;
        objEmployeeLeaveViewModel.no_of_days = items.no_of_days;
        objEmployeeLeaveViewModel.start_date = items.start_date;
        objEmployeeLeaveViewModel.end_date = items.end_date;
        objEmployeeLeaveViewModel.days_left = items.days_left;
        viewModelList.Add(objEmployeeLeaveViewModel);
    }

    WebGrid grid = new WebGrid(viewModelList, canPage: false, canSort: false);
    string gridHTML = grid.GetHtml(

        columns: grid.Columns
            (
                grid.Column("persal_no", "Persal number"),
                grid.Column("leave_type_id", "Leave type"),
                //grid.Column("emp_first_name", "Name"),
                grid.Column("no_of_days", "No of days"),
                grid.Column("start_date", "Start date"),
                grid.Column("end_date", "End date"),
                grid.Column("days_left", "Days left")
                //grid.Column("emp_tel_no", "Contact number")
            )).ToString();

    string exportData = String.Format("<html><head>{0}</head><body>{1}</body></html>",
        "<style> <h2><span>Leave Applications</span></h2> 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 = Encoding.UTF8.GetBytes(exportData);

    using (var input = new MemoryStream(bytes))
    {
        var output = new MemoryStream();

        var document = new Document(PageSize.A4, 50, 50, 50, 50);

        var writer = PdfWriter.GetInstance(document, output);

        writer.CloseStream = false;

        document.Open();

        var xmlWorker = XMLWorkerHelper.GetInstance();

        xmlWorker.ParseXHtml(writer, document, input, Encoding.UTF8);

        document.Close();

        output.Position = 0;

        return new FileStreamResult(output, "application/pdf");
    }
}
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
Rivash
  • 21
  • 2
  • Did you try the text-decoration CSS declaration or the `` tag? – Chris Haas Aug 10 '15 at 13:21
  • Also, why is your `

    ` stuff wrapped in a `

    – Chris Haas Aug 10 '15 at 13:21
  • CSS code for some reason has no effect so removed that, the tag is no longer used for underline in html5, I got this definition online, the element is redefined in HTML5, to represent text that should be stylistically different from normal text, such as misspelled words or proper nouns in Chinese. When I remove the tag out of the – Rivash Aug 10 '15 at 19:12
  • First, when dealing with iText don't worry too much about semantic web and HTML5 theories of best tag practices since iText is just going to throw your tags away when it creates the PDF. HTML5 is about separating content from presentation but PDF is explicitly all about combing content and presentation so that a document looks 100% the same wherever it gets rendered. I could be wrong but the `` really should work to underline something. Second, [read this](http://stackoverflow.com/a/25164258/231316) specifically the fourth paragraph, and then post a small subset of your HTML. – Chris Haas Aug 10 '15 at 20:19
  • Thanks the tag works cant believe I didn't try this. Is it also possible to insert an image as a logo on the top right or left of the pdf page which also includes the grid, using itextsharp. – Rivash Aug 10 '15 at 21:19

0 Answers0