J-Man
J-Man

Reputation: 209

razorPDF - automatic page numbering

Using C# MVC I am creating a report by passing a HTML partial view through PdfResult. 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. The printlist view is then called using the list of results as a model (see below).

Does RazorPDF provide a way to put page numbers on the report?

    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>       

Upvotes: 4

Views: 2601

Answers (2)

lopezbertoni
lopezbertoni

Reputation: 3641

The quick answer as suggested by Nick is NO, RazorPDF does not support the ability to add page numbers automatically to your document.

RazorPDF uses an older version of iTextSharp which I believe was the latest free iTextSharp version. The latest version (5.5.5) is available under the AGPL license.

Latest iTextSharp version in RazorPDF

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

If rendering HTML is not a dealbreaker you can take a look at PDFsharp & MigraDoc. MigraDoc provide page numbering functionality as shown here.

Hope this helps.

Upvotes: 1

Bardicer
Bardicer

Reputation: 1469

RazorPDF does not have the ability to set the page numbers in the PDF file. 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).

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.

I had a similar problem and tried various packages (RazorPDF, iTextSharp, and HiQPdf). If you're not limited to RazorPDF, I'd recommend looking into 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). 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.

EDIT: Since the solution is limited to only free options, I would recommend looking at iTextSharp. If you are using NuGet you can get it via the NuGet package.

As for using iTextSharp, good places to start would be:

iTextSharp Tutorial

How to convert HTML to PDF using iTextSharp

How to Add Page number in Footer in PDF by Itextsharp

Upvotes: 3

Related Questions