Click or drag to resize
Pdf Library for .NET
Page Numbering

Large pdf documents usually display page numbers in the footer to make it easier for users to identify content in the pdf document. Select.Pdf offers support for page numbering using special placeholders that can be included in text elements that repeat in all pdf pages using pdf templates.

There are 2 placeholders that can be included in any PdfTextElement that belongs to a template:

  • {page_number} - represents the current page of the pdf document.

  • {total_pages} - represents the total number of pages of the pdf document.

A few properties of the PdfTemplate class can be used to customize page numbering:

  • FirstPageNumber - Controls the page number for the first page being rendered. The page number is displayed setting a {page_number} placeholder in a Text property of a PdfTextElement object that can be added to the template. By default the page numbers start with 1. This can be changed using the FirstPageNumber property.

  • TotalPagesOffset - Controls the total number of pages offset in the pdf document. The total number of pages is displayed setting a {total_pages} placeholder in the Text property of a PdfTextElement object that can be added to the template. The total number of pages can be incremented with a value specified by the TotalPagesOffset property. This could be useful when the generated pdf will be merged with other documents.

Sample Code

This sample code shows how to create a new PDF document using Select.Pdf, how to add several pages and automatic page numbering in the document footer.

// create a new pdf document
PdfDocument doc = new PdfDocument();
doc.Margins = new PdfMargins(10, 10, 0, 0);

// create a new pdf font
PdfFont font = doc.AddFont(PdfStandardFont.Helvetica);
font.Size = 24;

// add a new page to the document
PdfPage page = doc.AddPage();

// footer template (100 points in height) with text element
doc.Footer = doc.AddTemplate(doc.Pages[0].ClientRectangle.Width, 100);
PdfTextElement text1 = new PdfTextElement(0, 50, 
    "Page: {page_number} of {total_pages}.", font);
text1.ForeColor = System.Drawing.Color.Blue;
doc.Footer.Add(text1);

// create a new text element and add it to the page
// if page elements are added after header and footer is set, 
// they will not be displayed in those areas.
PdfTextElement text = new PdfTextElement(0, 0, Helper.SomeLongText(), font);
page.Add(text);

// save pdf document
doc.Save(Response, false, "Sample.pdf");

// close pdf document
doc.Close();
See Also