I am using following code to create header in selectPDF.

string headerUrl = ViewToHtmlAsync("~/Views/HeaderTemplate/Header.cshtml");

PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl,"https://selectpdf.com/demo/files/header.html");
converter.Header.Add(headerHtml);

ViewToHtmlAsync is the function that convert .cshtml file to html string. The Header.cshtml will generate following template.

enter image description here

But currently the page number data "1 of 2" is hardcoded.

My question is how do I pass the page number from the controller to Header.cshtml and put it inside the table so that I got the correct page number data on each page? It seems like only the placeholder {page_number} and {total_pages} can generate the page number? If yes, how do I pass it into my Header.cshtml? Thanks for any help.

1

There are 1 best solutions below

2
On

Disclaimer: I work for SelectPdf.

Placeholders for page numbers cannot be used inside HTML. If you need the page number in the header of the pdf, you would need to use a PdfTextSection object:

// page numbers can be added using a PdfTextSection object
PdfTextSection text = new PdfTextSection(0, 10, "Page: {page_number} of {total_pages}  ", new System.Drawing.Font("Arial", 8));
text.HorizontalAlign = PdfTextHorizontalAlign.Right;
converter.Header.Add(text);