Evo Pdf - Page numbering from within HTML

4.1k views Asked by At

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

There are 2 answers

2
Sam Plus Plus On

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.

1
Sarah Carter On

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);