首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >razorPDF -自动页码

razorPDF -自动页码
EN

Stack Overflow用户
提问于 2014-09-29 14:18:00
回答 2查看 2.2K关注 0票数 4

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

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

RazorPDF提供了一种将页码放入报表的方法吗?

代码语言:javascript
复制
    public ActionResult PrintReport(DateTime printDate)
    {
        var printModel = printController.GetDetails(printDate);
        var pdfDoc = new PdfResult(printModel , "printlist");

        return pdfDoc;
    }

查看:

代码语言:javascript
复制
<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>       
EN

回答 2

Stack Overflow用户

发布于 2015-04-22 16:37:44

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

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

我遇到了类似的问题,尝试了各种包(RazorPDF、iTextSharp和HiQPdf)。如果您不局限于RazorPDF,我建议您查看HiQPdf。您基本上可以设置一个ActionResult,它定义页眉和页脚元素,将视图的html传递给它,它将为您的页眉和页脚提供任何您想要的PDF格式(如果您设置了它们)。对于页码来说,它就像:

代码语言:javascript
复制
 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“。

编辑:,因为解决方案仅限于免费选项,所以我建议您查看iTextSharp。如果您正在使用NuGet,您可以通过NuGet包获得它。

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

iTextSharp教程

如何使用iTextSharp将HTML转换为PDF

如何用Itextsharp在PDF格式的页脚中添加页码

票数 3
EN

Stack Overflow用户

发布于 2015-04-23 13:48:56

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

RazorPDF使用了iTextSharp的旧版本,我认为这是最新的免费iTextSharp版本。最新版本(5.5.5)可在AGPL许可下使用。

最新的iTextSharp RazorPDF版本 包装id="iTextSharp“version="4.1.2”targetFramework="net40“

如果呈现HTML不是一个解除程序,您可以查看PDFsharp & MigraDoc。MigraDoc提供页面编号功能,如这里。所示

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26102065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档