
本教程详细介绍了在nreco.pdfgenerator中自定义pdf页面编号的两种高级方法。首先,通过`generatepdffromfiles`方法结合`--page-offset`参数,实现对不同html输入文件的起始页码控制;其次,展示了如何通过修改页脚html中的javascript代码,实现更灵活的页码逻辑定制,例如跳过特定页码。这些方法能帮助开发者精确控制生成pdf的页码显示。
NReco.PdfGenerator是一个基于wkhtmltopdf的.NET库,用于将HTML内容转换为PDF。在生成PDF时,页码的显示是一个常见的需求,而默认的页码通常是按顺序递增的(例如:1, 2, 3...)。然而,在某些特定场景下,我们可能需要更灵活的页码控制,例如让第一页显示为“Page 1”,而第二页直接显示为“Page 3”,跳过“Page 2”。本文将详细介绍两种实现这种高级页码自定义的方法。
当您的PDF内容可以逻辑上分割成多个独立的HTML文件时,这种方法非常适用。通过为每个HTML文件指定不同的页码偏移量,可以实现灵活的页码起始设置。
核心原理: NReco.PdfGenerator的GeneratePdfFromFiles方法允许您传入一个WkHtmlInput对象数组,每个对象代表一个要转换的HTML文件。WkHtmlInput类提供了一个CustomWkHtmlPageArgs属性,您可以在其中设置wkhtmltopdf的命令行参数,包括--page-offset。--page-offset N参数会将当前文件的所有页码都加上N。
实现步骤:
示例代码:
以下示例展示了如何生成一个PDF,其中第一个HTML文件(first.html)的页码从1开始,而第二个HTML文件(main.html)的页码从3开始。
using NReco.PdfGenerator;
using System;
using System.IO; // For creating dummy HTML files
public class PdfPageNumberCustomization
{
public void GeneratePdfWithOffsetPages()
{
// 创建模拟的HTML文件内容
string firstPageHtml = "<html><body><h1>First Page</h1><p>This is the content for page 1.</p></body></html>";
string mainPageHtml = "<html><body><h1>Main Content</h1><p>This section will start from page 3.</p><p>More content...</p></body></html>";
// 将内容写入临时文件,或直接使用HTML字符串
// 这里为了演示WkHtmlInput,我们假设有文件
File.WriteAllText("first.html", firstPageHtml);
File.WriteAllText("main.html", mainPageHtml);
var htmlToPdf = new HtmlToPdfConverter();
// 定义统一的页脚HTML
string footerHtml = "<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>";
htmlToPdf.GeneratePdfFromFiles(
new WkHtmlInput[]
{
// 第一个文件,页码从1开始,无需特殊偏移
new WkHtmlInput("first.html")
{
PageFooterHtml = footerHtml
},
// 第二个文件,设置页码偏移量为2。
// 如果wkhtmltopdf默认会将其内部页码计为1,那么1 + 2 = 3。
// 这样,第二个文件的第一页将显示为“Page 3”。
new WkHtmlInput("main.html")
{
CustomWkHtmlPageArgs = " --page-offset 2 ", // 页码将变为: 3, 4, 5 等
PageFooterHtml = footerHtml
}
},
null, // 可选的封面页HTML
"output_with_offset.pdf"
);
Console.WriteLine("PDF generated with custom page offsets: output_with_offset.pdf");
// 清理临时文件
File.Delete("first.html");
File.Delete("main.html");
}
}适用场景与注意事项:
这种方法提供了更细粒度的控制,允许您在页脚的HTML中嵌入JavaScript代码,直接修改页码的显示逻辑。wkhtmltopdf在渲染页脚时会执行其中包含的JavaScript。
核心原理: wkhtmltopdf在页脚或页眉中遇到特定类名(如<span class="page"></span>)时,会通过一个内置的JavaScript函数来填充这些占位符。这个函数通常命名为subst,并从URL查询字符串中获取页码等信息。通过在您的PageFooterHtml中定义一个同名的window.subst函数,您可以覆盖其默认行为,实现自定义的页码计算和显示逻辑。
实现步骤:
示例代码:
以下示例展示了如何实现“第一页显示为Page 1,第二页显示为Page 3,之后按顺序递增”的逻辑。
using NReco.PdfGenerator;
using System;
public class PdfAdvancedPageNumberCustomization
{
public void GeneratePdfWithCustomJsPages()
{
var htmlContent = String.Format("<body><h1>Page 1 Content</h1><p>This is the first page.</p><div style='page-break-before: always;'></div><h1>Page 2 Content (will display as Page 3)</h1><p>This is the second page, but we want it to be page 3.</p><div style='page-break-before: always;'></div><h1>Page 3 Content (will display as Page 4)</h1><p>This is the third page, but we want it to be page 4.</p></body>", DateTime.Now);
var htmlToPdf = new HtmlToPdfConverter();
// 包含自定义JavaScript的页脚HTML
htmlToPdf.PageFooterHtml = @"
<script>
window.subst = function() {
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]];
// 核心逻辑:如果当前页码大于1,则将其加1
// 这样,第1页显示1,第2页显示3,第3页显示4,以此类推。
if (x[i]=='page' && parseInt(val)>1) {
val = parseInt(val)+1;
}
for(var j=0; j<y.length; ++j) y[j].textContent = val;
}
}
</script>
<table border-bottom: 1px solid black; width: 100%"">
<tr>
<td class=""section""></td>
<td text-align:right"">Page <span class=""page""></span></td>
</tr>
</table>
";
var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);
// 将生成的PDF保存到文件
File.WriteAllBytes("output_with_custom_js_pages.pdf", pdfBytes);
Console.WriteLine("PDF generated with custom JS page numbering: output_with_custom_js_pages.pdf");
}
}代码解析:
适用场景与注意事项:
NReco.PdfGenerator结合wkhtmltopdf提供了强大的PDF生成能力,包括对页码的灵活控制。
选择哪种方法取决于您的具体需求和文档结构。在实现过程中,建议充分测试以确保页码显示符合预期。
以上就是NReco.PdfGenerator:高级页面编号自定义教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号