将HTML转换成PDF时如何添加页码

3

我正在使用一个PDF生成器

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

我想要添加页码。

以下是我的代码:

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

有什么想法吗?

1个回答

1
我在我的HTML中添加了以下代码,然后它工作得很好 :)
<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接