Add PDF Footer in C#, VB.NET

A PDF header or footer present consistent information (For example: a date, page numbering, the title of the overall document, or author’s name) in the page margins throughout a PDF. This article presents how to add automatic page numbering and text to PDF footer space.

Spire.PDF has a class named PdfPageTemplateElement, which represents a page template that can be used as header, footer, watermark or stamp. The template can contain any types of elements including dynamic fields such as PdfPageCountField, PdfPageNumberField, etc. Once the template is created, we can apply the template to a newly-build or an existing PDF document to get the same information.

Step 1: Create a custom function that generates a footer template consisting of page number, page count and some text.

static void AddFooter(PdfDocument doc, PdfMargins margin)
{
    //Get the size of first page
    SizeF pageSize = doc.Pages[0].Size;

    //Create a PdfPageTemplateElement object that will be
    //used as footer space
    PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margin.Bottom);
    footerSpace.Foreground = true;
    doc.Template.Bottom = footerSpace;

    //Draw text at the center of footer space
    PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Bold), true);
    PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);
    String headerText = "Copyright © 2017 e-iceblue. All Rights Reserved.";
    float x = pageSize.Width / 2;
    float y = 15f;
    footerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Black, x, y, format);

    //Create page number automatic field
    PdfPageNumberField number = new PdfPageNumberField();
    //Create page count automatic field
    PdfPageCountField count = new PdfPageCountField();
    //Add the fields in composite field
    PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "Page {0} of {1}", number, count);
    //Align string of "Page {0} of {1}" to center 
    compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
    compositeField.Bounds = footerSpace.Bounds;
    //Draw composite field at footer space
    compositeField.Draw(footerSpace.Graphics);
}

Step 2: Invoke AddFooter() function in main method to add footer information to a newly built PDF document.

static void Main(string[] args)
{
    //Create a new PDF document
    PdfDocument doc = new PdfDocument();

    //Add two pages in it
    PdfPageBase page = doc.Pages.Add();
    doc.Pages.Add();

    //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(4.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
    margin.Right = margin.Left;

    //Call AddFooter method to add header information
    AddFooter(doc, margin);

    //Save the file
    doc.SaveToFile("PDF_Footer.pdf");
    doc.Close();
}

Output:

Add PDF Footer in C#, VB.NET

Full Code:

[C#]
static void Main(string[] args)
{
    //Create a new PDF document
    PdfDocument doc = new PdfDocument();

    //Add two pages in it
    PdfPageBase page = doc.Pages.Add();
    doc.Pages.Add();

    //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(4.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
    margin.Right = margin.Left;

    //Call AddFooter method to add header information
    AddFooter(doc, margin);

    //Save the file
    doc.SaveToFile("PDF_Footer.pdf");
    doc.Close();
}
static void AddFooter(PdfDocument doc, PdfMargins margin)
{
    //Get the size of first page
    SizeF pageSize = doc.Pages[0].Size;

    //Create a PdfPageTemplateElement object that will be
    //used as footer space
    PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margin.Bottom);
    footerSpace.Foreground = true;
    doc.Template.Bottom = footerSpace;

    //Draw text at the center of footer space
    PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Bold), true);
    PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);
    String headerText = "Copyright © 2017 e-iceblue. All Rights Reserved.";
    float x = pageSize.Width / 2;
    float y = 15f;
    footerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Black, x, y, format);

    //Create page number automatic field
    PdfPageNumberField number = new PdfPageNumberField();
    //Create page count automatic field
    PdfPageCountField count = new PdfPageCountField();
    //Add the fields in composite field
    PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "Page {0} of {1}", number, count);
    //Align string of "Page {0} of {1}" to center 
    compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
    compositeField.Bounds = footerSpace.Bounds;
    //Draw composite field at footer space
    compositeField.Draw(footerSpace.Graphics);
}
[VB.NET]
Private Shared Sub Main(args As String())
	'Create a new PDF document
	Dim doc As New PdfDocument()

	'Add two pages in it
	Dim page As PdfPageBase = doc.Pages.Add()

    doc.Pages.Add()

	'Set the margin
	Dim unitCvtr As New PdfUnitConvertor()

    Dim margin As New PdfMargins()

    margin.Top = unitCvtr.ConvertUnits(2.54F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
	margin.Bottom = margin.Top
    margin.Left = unitCvtr.ConvertUnits(4.17F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)

    margin.Right = margin.Left

	'Call AddFooter method to add header information

    AddFooter(doc, margin)

	'Save the file
	doc.SaveToFile("PDF_Footer.pdf")
	doc.Close()
End Sub
Private Shared Sub AddFooter(doc As PdfDocument, margin As PdfMargins)
	'Get the size of first page
	Dim pageSize As SizeF = doc.Pages(0).Size

	'Create a PdfPageTemplateElement object that will be
	'used as footer space
	Dim footerSpace As New PdfPageTemplateElement(pageSize.Width, margin.Bottom)

    footerSpace.Foreground = True
    doc.Template.Bottom = footerSpace

	'Draw text at the center of footer space

    Dim font As New PdfTrueTypeFont(New Font("Arial", 9F, FontStyle.Bold), True)
	Dim format As New PdfStringFormat(PdfTextAlignment.Center)

    Dim headerText As[String] = "Copyright © 2017 e-iceblue. All Rights Reserved."
	Dim x As Single = pageSize.Width / 2

    Dim y As Single = 15F

    footerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Black, x, y, format)

	'Create page number automatic field
	Dim number As New PdfPageNumberField()
	'Create page count automatic field
	Dim count As New PdfPageCountField()
	'Add the fields in composite field
	Dim compositeField As New PdfCompositeField(font, PdfBrushes.Black, "Page {0} of {1}", number, count)
	'Align string of "Page {0} of {1}" to center 
	compositeField.StringFormat = New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)

    compositeField.Bounds = footerSpace.Bounds
	'Draw composite field at footer space
	compositeField.Draw(footerSpace.Graphics)
End Sub