razorPDF - 自动页码

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>       

你有没有找到解决方案? - Girish Sakhare
很抱歉,我没有解决方案! - J-Man
相关问题:https://dev59.com/0V8e5IYBdhLWcg3w-OYn - 那个问题有一个更好的解决方案,但它并没有回答这个问题。 - Chad
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);

我在代码中有一些额外的自定义 - 但这应该是基本的量,可以为每个页面底部提供“第x页共y页”的页脚。
编辑:由于解决方案仅限于免费选项,建议查看iTextSharp。如果您正在使用NuGet,可以通过NuGet软件包获取它。
至于使用iTextSharp,好的起点包括: iTextSharp教程 如何使用iTextSharp将HTML转换为PDF 如何在Itextsharp中的页脚中添加页码

仅限于不需要付费使用的解决方案。 - Chad
嗯...我会为你找找看。HiQ有一个免费版本,但它会给所有东西打上水印。RazorPdf除了将视图呈现为PDF之外,没有任何其他功能。 - Bardicer

1
根据Nick的建议,快速答案是不支持。RazorPDF使用较旧版本的iTextSharp,我认为这是最新的免费iTextSharp版本。最新版本(5.5.5)可在AGPL许可下使用。如果渲染HTML不是必要条件,可以查看PDFsharp&MigraDoc。MigraDoc提供页面编号功能,如此处所示。希望这可以帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接