简体   繁体   English

razorPDF - 自动页码编号

[英]razorPDF - automatic page numbering

Using C# MVC I am creating a report by passing a HTML partial view through PdfResult. 使用C#MVC我通过PdfResult传递HTML部分视图来创建报告。 This provides exactly what is needed except for the lack of page numbers. 除了缺少页码之外,这确实提供了所需的内容。

The GetDetails simply calls a service that returns a list of results. GetDetails只调用返回结果列表的服务。 The printlist view is then called using the list of results as a model (see below). 然后使用结果列表作为模型调用printlist视图(参见下文)。

Does RazorPDF provide a way to put page numbers on the report? RazorPDF是否提供了在报告中放置页码的方法?

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

        return pdfDoc;
    }

View: 视图:

<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>       

RazorPDF does not have the ability to set the page numbers in the PDF file. RazorPDF无法在PDF文件中设置页码。 This is because it has the properties available from Object -> ActionResult -> ViewResultBase -> ViewResult (you can view these in visual studio by expanding the references and double clicking RazorPDF). 这是因为它具有Object - > ActionResult - > ViewResultBase - > ViewResult提供的属性(您可以通过扩展引用并双击RazorPDF在visual studio中查看这些属性)。

To add page numbers, you would have to make the calculations in the view itself to determine where the page breaks should be and put them in. You could potentially do this by passing a parameter to the view denoting whether or not it is being used to create a pdf and only show the page numbers at that time. 要添加页码,您必须在视图中进行计算以确定分页的位置并将其放入。您可以通过将参数传递给视图来表示是否正在使用它来执行此操作。创建一个pdf并仅显示当时的页码。

I had a similar problem and tried various packages (RazorPDF, iTextSharp, and HiQPdf). 我遇到了类似的问题并尝试了各种软件包(RazorPDF,iTextSharp和HiQPdf)。 If you're not limited to RazorPDF, I'd recommend looking into HiQPdf. 如果您不限于RazorPDF,我建议您查看HiQPdf。 You can basically set up an ActionResult that defines the header and footer elements, pass the view's html to it, and it'll render a PDF with whatever you want for your header and footer (if you set them). 您基本上可以设置一个定义页眉和页脚元素的ActionResult,将视图的html传递给它,它将为您的页眉和页脚提供任何您想要的PDF(如果您设置它们)。 For page numbering it was as easy as: 对于页码编号,它很简单:

 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);

I have some extra customizations in my code - but that should be the basic amount to give you a footer with "Page x of y" at the bottom of every page. 我的代码中有一些额外的自定义 - 但这应该是在每个页面底部为您提供“Page x of y”页脚的基本数量。

EDIT: Since the solution is limited to only free options, I would recommend looking at iTextSharp . 编辑:由于解决方案仅限于免费选项,我建议您查看iTextSharp If you are using NuGet you can get it via the NuGet package. 如果您正在使用NuGet,您可以通过NuGet包获得它。

As for using iTextSharp, good places to start would be: 至于使用iTextSharp,开始的好地方是:

iTextSharp Tutorial iTextSharp教程

How to convert HTML to PDF using iTextSharp 如何使用iTextSharp将HTML转换为PDF

How to Add Page number in Footer in PDF by Itextsharp 如何在Itextsharp中以PDF格式在页脚中添加页码

The quick answer as suggested by Nick is NO, RazorPDF does not support the ability to add page numbers automatically to your document. Nick建议的快速答案是否定的,RazorPDF不支持自动将页码添加到文档中的功能。

RazorPDF uses an older version of iTextSharp which I believe was the latest free iTextSharp version. RazorPDF使用较旧版本的iTextSharp,我相信这是最新的免费iTextSharp版本。 The latest version (5.5.5) is available under the AGPL license. 最新版本(5.5.5)根据AGPL许可证提供。

Latest iTextSharp version in RazorPDF RazorPDF中的最新iTextSharp版本

package id="iTextSharp" version="4.1.2" targetFramework="net40" package id =“iTextSharp”version =“4.1.2”targetFramework =“net40”

If rendering HTML is not a dealbreaker you can take a look at PDFsharp & MigraDoc . 如果渲染HTML不是一个破坏者,你可以看看PDFsharp和MigraDoc MigraDoc provide page numbering functionality as shown here. MigraDoc提供页面编号功能,如下所示

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM