自定义NReco.PdfGenerator中的页面编号

huangapple go评论56阅读模式
英文:

Customize Page Number in NReco.PdfGenerator

问题

目前在页脚已经写了页码,第一页是:第1页,第二页是:第2页,以此类推。

我想要实现的是第一页:第1页,第二页:第3页。

我该如何自定义这些数字?

public void GenerateHtmlToPdf()
 {
   var htmlContent = String.Format("<body>Hello world: {0}</body>",DateTime.Now);
   var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
   htmlToPdf.PageFooterHtml = "<table style=/"border-bottom: 1px solid black; width: 100%/"><tr><td class=/"section/"></td><td style=/"text-align:right/">Page <span class=/"page/"></span></td></tr></table>";
   var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);
...
}
英文:

Currently in footer already write page number which are first page : Page 1, second page : Page 2 and so on.

I am trying to achieve is first page : Page 1 and second page: Page 3.

How can I customize those number?

public void GenerateHtmlToPdf()
 {
   var htmlContent = String.Format("<body>Hello world: {0}</body>",DateTime.Now);
   var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
   htmlToPdf.PageFooterHtml = "<table style=/"border-bottom: 1px solid black; width: 100%/"><tr><td class=/"section/"></td><td style=/"text-align:right/">Page <span class=/"page/"></span></td></tr></table>";
   var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);
...
}

答案1

得分: 1

以下是已翻译的内容:

方法 #1

如果您可以将内容分成两个不同的HTML输入(一个用于第一页,应该从第一页开始,另一个应该从第三页开始),那么您可以使用GeneratePdfFromFiles方法,并为第二个文件指定从第3页开始(使用wkhtmltopdf选项“--page-offset”):

var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
htmlToPdf.GeneratePdfFromFiles(
  new WkHtmlInput[] {
    new WkHtmlInput("first.html") {  
      PageFooterHtml = "<table style=\"border-bottom: 1px solid black; width: 100%\"><tr><td class=\"section\"></td><td style=\"text-align:right;\">Page <span class=\"page\"></span></td></tr></table>" },
    new WkHtmlInput("main.html"){
        CustomWkHtmlPageArgs = " --page-offset 2 ",    // 页码将为:3, 4, 5 等
        PageFooterHtml = "<table style=\"border-bottom: 1px solid black; width: 100%\"><tr><td class=\"section\"></td><td style=\"text-align:right;\">Page <span class=\"page\"></span></td></tr></table>" },
    }
    null, // 可选的封面页 HTML
    "output.pdf"
);

方法 #2

您可以覆盖默认的JS代码(将页码插入到<span class="page"></span>占位符中)在页脚模板中。要了解这个JS代码如何工作,请参考wkhtmltopdf帮助(“Footers And Headers”部分):https://wkhtmltopdf.org/usage/wkhtmltopdf.txt

以下是一个这种覆盖的示例:

htmlToPdf.PageFooterHtml = @"
    <script>
    window.subst = function() {
        var pageOffset = 20;  // 第一页将成为第21页
        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]);
            var val = vars[x[i]];
            if (x[i]=='page' && parseInt(val)>1)
                val = parseInt(val)+1;  // 页码将为:1, 3, 4, 5 等
            for(var j=0; j<y.length; ++j) y[j].textContent = val;
        }
    }
    </script>  

    <table style=\"border-bottom: 1px solid black; width: 100%\"><tr><td class=\"section\"></td><td style=\"text-align:right;\">Page <span class=\"page\"></span></td></tr></table>
";
英文:

There are 2 ways how you can achieve the desired result:

Approach #1

If you can split your content on 2 different HTML inputs (one for the 1st page which should have page number #1, and another one that should start from page number #3) then you can use GeneratePdfFromFiles method and specify for 2nd file that it should start from page number = 3 (with wkhtmltopdf option "--page-offset"):

var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
htmlToPdf.GeneratePdfFromFiles(
  new WkHtmlInput[] {
    new WkHtmlInput(&quot;first.html&quot;) {  
	    PageFooterHtml = &quot;&lt;table style=/&quot;border-bottom: 1px solid black; width: 100%/&quot;&gt;&lt;tr&gt;&lt;td class=/&quot;section/&quot;&gt;&lt;/td&gt;&lt;td style=/&quot;text-align:right/&quot;&gt;Page &lt;span class=/&quot;page/&quot;&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&quot; },
    new WkHtmlInput(&quot;main.html&quot;){
        CustomWkHtmlPageArgs = &quot; --page-offset 2 &quot;,    // page numbers will be: 3, 4, 5 etc
        PageFooterHtml = &quot;&lt;table style=/&quot;border-bottom: 1px solid black; width: 100%/&quot;&gt;&lt;tr&gt;&lt;td class=/&quot;section/&quot;&gt;&lt;/td&gt;&lt;td style=/&quot;text-align:right/&quot;&gt;Page &lt;span class=/&quot;page/&quot;&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&quot; },
	}
    null, // optional cover page html
	&quot;output.pdf&quot;
);  

Approach #2

You may override default JS code (that inserts a page number into &lt;span class=&quot;page&quot;&gt;&lt;/span&gt; placeholder) in the footer's template. To get an understanding how this JS code works take a look at the wkhtmltopdf help ("Footers And Headers" section): https://wkhtmltopdf.org/usage/wkhtmltopdf.txt

Here an example of such override:

   htmlToPdf.PageFooterHtml = @&quot;
            &lt;script&gt;
window.subst = function() {
	var pageOffset = 20;  // first page will become 21
	var vars={};
	var x=document.location.search.substring(1).split(&#39;&amp;&#39;);
	for(var i in x) {var z=x[i].split(&#39;=&#39;,2);vars[z[0]] = unescape(z[1]);}
	var x=[&#39;frompage&#39;,&#39;topage&#39;,&#39;page&#39;,&#39;webpage&#39;,&#39;section&#39;,&#39;subsection&#39;,&#39;subsubsection&#39;];
	for(var i in x) {
		var y = document.getElementsByClassName(x[i]);
		var val = vars[x[i]];
		if (x[i]==&#39;page&#39; &amp;&amp; parseInt(val)&gt;1)
			val = parseInt(val)+1;  // page numbers will be: 1, 3, 4, 5 etc
		for(var j=0; j&lt;y.length; ++j) y[j].textContent = val;
	}
}
&lt;/script&gt;  
   
   
 &lt;table style=&quot;&quot;border-bottom: 1px solid black; width: 100%&quot;&quot;&gt;&lt;tr&gt;&lt;td class=&quot;&quot;section&quot;&quot;&gt;&lt;/td&gt;&lt;td style=&quot;&quot;text-align:right&quot;&quot;&gt;Page &lt;span class=&quot;&quot;page&quot;&quot;&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&quot;;

huangapple
  • 本文由 发表于 2023年6月12日 09:47:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定