Add Page Numbers to PDF in C# (.NET Core)

Page numbers can be added to an existing PDF document or to a PDF document created from scratch by using the Merger and Generator products respectively. The steps and sample codes to add page numbers to PDF is given below for both the products.

How to Add Page Numbers to an Existing PDF

Below are steps and sample code on how to add page numbering labels to an existing PDF document. The PageNumberingLabel page element can be used 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(@"C:\Output.pdf");

How to Create PDF with Page Numbers

Below are steps and sample code on how to add page numbers to a PDF document you are creating from scratch. The PageNumberingLabel page element can be used to automatically add page numbers to a PDF.

Steps for adding page numberd 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 the Section of the Document with different numbering styles and add pages to the sections as needed.
  5. 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(@"C:\Output.pdf");

Getting Started

NuGet Package

DynamicPDF Generator and Merger are available on NuGet and are part of the ceTe.DynamicPDF.CoreSuite.NET package. The Core Suite package includes Generator, Merger and ReportWriter.

Nuget Logo
NuGet Package ID: ceTe.DynamicPDF.CoreSuite.NET

Example Project

An example project showing this functionality can be cloned or viewed from GitHub:

GitHub Logo
Clone or View Example Project on GitHub

DynamicPDF Generator and Merger Information

More information can be found here:

Available in Other Platforms

Generator and Merger PDF Libraries are available for 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 18 Years)
  • Free Evaluation
  • .NET Core Support (Most Products)
  • Flexible Licensing

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