添加页码whle HTML转换为PDF [英] Add page number whle converting HTML to PDF

查看:489
本文介绍了添加页码whle HTML转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是PDF生成器

I am using a PDF generator

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

我也想添加poge号码。

I would like to add the poge number as well.

以下是我的代码:

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

任何想法?

推荐答案

我在HTML中添加了以下代码,然后效果很好:)

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>

这篇关于添加页码whle HTML转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆