Add page number whle converting HTML to PDF

烈酒焚心 提交于 2019-12-10 21:26:04

问题


I am using a PDF generator

http://www.nrecosite.com/pdf_generator_net.aspx

I would like to add the poge number as well.

following is my code:

string htmlContent = "....";
 var generator=new NReco.PdfGenerator.HtmlToPdfConverter();

            generator.Orientation = NReco.PdfGenerator.PageOrientation.Landscape;
            generator.PageHeaderHtml= "<img style='width:50px' src='"+HttpContext.Current.Server.MapPath("~") +"/test.png' />";
            generator.PageFooterHtml = "<h1>test </h1>";
            var pdfBytes = generator.GeneratePdf(htmlContent);


            System.IO.File.WriteAllBytes(HttpContext.Current.Server.MapPath("~") + "/hello2.pdf", pdfBytes);

Any Idea?


回答1:


I added following code in my HTML then it worked well :)

<html>
<head>
    <script>
        function subst() {
            var vars = {};
            var x = document.location.search.substring(1).split('&');
            for (var i in x) { var z = x[i].split('=', 2); vars[z[0]] = unescape(z[1]); }
            var x = ['frompage', 'topage', 'page', 'webpage', 'section', 'subsection', 'subsubsection'];
            for (var i in x) {
                var y = document.getElementsByClassName(x[i]);
                for (var j = 0; j < y.length; ++j) y[j].textContent = vars[x[i]];
            }
        }
    </script>
</head>
<body style="border: 0; margin: 0;" onload="subst()">
    <table style="border-bottom: 1px solid black; width: 100%">
        <tr>
            <td class="section">
            </td>
            <td style="text-align: right">
                Page <span class="page"></span>of <span class="topage"></span>
            </td>
        </tr>
    </table>
</body>
</html>


来源:https://stackoverflow.com/questions/26898876/add-page-number-whle-converting-html-to-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!