Header and Footer (4)
Adding footers to a PDF document is a useful way to provide additional information and context to the content within the document. Footers typically appear at the bottom of each page and can include elements such as page numbers, dates, copyright information, or any other relevant details. By incorporating footers, you can enhance the professionalism and organization of your PDF files, making them more informative and easier to navigate for readers. In this article, you will learn how to add a footer to an existing PDF document in C# and VB.NET using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLLs included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Background Knowledge
When an existing PDF document is manipulated by Spire.PDF for .NET, the origin of the coordinate system is located at the top left corner of the page, with the x-axis extending to the right and the y-axis extending downward. Adding a footer to a page means adding content, such as text, images, automatic fields and shapes, to a specified location in the bottom blank area of the page.

If the blank area is not large enough to accommodate the content you want to add, you can consider increasing the PDF page margins.
Add a Footer to an Existing PDF Document in C#, VB.NET
Spire.PDF for .NET offers the PdfCanvas.DrawString() method, PdfCanvas.DrawImage() method, PdfCanvas.DrawLine() method and its similar methods, allowing users to draw text, images and shapes on a PDF page at the specified location. To add dynamic data to the footer, such as page numbers, sections, dates, you need to use the automatic fields. Spire.PDF for .NET provides the PdfPageNumberField class, PdfPageCountField calss, PdfSectionNumberField class etc. to achieve the addition of dynamic information.
The following are the steps to add a footer consisting of an image and page number to a PDF document using Spire.PDF for .NET.
- Create a PdfDocument object.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Load an image using PdfImage.FromFile() method.
- Draw the image on the bottom blank area of a page using PdfPageBase.Canvas.DrawImage() method.
- Create a PdfPageNumberField object, a PdfPageCountField object, and combine them in a PdfCompositefield object to return the string "Page X of Y".
- Draw page number on the bottom blank area of a page using PdfCompositeField.Draw() method.
- Save the document to another PDF file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddHeaderToExistingPdf
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Load a PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//Load an image
PdfImage footerImage = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\bg.jpg");
//Create a true type font
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12f, FontStyle.Bold), true);
//Create a brush
PdfBrush brush = PdfBrushes.White;
//Create a page number field
PdfPageNumberField pageNumberField = new PdfPageNumberField();
//Create a page count field
PdfPageCountField pageCountField = new PdfPageCountField();
//Create a composite field to combine page count field and page number field in a single string
PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField);
//Get the text size
SizeF fontSize = font.MeasureString(compositeField.Text);
//Get the page size
SizeF pageSize = doc.Pages[0].Size;
//Set the position of the composite field
compositeField.Location = new Point((int)(pageSize.Width - fontSize.Width) / 2, (int)pageSize.Height - 45);
//Loop through the pages in the document
for (int i = 0; i < doc.Pages.Count; i++)
{
//Get a specific page
PdfPageBase page = doc.Pages[i];
//Draw the image on the bottom blank area
page.Canvas.DrawImage(footerImage, 55, pageSize.Height - 65, pageSize.Width - 110, 50);
//Draw the composite field on the bottom blank area
compositeField.Draw(page.Canvas);
}
//Save to file
doc.SaveToFile("AddFooter.pdf");
doc.Dispose();
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
A header can provide useful information about the document's content, such as its title, author, date, and page numbers. This helps readers understand the document's purpose and navigate it more easily. In addition, including a logo or company name in the header reinforces brand identity and helps readers associate the document with a particular organization or business. In this article, you will learn how to add a header to an existing PDF document in C# and VB.NET using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Background Knowledge
When an existing PDF document is manipulated by Spire.PDF for .NET, the origin of the coordinate system is located at the top left corner of the page, with the x-axis extending to the right and the y-axis extending downward. Adding a header to a page means adding content, such as text, images, automatic fields and shapes, to a specified location in the upper blank area of the page.

If the blank area is not large enough to accommodate the content you want to add, you can consider increasing the PDF page margins.
Add a Header to an Existing PDF Document in C# and VB.NET
Spire.PDF for .NET allows users to draw text, images and shapes on a PDF page using PdfCanvas.DrawString() method, PdfCanvas.DrawImage() method, PdfCanvas.DrawLine() and other similar methods. To add dynamic information to the header, such as page numbers, sections, dates, you need to resort to automatic fields. Spire.PDF for .NET provides the PdfPageNumberField class, PdfSectionNumberField class, PdfCreationDateField class, etc. to achieve the dynamic addition of these data.
The following are the steps to add a header consisting of text, an image, a date, and a line to a PDF document using Spire.PDF for .NET.
- Create a PdfDocument object.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Create font, pen and brush objects that will be used to draw text or shapes.
- Draw text on the top blank area of a page using PdfPageBase.Canvas.DrawString() method.
- Draw a line on the top blank area of a page using PdfPageBase.Canvas.DrawLine() method.
- Load an image using PdfImage.FromFile() method.
- Draw the image on the top blank area of a page using PdfPageBase.Canvas.DrawImage() method.
- Create a PdfCreationDateField object that reflects the creation time of the document.
- Draw the creation time on the top blank area of a page using PdfCreationDateField.Draw() method.
- Save the document to another PDF file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddHeaderToExistingPdf
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Load a PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\TargetMarket.pdf");
//Load an image for the header
PdfImage headerImage = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\logo.png");
//Get image width in pixel
float width = headerImage.Width;
//Convert pixel to point
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
float pointWidth = unitCvtr.ConvertUnits(width, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);
//Specify text for the header
string headerText = "E-iceblue Tech\nwww.e-iceblue.com";
//Create a true type font
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12f, FontStyle.Bold), true);
//Create a brush
PdfBrush brush = PdfBrushes.Purple;
//Create a pen
PdfPen pen = new PdfPen(brush, 1.0f);
//Create a creation date field
PdfCreationDateField creationDateField = new PdfCreationDateField(font, brush);
creationDateField.DateFormatString = "yyyy-MM-dd";
//Create a composite field to combine static string and date field
PdfCompositeField compositeField = new PdfCompositeField(font, brush, "creation time: {0}", creationDateField);
compositeField.Location = new Point(55, 48);
//Loop through the pages in the document
for (int i = 0; i < doc.Pages.Count; i++)
{
//Get specific page
PdfPageBase page = doc.Pages[i];
//Draw the image on the top blank area
page.Canvas.DrawImage(headerImage, page.ActualSize.Width - pointWidth - 55, 20);
//Draw text on the top blank area
page.Canvas.DrawString(headerText, font, brush, 55, 20);
//Draw a line on the top blank area
page.Canvas.DrawLine(pen, new PointF(55, 70), new PointF(page.ActualSize.Width - 55, 70));
//Draw the composite field on the top blank area
compositeField.Draw(page.Canvas);
}
//Save to file
doc.SaveToFile("AddHeader.pdf");
doc.Dispose();
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
A PDF header or footer presents 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. In this article, you will learn to add text and automatic page numbering to footer space when creating a PDF document from scratch.
Spire.PDF has a class named PdfPageTemplateElement, which represents a page template element that can be used as header, footer, watermark or stamp. The template can contain text, image as well as dynamic fields like PdfPageCountField, PdfPageNumberField, etc.
Step 1: Define a custom function CreateFooterTemplate() to create a page template element that servers as footer, and return a PdfPageDocumentElement object.
static PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc, PdfMargins margins)
{
//get page size
SizeF pageSize = doc.PageSettings.Size;
//create a PdfPageTemplateElement object which works as footer space
PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Bottom);
footerSpace.Foreground = false;
//declare two float variables
float x = margins.Left;
float y = 0;
//draw line in footer space
PdfPen pen = new PdfPen(PdfBrushes.Gray, 1);
footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y);
//draw text in footer space
y = y + 5;
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Impact", 10f), true);
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
String footerText = "E-iceblue Technology Co., Ltd.\nTel:028-81705109\nWebsite:http://www.e-iceblue.com";
footerSpace.Graphics.DrawString(footerText, font, PdfBrushes.Gray, x, y, format);
//draw dynamic field in footer space
PdfPageNumberField number = new PdfPageNumberField();
PdfPageCountField count = new PdfPageCountField();
PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Gray, "Page {0} of {1}", number, count);
compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top);
SizeF size = font.MeasureString(compositeField.Text);
compositeField.Bounds = new RectangleF(pageSize.Width - x , y, size.Width, size.Height);
compositeField.Draw(footerSpace.Graphics);
//return footerSpace
return footerSpace;
}
Step 2: Create a PDF document, call the method CreateFooterTemplate() to create a footer template and apply it to the document.
static void Main(string[] args)
{
//create a PDF document
PdfDocument doc = new PdfDocument();
doc.PageSettings.Size = PdfPageSize.A4;
//reset the default margins to 0
doc.PageSettings.Margins = new PdfMargins(0);
//create a PdfMargins object, the parameters indicate the page margins you want to set
PdfMargins margins = new PdfMargins(60, 60, 60, 60);
//create a footer template with content and apply it to bottom page template
doc.Template.Bottom = CreateFooterTemplate(doc, margins);
//apply blank templates to other parts of page template
doc.Template.Top = new PdfPageTemplateElement(doc.PageSettings.Size.Width, margins.Top);
doc.Template.Left = new PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height);
doc.Template.Right = new PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height);
//add two pages in the document
doc.Pages.Add();
doc.Pages.Add();
//save the file
doc.SaveToFile("PdfFooter.pdf");
}
Output:

Full Code:
using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;
namespace AddPDFFooter
{
class Program
{
static void Main(string[] args)
{
//create a PDF document
PdfDocument doc = new PdfDocument();
doc.PageSettings.Size = PdfPageSize.A4;
//reset the default margins to 0
doc.PageSettings.Margins = new PdfMargins(0);
//create a PdfMargins object, the parameters indicate the page margins you want to set
PdfMargins margins = new PdfMargins(60, 60, 60, 60);
//create a footer template with content and apply it to page template
doc.Template.Bottom = CreateFooterTemplate(doc, margins);
//apply blank templates to other parts of page template
doc.Template.Top = new PdfPageTemplateElement(doc.PageSettings.Size.Width, margins.Top);
doc.Template.Left = new PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height);
doc.Template.Right = new PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height);
//add two pages in the document
doc.Pages.Add();
doc.Pages.Add();
//save the file
doc.SaveToFile("PdfFooter.pdf");
}
static PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc, PdfMargins margins)
{
//get page size
SizeF pageSize = doc.PageSettings.Size;
//create a PdfPageTemplateElement object which works as footer space
PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Bottom);
footerSpace.Foreground = false;
//declare two float variables
float x = margins.Left;
float y = 0;
//draw line in footer space
PdfPen pen = new PdfPen(PdfBrushes.Gray, 1);
footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y);
//draw text in footer space
y = y + 5;
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Impact", 10f), true);
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
String footerText = "E-iceblue Technology Co., Ltd.\nTel:028-81705109\nWebsite:http://www.e-iceblue.com";
footerSpace.Graphics.DrawString(footerText, font, PdfBrushes.Gray, x, y, format);
//draw dynamic field in footer space
PdfPageNumberField number = new PdfPageNumberField();
PdfPageCountField count = new PdfPageCountField();
PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Gray, "Page {0} of {1}", number, count);
compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top);
SizeF size = font.MeasureString(compositeField.Text);
compositeField.Bounds = new RectangleF(pageSize.Width - x, y, size.Width, size.Height);
compositeField.Draw(footerSpace.Graphics);
//return footerSpace
return footerSpace;
}
}
}
Imports Spire.Pdf
Imports Spire.Pdf.AutomaticFields
Imports Spire.Pdf.Graphics
Imports System.Drawing
Namespace AddPDFFooter
Class Program
Private Shared Sub Main(args As String())
'create a PDF document
Dim doc As New PdfDocument()
doc.PageSettings.Size = PdfPageSize.A4
'reset the default margins to 0
doc.PageSettings.Margins = New PdfMargins(0)
'create a PdfMargins object, the parameters indicate the page margins you want to set
Dim margins As New PdfMargins(60, 60, 60, 60)
'create a footer template with content and apply it to page template
doc.Template.Bottom = CreateFooterTemplate(doc, margins)
'apply blank templates to other parts of page template
doc.Template.Top = New PdfPageTemplateElement(doc.PageSettings.Size.Width, margins.Top)
doc.Template.Left = New PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height)
doc.Template.Right = New PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height)
'add two pages in the document
doc.Pages.Add()
doc.Pages.Add()
'save the file
doc.SaveToFile("PdfFooter.pdf")
End Sub
Private Shared Function CreateFooterTemplate(doc As PdfDocument, margins As PdfMargins) As PdfPageTemplateElement
'get page size
Dim pageSize As SizeF = doc.PageSettings.Size
'create a PdfPageTemplateElement object which works as footer space
Dim footerSpace As New PdfPageTemplateElement(pageSize.Width, margins.Bottom)
footerSpace.Foreground = False
'declare two float variables
Dim x As Single = margins.Left
Dim y As Single = 0
'draw line in footer space
Dim pen As New PdfPen(PdfBrushes.Gray, 1)
footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y)
'draw text in footer space
y = y + 5
Dim font As New PdfTrueTypeFont(New Font("Impact", 10F), True)
Dim format As New PdfStringFormat(PdfTextAlignment.Left)
Dim footerText As [String] = "E-iceblue Technology Co., Ltd." & vbLf & "Tel:028-81705109" & vbLf & "Website:http://www.e-iceblue.com"
footerSpace.Graphics.DrawString(footerText, font, PdfBrushes.Gray, x, y, format)
'draw dynamic field in footer space
Dim number As New PdfPageNumberField()
Dim count As New PdfPageCountField()
Dim compositeField As New PdfCompositeField(font, PdfBrushes.Gray, "Page {0} of {1}", number, count)
compositeField.StringFormat = New PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top)
Dim size As SizeF = font.MeasureString(compositeField.Text)
compositeField.Bounds = New RectangleF(pageSize.Width - x, y, size.Width, size.Height)
compositeField.Draw(footerSpace.Graphics)
'return footerSpace
Return footerSpace
End Function
End Class
End Namespace