How to Add Page Numbers to a PDF Document in C#

It’s easy to manage PDF files by adding page numbers in the header or footer. When you have a PDF file with lots of pages & confused which page you left reading last time. You can easily add page numbers in the PDF file to get rid of the problem. Also, you can share it with others so it will be easier for them to read it too.

In this post, I am going to introduce how to insert page numbers to an existing PDF document programmatically using .NET PDF library in C#.  Before you can run the following code, you have to download the library and import dll file to your project. In this example, I will add page number with format X of Y. X is number of current page number. Y is number of total pages.

Brief introduction about the method

  • Create a custom function DrawPageNumber(PdfPageCollection section, PdfMargins margin, int startNumber, int pageCount) to draw “X of Y” in PDF.
  • Load the PDF document which you want to insert page number.
  • Set the margin where you want page number to be displayed.
  • Call DrawPageNumber() function to add page number to footer.

Using the code

using Spire.Pdf;

using Spire.Pdf.Widget;

using Spire.Pdf.Graphics;

using System.Drawing;

 

namespace AddPageNumber2Pdf

{

    class Program

    {

        static void Main(string[] args)

        {

            PdfDocument doc = new PdfDocument();

            doc.LoadFromFile(“Test.pdf”);

            //set the margin

            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();

            PdfMargins margin = new PdfMargins();

            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);

            margin.Bottom = margin.Top;

            margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);

            margin.Right = margin.Left;

            //draw page number

      DrawPageNumber(doc.Pages, margin, 1, doc.Pages.Count);

            //save the file

            doc.SaveToFile(“AddNumber.pdf”,FileFormat.PDF);

            System.Diagnostics.Process.Start(“AddNumber.pdf”);

        }

        private static void DrawPageNumber(PdfPageCollection section, PdfMargins margin, int startNumber, int pageCount)

        {

            foreach (PdfPageBase page in section)

            {
page.Canvas.SetTransparency(0.5f);

                PdfBrush brush = PdfBrushes.Black;

                PdfPen pen = new PdfPen(brush, 0.75f);

                PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(“Arial”, 10f, FontStyle.Bold), true);

                PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);

                format.MeasureTrailingSpaces = true;

                float space = font.Height * 0.75f;

                float x = margin.Left;

                float width = page.Canvas.ClientSize.Width – margin.Left – margin.Right;

                float y = page.Canvas.ClientSize.Height – margin.Bottom + space;

                y = y + 1;

                String numberLabel = String.Format(“{0} of {1}”, startNumber++, pageCount);
page.Canvas.DrawString(numberLabel, font, brush, x + width, y, format);

                page.Canvas.SetTransparency(1);

            }

        }

    }

}

Output

AddPageNumber

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s