3

I know it is possible to render "Page X of Y" in a header/footer using the C# API like this:

//write the page number
TextElement footerText = new TextElement(0, pdfConverter.PdfFooterOptions.FooterHeight - 15, "This is page &p; of &P;  ",
    new System.Drawing.Font(new System.Drawing.FontFamily("Times New Roman"), 10, System.Drawing.GraphicsUnit.Point));
footerText.EmbedSysFont = true;
footerText.TextAlign = HorizontalTextAlign.Right;
pdfConverter.PdfFooterOptions.AddElement(footerText);

..but i'd rather position and style it directly in html using:

 HtmlToPdfElement footerHtml = new HtmlToPdfElement(pdfOptions.DocumentFooterHtmlString, pdfOptions.BaseUrl);
 footerHtml.FitHeight = true;
 pdfConverter.PdfFooterOptions.AddElement(footerHtml);

where pdfOptions.DocumentFooterHtmlString looks something like this:

<div class="clearfix">
    <span class="pull-right">This is page &p; of &P;</span>
</div>

Is this something that is possible? If I try this I just get the: This is page &p; of &P; rendered in the footer.

2 Answers2

6

I struggled with this briefly. For the sake of others who find this through search, the trick is that when you add the HTML element, it has to be a HtmlToPdfVariableElement instead of a HtmlToPdfElement.

HtmlToPdfVariableElement footerHtml = new    HtmlToPdfVariableElement(pdfOptions.DocumentFooterHtmlString, pdfOptions.BaseUrl);
footerHtml.FitHeight = true;
pdfConverter.PdfFooterOptions.AddElement(footerHtml);
Sarah Carter
  • 86
  • 2
  • 3
3

It looks like you can do this. See http://www.evopdf.com/demo/PDF_Creator/Headers_and_Footers/Page_Numbers_in_HTML.aspx

In the sample code Evo Provides they use HtmlToPdfVariableElement.

Sam Plus Plus
  • 4,381
  • 2
  • 21
  • 43
  • Sadly this doesn't seem to work at the same time as [dynamic header/footer heights](http://www.evopdf.com/help/html_to_pdf/html/284d1e81-5fb2-4e76-a050-321dfb7a8df2.htm). I have emailed evo about this. – 8128 Aug 13 '18 at 08:51
  • Update to my comment about using this with dynamic header/footer heights - Evo told me that it wasn't possible. I found a workaround where I found the desired dynamic height using HtmlToPdfElement and then re-rendered using a HtmlToPdfVariableElement. – 8128 Aug 15 '18 at 07:51