With this source code sample you may quickly learn how to add page numbers in C#. What is ByteScout PDF SDK? It is the SDK for pdf documents generation, modification and updates. Can also generate and fill PDF forms. Provides support for text (fonts, style, size, font family), layers, pdf form fields, vector and raster drawings. It can help you to add page numbers in your C# application.
This code snippet below for ByteScout PDF SDK works best when you need to quickly add page numbers in your C# application. Just copy and paste the code into your C# application’s code and follow the instruction. Enjoy writing a code with ready-to-use sample codes in C#.
Free trial version of ByteScout PDF SDK is available for download from our website. Get it to try other source code samples for C#.
On-demand (REST Web API) version:
Web API (on-demand version)
On-premise offline SDK for Windows:
60 Day Free Trial (on-premise)
using Bytescout.PDF;
using System;
using System.Diagnostics;
using Font = Bytescout.PDF.Font;
using SolidBrush = Bytescout.PDF.SolidBrush;
namespace AddHeaderAndFooter
{
class Program
{
#region Declarations
private enum PageNoStyle
{
TopLeft,
TopRight,
BottomLeft,
BottomRight
}
#endregion
static void Main(string[] args)
{
try
{
using (Document doc = new Document())
{
// Add registration keys
doc.RegistrationName = "demo";
doc.RegistrationKey = "demo";
// Load document
doc.Load("sample.pdf");
// Write template in each pages
for (int i = 0; i < doc.Pages.Count; i++)
{
Page currentPage = doc.Pages[i];
_AddPageNo(currentPage, "Page #" + (i + 1), PageNoStyle.BottomRight);
}
// Save output file
doc.Save("result.pdf");
}
// Open result document in default associated application (for demo purpose)
ProcessStartInfo processStartInfo = new ProcessStartInfo("result.pdf");
processStartInfo.UseShellExecute = true;
Process.Start(processStartInfo);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("Press enter key to exit...");
Console.ReadLine();
}
}
/// <summary>
/// Add Page No
/// </summary>
private static void _AddPageNo(Page currentPage, string pageNoText, PageNoStyle pageNoStyle)
{
float float_left = 10;
float float_top = 10;
switch (pageNoStyle)
{
case PageNoStyle.TopRight:
float_left = currentPage.Width - 60;
break;
case PageNoStyle.BottomLeft:
float_top = currentPage.Height - 20;
break;
case PageNoStyle.BottomRight:
float_top = currentPage.Height - 20;
float_left = currentPage.Width - 60;
break;
case PageNoStyle.TopLeft:
default:
break;
}
// Draw/Write page no
currentPage.Canvas.DrawString(pageNoText,
new Font(StandardFonts.CourierBold, 12),
new SolidBrush(),
float_left,
float_top);
}
}
}
60 Day Free Trial or Visit ByteScout PDF SDK Home Page
Explore ByteScout PDF SDK Documentation
Explore Samples
Sign Up for ByteScout PDF SDK Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
60 Day Free Trial or Visit ByteScout PDF SDK Home Page
Explore ByteScout PDF SDK Documentation
Explore Samples
Sign Up for ByteScout PDF SDK Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
also available as: