1

Trying to achieve this (converting html to pdf using itextsharp): enter image description here

Using:

  StringBuilder pdfHtmlBody = new StringBuilder();

                    pdfHtmlBody.Append("<table cellspacing='2' width='100%'>");

                    foreach (var scoreSheetItem in reportCardList)
                    {





                        pdfHtmlBody.Append("<tr>");
                        pdfHtmlBody.Append("<th></th>");
                        foreach(var header in scoreSheetItem.ReportHeader)
                        {
                            pdfHtmlBody.Append("<th>");

                            pdfHtmlBody.Append(header.HeaderTitle);

                            pdfHtmlBody.Append("</th>");

                        }


                        pdfHtmlBody.Append("<th></th>");

                        pdfHtmlBody.Append("</tr>");



                        foreach (var item in scoreSheetItem.ScoreSheet)
                        {
                            pdfHtmlBody.Append("<tr>");

                            pdfHtmlBody.Append("<td>");
                            pdfHtmlBody.Append(item.Subject);
                            pdfHtmlBody.Append("</td>");
                            pdfHtmlBody.Append("<td>");

                            pdfHtmlBody.Append(item.TotalCAScore == null ? "-" : item.TotalCAScore);

                            pdfHtmlBody.Append("</td>");
                            pdfHtmlBody.Append("<td>");
                            pdfHtmlBody.Append(item.TotalExamScore == null ? "-" : item.TotalExamScore);
                            pdfHtmlBody.Append("</td>");

                            foreach(var col in item.Cummulative)
                            {
                                pdfHtmlBody.Append("<td>");
                                pdfHtmlBody.Append(col.TotalScore == null ? "-" : col.TotalScore);
                                pdfHtmlBody.Append("</td>");
                            }



                                         pdfHtmlBody.Append("<td>");
                            pdfHtmlBody.Append(item.GradeLabel);
                            pdfHtmlBody.Append("</td>");


                            pdfHtmlBody.Append("<td>");
                            pdfHtmlBody.Append(item.GradeInterpretation);
                            pdfHtmlBody.Append("</td>");




                                                     pdfHtmlBody.Append("</tr>");

                        }



                    }

                    pdfHtmlBody.Append("</table>");

Getting this result Instead: enter image description here

Note: Similar question found on stack does not address the issue

codegrid
  • 977
  • 2
  • 13
  • 33
  • It seems that you are missing the CSS style information in the pdf context. You might want to have a look at this blog explaining how to use css in the pdf generation: https://kalanamith.wordpress.com/2015/08/07/itextsharp-html-to-pdf-conversion-with-css-files/ – DeveloperExceptionError Aug 15 '18 at 06:50
  • 1
    @DeveloperExceptionError Please note that the blog you refer to is outdated. It talks about **iTextSharp**, a name that was abandoned two years ago in favor of **iText for .NET**, and it uses iText 5 and XML Worker, which were replaced by iText 7 and pdfHTML: https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml – Bruno Lowagie Aug 15 '18 at 06:56
  • 1
    Support for CSS comes out of the box once you upgrade to iText 7. Chances are that you were still using an old version of iText. Please upgrade, and the problem will disappear. – Bruno Lowagie Aug 15 '18 at 06:57
  • @bruno I didn't see any question similar to the one I created. Please point me to the question. Secondly, I have not yet added css, I want the table to first maintain its structure. The table structure is scattered after conversion to pdf. – codegrid Aug 15 '18 at 07:08
  • This page shows an example of a table with structure and CSS style: http://www.javamagazine.mozaicreader.com/JulyAugust2018#&pageSet=23 Browse through the pages of the magazine, and you'll find the corresponding code you need. [Chapter 4](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-4-creating-reports-using-pdfhtml) in the official HTML to PDF tutorial contains some more table examples. If these examples/tutorial don't help, please show us your C# code. – Bruno Lowagie Aug 15 '18 at 07:12
  • The first screenshot shows how it looks when displayed on the browser. I have already included the code that generate the table in c#. Observation: If I remove the loop within the tbody loop the table structure becomes ok. – codegrid Aug 15 '18 at 07:26

0 Answers0