Adding Page Numbers to a PDF
Add page numbers to a new or existing PDF using DynamicPDF Core Suite for .NET. The following C# examples show you how.
Watch the Video
How to Create PDF with Page Numbers
The following steps and C# sample code illustrate how to add page numbers to a PDF document created from scratch.
Steps for Adding Page Numbers to a PDF document
- Create a
Document
object. - Create a document
Template
object and add it to theDocument
instance. - Place a
PageNumberingLabel
on theTemplate
instance. - Begin a
Section
for theDocument
instance. - Add nine pages, note the different numbering styles for the first section and then the appendix (pages 8 and 9).
- Add the
Draw
method to output the PDF document.
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");
How to Add Page Numbers to Existing PDF
The following steps and C# 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
- Create a
MergeDocument
object with the source PDF. - Create a document
Template
object and add it to theDocument
instance. - Place a
PageNumberingLabel
on the documentTemplate
instance. - Add the
Draw
method to save the merged 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");
GitHub Project
An example project is available on GitHub (examples.dynamicpdf-core-suite-dotnet-core). Examples are provided in C# and VB.NET. Clone or view the example project at GitHub. This specific example discussed on this page are all contained in following classes on GitHub
- C# - PageNumbers.cs
- VB.NET - PageNumbers.vb
Getting Started
Get started easily by installing DynamicPDF Core Suite for .NET through NuGet or manually. Then, refer to the documentation for more information on using and purchasing the product.
NuGet Package
The easiest way to install DynamicPDF Core Suite is by obtaining the NuGet package using Visual Studio's Package Manager. You can also obtain the NuGet package by downloading it directly. Refer to the installation documentation for more information.
DynamicPDF Core Suite Information
DynamicPDF Core Suite for .NET combines creating, merging, and visual report creation into one powerful product for creating PDF documents. It is ideal for anyone who needs to generate PDF documents or reports or work with existing PDFs in their applications. With a free Evaluation Edition to try and with flexible and royalty-free licensing options, why not start using DynamicPDF Core Suite for .NET today!
More Information on Page Numbers
- Refer to the PageNumberLabel SDK documentation topic, or
- Chapter 4.8, Text, in the book DynamicPDF Core Suite for .NET by Example available on GitHub.
Available on Other Platforms
DynamicPDF Core Suite is also available for the Java and COM/ActiveX platforms. Refer to the respective product pages for more details.
- DynamicPDF Generator
- Java - DynamicPDF Generator for Java
- COM/ActiveX - DynamicPDF Generator for COM/ActiveX
- DynamicPDF Merger
- Java - DynamicPDF Merger for Java
- COM/ActiveX - DynamicPDF Merger for COM/ActiveX