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
- Create a
MergeDocument
object with the source PDF. - Create a document
Template
object and add it to the Document. - Place a
PageNumberingLabel
on the document Template. - 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
- Create a
Document
object. - Create a document
Template
object and add it to the Document. - Place a
PageNumberingLabel
on the document Template. - Begin a
Section
for the Document instance. - Add nine pages, note the different numbering styles for the first section and then the appendix (pages 8 and 9).
- 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.
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.
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.
- 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