Examples

Add Page Numbers to a PDF

Page numbers can be added to an existing PDF document or to a PDF document created from scratch. The steps and sample code for adding page numbers to a PDF for both situations are included below.

How to Add Page Numbers to an Existing PDF

The following steps and sample code illustrate how to add page numbering labels to an existing PDF document using the PageNumberingLabel page element to automatically add page numbers to a PDF.

Steps for Adding Page Numbers to an Existing PDF Document

  1. Create a MergeDocument object with the source PDF.
  2. Create a document Template object and add it to the Document.
  3. Place a PageNumberingLabel on the document Template.
  4. Invoke the Draw method of the Document to output the PDF.

Sample Code - C#

MergeDocument document = new MergeDocument("doc-a.pdf");

Template template = new Template();
PageNumberingLabel pageLabels = new PageNumberingLabel("%%CP%% of %%TP%%", 0, 0, 200, 20);
template.Elements.Add(pageLabels);
document.Template = template;

document.Draw("output.pdf");

How to Create a PDF with Page Numbers

The following steps and sample code illustrate how to add page numbers to a PDF document created from scratch. As with the previous example, you use the PageNumberingLabel page element.

Steps for Adding Page Numbers to a PDF document

  1. Create a Document object.
  2. Create a document Template object and add it to the Document.
  3. Place a PageNumberingLabel on the document Template.
  4. Begin a Section for the Document instance.
  5. Add nine pages, note the different numbering styles for the first section and then the appendix (pages 8 and 9).
  6. Invoke the Draw method of the Document to output the PDF.

Sample Code - C#

Document document = new Document();    
Template documentTemplate = new Template();
document.Template = documentTemplate;
    
documentTemplate.Elements.Add(new PageNumberingLabel("%%PR%%%%SP%% of %%ST%%", 0, 680, 512, 12, Font.Helvetica, 12, TextAlign.Center));
    
document.Sections.Begin(NumberingStyle.RomanLowerCase);
document.Pages.Add(new Page()); //Page 1
document.Pages.Add(new Page()); //Page 2
document.Pages.Add(new Page()); //Page 3
    
document.Sections.Begin( NumberingStyle.Numeric );
document.Pages.Add(new Page()); //Page 4
document.Pages.Add(new Page()); //page 5
document.Pages.Add(new Page()); //page 6
document.Pages.Add(new Page()); //page 7
    
document.Sections.Begin( NumberingStyle.RomanLowerCase, "Appendix A - " );
document.Pages.Add(new Page()); //page 8
document.Pages.Add(new Page()); //page 9
    
document.Draw("output.pdf");

GitHub Project

Clone or view the example project at GitHub. The example code on this page is contained in the Examples/PageNumbers.cs file in the project.

Clone or View Example Project on GitHub

Getting Started

NuGet Package

DynamicPDF Core Suite is available on NuGet and is part of the ceTe.DynamicPDF.CoreSuite.NET package. The easiest way too install the DynamicPDF Core Suite is through the Visual Studio Package Manager, but you can also download the package directly from NuGet.

NuGet Package ID: ceTe.DynamicPDF.CoreSuite.NET

DynamicPDF Core Suite Information

More information can be found at the DynamicPDF Core Suite webpage.

Available on Other Platforms

DynamicPDF Core Suite is also available for the Java and COM/AxtiveX platforms. Refer to the respective product pages for more details.

Why Choose DynamicPDF?

  • Transparent Pricing
  • Lots of Features
  • Easy to Use
  • Great Support
  • Efficient Performance
  • Product Maturity (Over 22 Years)
  • Free Evaluation
  • .NET Core Support (Most Products)
  • Flexible Licensing

We’re Not The Only Ones That Think We’re Great!