4

使用 C# MVC,我通过 PdfResult 传递 HTML 部分视图来创建报告。除了缺少页码之外,这正好提供了所需的内容。

GetDetails只需调用一个返回结果列表的服务。然后printlist使用结果列表作为模型调用视图(见下文)。

RazorPDF 是否提供在报告上添加页码的方法?

    public ActionResult PrintReport(DateTime printDate)
    {
        var printModel = printController.GetDetails(printDate);
        var pdfDoc = new PdfResult(printModel , "printlist");

        return pdfDoc;
    }

看法:

<paragraph style="font-family:Arial;font-size:18;">
    <table width="100%" cellpadding="1.0" cellspacing="1.0" widths="5;5">
        <row>
            <cell borderwidth="0.5" left="false" right="false" top="false" bottom="false">
                <chunk style="font-size:14;font-weight:bold;">@ViewBag.Title</chunk>
            </cell>
            <cell borderwidth="0.5" left="false" right="false" top="false" bottom="false" horizontalalign="Right">
                <chunk style="font-size:14;font-weight:bold;">@Model.First().appointment_date.ToString().Substring(0,10)</chunk>
            </cell>
        </row>
    </table>
</paragraph>

<table width="100%" cellpadding="1.0" cellspacing="1.0" widths="4;10;7;14;4;4;4;3;4;4;4;4">
    <row>
        <cell borderwidth="0.5" left="false" right="false" top="true" bottom="true"><chunk>Time</chunk></cell>
        <cell borderwidth="0.5" left="false" right="false" top="true" bottom="true"><chunk>Name</chunk></cell>
        <cell borderwidth="0.5" left="false" right="false" top="true" bottom="true"><chunk>Number</chunk></cell>
        <cell borderwidth="0.5" left="false" right="false" top="true" bottom="true"><chunk>Reason</chunk></cell>
    </row>

        @foreach (var item in Model)
        {
            <row>
                <cell>@item.appointmentStart</cell>
                <cell>@item.surname,@item.forename</cell>
                <cell>@item.customerIdentifier</cell>
                <cell>@item.reason</cell>
            </row>
        }
</table>       
4

2 回答 2

3

RazorPDF 无法在 PDF 文件中设置页码。这是因为它具有 Object -> ActionResult -> ViewResultBase -> ViewResult 中可用的属性(您可以通过展开引用并双击 RazorPDF 在 Visual Studio 中查看这些属性)。

要添加页码,您必须在视图本身中进行计算以确定分页符的位置并将其放入。您可以通过向视图传递一个参数来表示它是否用于创建一个 pdf 并仅显示当时的页码。

我遇到了类似的问题并尝试了各种软件包(RazorPDF、iTextSharp 和 HiQPdf)。如果您不限于 RazorPDF,我建议您查看 HiQPdf。您基本上可以设置一个定义页眉和页脚元素的 ActionResult,将视图的 html 传递给它,它会使用您想要的页眉和页脚(如果您设置它们)呈现 PDF。对于页码,它很简单:

 var pdfWorker = new HtmlToPdf();
 var html = "Html as string including tags";
 var url = "Whatever url you are converting - still works if empty string";
 pdfWorker.Document.Footer.Enabled = true;
 var pageNumberFont = new Font(new FontFamily("Arial"), 8, GraphicsUnit.Point);
 var pageNumberText = new PdfText(x-position, y-position, "Page {CrtPage} of {PageCount}, pageNumberFont);
 pdfWorker.Document.Footer.Layout(pageNumberText);
 return pdfWorker.ConvertHtmlToPdfDocument(html, url);

我的代码中有一些额外的自定义 - 但这应该是在每页底部为您提供“Page x of y”的页脚的基本数量。

编辑:由于解决方案仅限于免费选项,我建议查看iTextSharp。如果您使用的是 NuGet,您可以通过 NuGet 包获取它。

至于使用 iTextSharp,好的起点是:

iTextSharp 教程

如何使用 iTextSharp 将 HTML 转换为 PDF

如何通过 Itextsharp 在 PDF 的页脚中添加页码

于 2015-04-22T16:37:44.917 回答
1

Nick 建议的快速回答是否定的,RazorPDF 不支持自动向文档添加页码的功能。

RazorPDF 使用旧版本的 iTextSharp,我相信它是最新的免费 iTextSharp 版本。最新版本 (5.5.5) 在 AGPL 许可下可用。

RazorPDF 中的最新 iTextSharp 版本

包 id="iTextSharp" 版本="4.1.2" targetFramework="net40"

如果渲染 HTML 不是一个大问题,你可以看看PDFsharp & MigraDoc。MigraDoc 提供页码功能,如此处所示

希望这可以帮助。

于 2015-04-23T13:48:56.610 回答