使用MvcRazorToPdf显示页码

本文关键字:显示 MvcRazorToPdf 使用 | 更新日期: 2023-09-27 18:21:11

我正试图在我的一个项目中使用这个包,并希望显示页码。Document对象上有一个属性具有PageNumber,但是。我不知道如何从视图访问它

当前代码:

PrintManager prntManager = new PrintManager();
var data = prntManager.ObjectToPdf(item, para);
byte[] pdf = null;
pdf = ControllerContext.GeneratePdf(prntManager.CreateController<PrintingController>(), data, prntManager.ReportToView(para.Export.Report.Code));   
HttpContext.Current.Response.AppendHeader("Export-Location", prntManager.GeneratePdf(pdf));

使用MvcRazorToPdf显示页码

如果有人遇到同样的问题,这里是我的解决方案:

PrintManager prntManager = new PrintManager();
var data = prntManager.ObjectToPdf(item, para);
byte[] pdf = null;
var cfg = new Action<iTextSharp.text.pdf.PdfWriter,iTextSharp.text.Document>((writer, document) =>
{
    writer.PageEvent = new StandardPrintHelper();
    document.NewPage();
});
pdf = ControllerContext.GeneratePdf(prntManager.CreateController<PrintingController>(), data, prntManager.ReportToView(para.Export.Report.Code), cfg);   
HttpContext.Current.Response.AppendHeader("Export-Location",
    prntManager.GeneratePdf(pdf));

StandardPrintHelper:

public class StandardPrintHelper : iTextSharp.text.pdf.PdfPageEventHelper
{
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_CENTER, new Phrase(writer.PageNumber.ToString()), 500, 140, 0);
    }
}