This question pertains to NReco's PdfGenerator component.
I use this product to convert dynamically generated HTML string to a Pdf document within the .NET MVC framework using C#.
While looking for ways to add page numbers (e.g., 1 of 5) to the footer of the Pdf, I came across this and this on SO. And not surprisingly, both options seem to offer a similar approach to accomplishing the same goal.
While the code itself makes sense, what I'm having a hard time understanding is this - My document content (or the HTML string) is generated inside a View. The HTML string is then passed to a Controller (.cs) for the actual conversion process. With my very limited knowledge on MVC framework, I think there's no way you can add JavaScript code to the Controller (or is there?).
So, I don't quite understand how the above two JavaScript based methods can be incorporated inside my C# function that handles the document conversion. Or is this something that should be done inside the View?
Controller:
[HttpPost]
public ActionResult Html2Pdf(FormCollection form) {
var docTitle = form["doctitle"].ToString();
var headerHtml =
"<div style='width:100%; margin-top:1em; display:block;'>" +
"<img src='" + System.Web.HttpContext.Current.Server.MapPath("~") + "/media/images/document_banner.png' />" +
"</div>" +
"<div style='width:100%; bottom:110px; left:0; position:relative; display:block;'>" +
"<span style='color:#fff; font-size:2.5em; font-family:georgia; margin-left:1em;'>" + docTitle + "</span>" +
"</div>";
var footerHtml =
"<div style='width:100%; text-align:center; border-top:1px solid #abc; margin-top:2em;'>Page 0 of 0</div>;
var htmlToPdf = new HtmlToPdfConverter();
// various parameters get set here
htmlToPdf.PageHeaderHtml = headerHtml;
htmlToPdf.PageFooterHtml = footerHtml;
....
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=MyTestDocument.pdf");
htmlToPdf.GeneratedPdf(form["htmlcontent"].ToString(), null, Response.OutputStream); // form["htmlcontent"] holds the document body
Response.End();
Return new EmptyResult();
}
Drag and drop the PDF or select a file to upload it to Adobe's online PDF editor. Select the add text control. It's the large capital T at the top of the page. Click on the page where you'd like the page number to appear and type the number.
You don't need to append javascript code (from wkhtmltopdf help) for rendering page number because PdfGenerator will do that for you if you set PageHeaderHtml or PageFooterHtml properties. All you need is just mark with "page" class element where you want to render page number:
htmlToPdf.PageHeaderHtml = "<div>Page: <span class="page"></span></div>";
that's all.
You can use this code if you want to display also the total number page (topage class):
var generator = new NReco.PdfGenerator.HtmlToPdfConverter();
generator.PageFooterHtml = $@"page <span class=""page""></span> of <span class=""topage""></span>";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With