This project has moved and is read-only. For the latest updates, please go here.

Rendering HTML into Header

Jun 11, 2013 at 5:15 PM
Hi. Thanks a lot for your very useful work.

Is there anyway for so that I can render my header with html tags? something like this:
<table> <tr> <td> //something </td> </tr> //and so on... </table> thanks in advance
Jun 11, 2013 at 5:21 PM
Hi, Yes. I have some samples about this subject here (HtmlHeader, HtmlHeaderRtl & HtmlCellTemplate)
Jun 11, 2013 at 5:49 PM
Ok. I have this code for header. However it causes to 'object null' exceptions. What's my mistake?
        .PagesHeader(header =>
            header.HtmlHeader(rptHeader =>
                 rptHeader.PageHeaderProperties(new HeaderBasicProperties
                     RunDirection = PdfRunDirection.RightToLeft,
                     HorizontalAlignment = HorizontalAlignment.Center,
                     ShowBorder = true,
                     PdfFont = header.PdfFont,
                 rptHeader.AddPageHeader(pageHeader =>
                     using (PtDbContext db = new PtDbContext())
                         Course c = db.Courses.Find(_cid);
                         string html = string.Format(@"<table>
                                            <tr><td colspan='6'>لیست حضور رو غیاب دوره<hr style='border-bottom:dotted;' /><td></tr>
                                                <td>شناسه دوره:</td>
                                                <td style='font-weight:bold'>{0}</td>
                                                <td>عنوان دوره:</td>
                                                <td style='font-weight:bold'>{1}</td>
                                                <td>نام استاد:</td>
                                                <td style='font-weight:bold'>{2} &nbsp; {3}</td>
                                                <td>تاریخ شروع:</td>
                                                <td style='font-weight:bold'>{4}</td>
                                                <td>تاریخ پایان:</td>
                                                <td style='font-weight:bold'>{5}</td>
                                                <td>محل تشکیل:</td>
                                                <td style='font-weight:bold'>{6}</td>
                                            </tr></table>", _cid, c.Title.TitleName, c.Teacher.FirstName, c.Teacher.LastName,
                                                          c.BeginDate, c.EndDate, c.Location);
                         return html;
Jun 11, 2013 at 5:58 PM
Edited Jun 11, 2013 at 6:02 PM
You need to investigate the stack trace, to find out what is the source of null reference. probably one of your Course property values is null. put a break-point on the string HTML line and then verify the content of all of the used values here.
It's better to fetch the required values outside of the rptHeader.AddPageHeader. because it's a callback and it's not necessary to fetch this data on every new page again.
Jun 12, 2013 at 3:31 AM
That's not because of those properties. I removed them from header, init them in the constructor and replace them with local vars. They all have expected values. However, I Looked at exceptions detail and found the except source:

at {Void SafeUsingBlock[TDisposable,T](TDisposable, System.Action1[T], System.Func2[TDisposable,T])}

any suggestion?
Jun 12, 2013 at 5:05 AM
That's because there is an exception somewhere in your codes (and I can't debug your codes remotely without having the complete stack trace).
To see that exception clearly, try Generate(data => data... method with this additional parameter debugMode:true.
Jun 13, 2013 at 3:21 AM
Hi. the complete stack trace is like the following and now the message (after I added debugMode: true, it changed) is :
Object reference not set to an instance of an object.

Complete stack trace:
   at iTextSharp.text.html.simpleparser.HTMLWorker.CreateLineSeparator(IDictionary`2 attrs)
   at iTextSharp.text.html.simpleparser.HTMLTagProcessors.HTMLTagProcessor_HR.StartElement(HTMLWorker worker, String tag, IDictionary`2 attrs)
   at iTextSharp.text.html.simpleparser.HTMLWorker.StartElement(String tag, IDictionary`2 attrs)
   at iTextSharp.text.xml.simpleparser.SimpleXMLParser.ProcessTag(Boolean start)
   at iTextSharp.text.xml.simpleparser.SimpleXMLParser.Go(TextReader reader)
   at iTextSharp.text.xml.simpleparser.SimpleXMLParser.Parse(ISimpleXMLDocHandler doc, ISimpleXMLDocHandlerComment comment, TextReader r, Boolean html)
   at iTextSharp.text.html.simpleparser.HTMLWorker.Parse(TextReader reader)
   at iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(TextReader reader, StyleSheet style, IDictionary`2 tags, Dictionary`2 providers)
   at PdfRpt.Core.Helper.HtmlWorkerHelper.RenderHtml()
   at PdfRpt.HeaderTemplates.HtmlHeaderProvider.createTable(String html, HeaderBasicProperties basicProperties)
   at PdfRpt.HeaderTemplates.HtmlHeaderProvider.RenderingReportHeader(Document pdfDoc, PdfWriter pdfWriter, IList`1 summaryData)
   at PdfRpt.Core.Helper.HeaderFooterManager.AddHeader(PdfWriter writer, Document document)
   at PdfRpt.Core.PdfTable.PageEvents.OnStartPage(PdfWriter writer, Document document)
   at iTextSharp.text.pdf.PdfDocument.InitPage()
   at iTextSharp.text.pdf.PdfDocument.Open()
   at iTextSharp.text.Document.Open()
   at PdfRpt.PdfReportDocument.createPdf()
   at PdfRpt.PdfReportDocument.runInDebugMode()
   at PdfRpt.PdfReportDocument.GeneratePdf(Boolean debugMode)
   at PdfRpt.FluentInterface.PdfReport.Generate(Action`1 pdfRptFileBuilder, Boolean debugMode)
   at jqPersonnelTraining.Reports.RollReport.CreatePdfReport() in c:\Users\Amin\Documents\Visual Studio 2012\Projects\jqPersonnelTraining\jqPersonnelTraining\Reports\RollReport.cs:line 40
   at jqPersonnelTraining.Controllers.HoldCourseController.GetReportFileName(String id) in c:\Users\Amin\Documents\Visual Studio 2012\Projects\jqPersonnelTraining\jqPersonnelTraining\Controllers\HoldCourseController.cs:line 115
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
Jun 19, 2013 at 7:07 AM
Edited Jun 19, 2013 at 7:12 AM
That exception is being raised in HTMLWorker.ParseToList method of iTextSharp,
In \core\iTextSharp\text\html\simpleparser\HTMLWorker.cs file here:
public LineSeparator CreateLineSeparator(IDictionary<String, String> attrs) {
            return factory.CreateLineSeparator(attrs, currentParagraph.Leading / 2);
because hr tag is not supported by HTMLWorker worker of iTextSharp (currentParagraph is null here). (as I told you before)
HTMLWorker is not supported by iTextSharp team anymore. They suggest using their XML Worker. But I'm not happy with that, because it does not support Unicode and RTL.
Also HTMLWorker does not support RTL by default and I applied a lot of changes and hacks here to make it work.

Remove the hr tag or download the source code of iTextSharp and fix it yourself (you need to fix the CreateLineSeparator method of HTMLWorker.cs file by adding the following lines at the beginning of the method
            if (currentParagraph == null) {
                currentParagraph = CreateParagraph();

This is the mailing list of iText/iTextSharp