英文:
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("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 ", // page numbers will be: 3, 4, 5 etc
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, // optional cover page html
"output.pdf"
);
Approach #2
You may override default JS code (that inserts a page number into <span class="page"></span>
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 = @"
<script>
window.subst = function() {
var pageOffset = 20; // first page will become 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; // page numbers will be: 1, 3, 4, 5 etc
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>
";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论