c# - 在 Rotativa 生成的 PDF 中显示页眉和页脚

标签 c# asp.net-mvc-3 pdf-generation rotativa

我正在尝试在 Rotativa 库生成的 PDF 中指定页眉和页脚。正如作者回答here应该可以使用 CSS(描述 here )。但是,我无法执行此操作。

我在元标记中加载了一个样式表:

<link href="print.css" rel="stylesheet" type="text/css" media="print" />

在底部的样式表中:

@page {
    @top-left {
        content: "TOP SECRET";
        color: red
    }
    @bottom-right {
        content: counter(page);
        font-style: italic
    }
}

然后通过以下方式生成 PDF:

public ActionResult ShowPdf()
{
     var model = new Model();
     return new ViewAsPdf("view.cshtml", model)
                {
                    FileName = "Report.pdf",
                    CustomSwitches = "--print-media-type"
                };
}

然后 PDF 的页眉和页脚中什么也没有出现。有什么想法吗?

最佳答案

我找到了 documentation of wkhtmltopdf (或更好的 archived version )并且在那里描述了如何管理页眉和页脚。

基本上,您只需将 --header-center "text"(或类似的开关)添加到参数列表即可。

因此将它与 Rotativa 一起使用将是:

public ActionResult ShowPdf()
{
     var model = new Model();
     return new ViewAsPdf("view.cshtml", model)
                {
                    FileName = "Report.pdf",
                    CustomSwitches = "--print-media-type --header-center \"text\""
                };
}

(不知道是否需要--print-media-type。)

关于c# - 在 Rotativa 生成的 PDF 中显示页眉和页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15250505/

相关文章:

c# - WPF多个按钮,点击功能相同但参数不同

c# - c#中的多个附件

asp.net - 我的 Asp.Net MVC 3 站点中出现奇怪的错误

c# - 合并 pdf 与 pdfsharp : exception en the pdfDocument. close()

java - 在浏览器中渲染 docx 文件

c# - 如何在不需要提供用户详细信息的情况下从特定的远程分支中提取最新信息?

c# - 即将到期的 Lazy<T> 类

c# - 如何从 mvc3 中的 Html.ActionLink 中废弃 Controller 名称

java - 如何避免PDF添加内容后签名失效?

c# - 如何强制执行 Catch block ?