How to add Page Numbering in PDF file


Often, we have documents in which it is necessary to make any edits. For example, add page numbering in PDF file!

This sample code will help you to add page numbering in an existing PDF file.

For example : We have the PDF file and we need to add Page Numbering

Numbering Format: "Page N of M"

We place our page numbers into the footer using a new paragraph.

 

            // Create a footer.
            HeaderFooter footer = new HeaderFooter(dc, HeaderFooterType.FooterDefault); 
 
            // Create a new paragraph to insert a page numbering. 
            // So that, our page numbering looks as: Page N of M. 
            Paragraph par = new Paragraph(dc); 
            par.ParagraphFormat.Alignment = HorizontalAlignment.Right; 
            CharacterFormat cf = new CharacterFormat() { FontName = "Consolas", Size = 14.0 }; 
            par.Content.Start.Insert("Page ", cf.Clone()); 
             
            

The next step is adding two Fields : FieldType.Page and FieldType.NumPages


            // Page numbering is a Field. 
            Field fPage = new Field(dc, FieldType.Page); 
            fPage.CharacterFormat = cf.Clone(); 
            par.Content.End.Insert(fPage.Content); 
            par.Content.End.Insert(" of ", cf.Clone()); 
            Field fPages = new Field(dc, FieldType.NumPages); 
            fPages.CharacterFormat = cf.Clone(); 
            par.Content.End.Insert(fPages.Content); 
            footer.Blocks.Add(par);
             

Here you may download the input PDF file and the output result.

Complete code

using System;
using System.Collections.Generic;
using System.Linq;
using SautinSoft.Document;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/

            AddPageNumbering();            
        }

        /// <summary>
        /// Add page numbering into an existing PDF document.
        /// </summary>
        /// <remarks>
        /// https://www.sautinsoft.com/products/document/help/net/developer-guide/add-page-numbering-in-pdf-net-csharp-vb.php
        /// </remarks>
        static void AddPageNumbering()
        {
            string inpFile = @"..\..\..\shrek.pdf";
            string outFile = @"With Pages.pdf";
            DocumentCore dc = DocumentCore.Load(inpFile);

            // We place our page numbers into the footer.
            // Therefore we've to create a footer.
            HeaderFooter footer = new HeaderFooter(dc, HeaderFooterType.FooterDefault);

            // Create a new paragraph to insert a page numbering.
            // So that, our page numbering looks as: Page N of M.
            Paragraph par = new Paragraph(dc);
            par.ParagraphFormat.Alignment = HorizontalAlignment.Right;
            CharacterFormat cf = new CharacterFormat() { FontName = "Consolas", Size = 14.0 };
            par.Content.Start.Insert("Page ", cf.Clone());

            // Page numbering is a Field.
            // Create two fields: FieldType.Page and FieldType.NumPages.
            Field fPage = new Field(dc, FieldType.Page);
            fPage.CharacterFormat = cf.Clone();
            par.Content.End.Insert(fPage.Content);
            par.Content.End.Insert(" of ", cf.Clone());
            Field fPages = new Field(dc, FieldType.NumPages);
            fPages.CharacterFormat = cf.Clone();
            par.Content.End.Insert(fPages.Content);
            footer.Blocks.Add(par);

            foreach (Section s in dc.Sections)
            {
                s.HeadersFooters.Add(footer.Clone(true));
            }
            dc.Save(outFile);

            // Open the results for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(inpFile) { UseShellExecute = true });
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports System.IO
Imports SautinSoft.Document

Module Sample
    Sub Main()
        AddPageNumbering()
    End Sub
    ''' Get your free 30-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' Add page numbering into an existing PDF document.
    ''' </summary>
    ''' <remarks>
    ''' https://www.sautinsoft.com/products/document/help/net/developer-guide/add-page-numbering-in-pdf-net-csharp-vb.php
    ''' </remarks>
    Sub AddPageNumbering()
        Dim inpFile As String = "..\..\..\shrek.pdf"
        Dim outFile As String = "With Pages.pdf"
        Dim dc As DocumentCore = DocumentCore.Load(inpFile)

        ' We place our page numbers into the footer.
        ' Therefore we've to create a footer.
        Dim footer As New HeaderFooter(dc, HeaderFooterType.FooterDefault)

        ' Create a new paragraph to insert a page numbering.
        ' So that, our page numbering looks as: Page N of M.
        Dim par As New Paragraph(dc)
        par.ParagraphFormat.Alignment = HorizontalAlignment.Right
        Dim cf As New CharacterFormat() With {
                .FontName = "Consolas",
                .Size = 14.0
            }
        par.Content.Start.Insert("Page ", cf.Clone())

        ' Page numbering is a Field.
        ' Create two fields: FieldType.Page and FieldType.NumPages.
        Dim fPage As New Field(dc, FieldType.Page)
        fPage.CharacterFormat = cf.Clone()
        par.Content.End.Insert(fPage.Content)
        par.Content.End.Insert(" of ", cf.Clone())
        Dim fPages As New Field(dc, FieldType.NumPages)
        fPages.CharacterFormat = cf.Clone()
        par.Content.End.Insert(fPages.Content)
        footer.Blocks.Add(par)

        For Each s As Section In dc.Sections
            s.HeadersFooters.Add(footer.Clone(True))
        Next s
        dc.Save(outFile)

        ' Open the results for demonstration purposes.
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(inpFile) With {.UseShellExecute = True})
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
    End Sub
End Module

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.