PDFRasterizer.NET 4.0 is now available. Supports .NET Core. Renders pixel-perfectly.

How to add page numbers to your PDF

This article shows how to add page numbers to PDF using TallPDF built-in features.

We will build a simple structure based on TallPDF’s objects and using html content create a paged PDF document.

Code sample to add page numbers to PDF in C#:

      private static void Main(string[] args)
      {
         // prepare our document
         Document document = new Document();

         // document may have one or more section, create one here
         Section section = new Section();
         document.Sections.Add(section);

         // adding a fragment where page number will be generated
         Fragment pagenumbers = new Fragment("#p/#P", 12);
         pagenumbers.HasContextFields = true;

         TextParagraph textParagraph = new TextParagraph();

         textParagraph.Fragments.Add(pagenumbers);

         // footer will be repeated in each page, a good place to put our page number
         section.Footer = new Footer();
         section.Footer.Paragraphs.Add(textParagraph);

         using (FileStream inputFile = File.Open("input.html", FileMode.Open))
         {
            // add page content to the section here
            XhtmlParagraph htmlParagraph = new XhtmlParagraph(inputFile);
            section.Paragraphs.Add(htmlParagraph);
         }

         // save the result to PDF
         using (Stream stream = new FileStream("out.pdf", FileMode.Create))
         {
            document.Write(stream);
         }
      }
Download TallPDF.NET 4.0
We will send you a download link

  • This field is for validation purposes and should be left unchanged.
Why do we ask your email address?
We send tips that speed up your evaluation
We let you know about bug fixes
You can always unsubscribe with one click
We never share your address with a 3rd party
Thank you for your download

We have sent an email with a download link.