2

I am trying to use this package in a project of mine and would like to display the page number. There is a property on the Document object that has the PageNumber but. I have no clue how to access it from the view

Current code:

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));
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
Jester
  • 3,069
  • 5
  • 30
  • 44
  • Where would you display this page number? On a web page? Or do you want to [modify an existing PDF and add page numbers to it](http://stackoverflow.com/a/9845722/231316)? – Chris Haas Nov 09 '15 at 19:33
  • the code above is basicly generating a pdf from a razor view and I would like page number to be displayed on that pdf – Jester Nov 09 '15 at 19:34

1 Answers1

2

If someone has the same problem here is my solution:

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);
    }
}
DLeh
  • 23,806
  • 16
  • 84
  • 128
Jester
  • 3,069
  • 5
  • 30
  • 44
  • 1
    For those without PrintManager, `return new PdfActionResult(viewName, viewModel, writer, document) => { writer.PageEvent = new StandardPrintHelper(); document.NewPage(); });` Will do the trick – Jean-Bernard Pellerin Mar 07 '16 at 22:42