Articles in this section
Category / Section

How to add headers and footers to a PDF file

3 mins read

The headers and footers of PDF appear on the top and bottom of the pages in a consistent way with text, image, or shapes. This includes title, page count, page numbers, author name, date, and time using dynamic fields such as PdfPageCountField and PdfPageNumberField.

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add headers and footers to a PDF file in C# and VB.NET.

Steps to add headers and footers to a PDF file programmatically:

  1. Create a new C# console application project. Create new console application
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. Install nuget packages
  3. Include the following namespace in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Drawing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports System.Drawing

 

  1. Use the following code snippet to add headers and footers in a PDF file.

C#

//Create a new PDF document
PdfDocument pdfDocument = new PdfDocument();
//Add a page to the PDF document
PdfPage pdfPage = pdfDocument.Pages.Add();
//Create a header and draw an image
RectangleF bounds = new RectangleF(0, 0, pdfDocument.Pages[0].GetClientSize().Width, 50);
PdfPageTemplateElement header = new PdfPageTemplateElement(bounds);
PdfImage image = new PdfBitmap(@"Logo.png");
//Draw an image in the header
header.Graphics.DrawImage(image, new PointF(0, 0), new SizeF(100, 50));
//Add the header at the top
pdfDocument.Template.Top = header;
//Create a page template that is used as footer
PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds);
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 7);
PdfBrush brush = new PdfSolidBrush(Color.Black);
//Create page number field
PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);
//Create page count field
PdfPageCountField count = new PdfPageCountField(font, brush);
//Add the fields in composite fields
PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Rattlesnake Canyon Grocery");
PdfCompositeField compositeField1 = new PdfCompositeField(font, brush, "Milton Dr.");
PdfCompositeField compositeField2 = new PdfCompositeField(font, brush, "Albuquerque 87110 USA");
PdfCompositeField compositeField3 = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count);
compositeField.Bounds = footer.Bounds;
//Draw the composite field in footer
compositeField.Draw(footer.Graphics, new PointF(200, 0));
compositeField1.Draw(footer.Graphics, new PointF(230, 10));
compositeField2.Draw(footer.Graphics, new PointF(205, 20));
compositeField3.Draw(footer.Graphics, new PointF(228, 40));
//Add the footer template at the bottom
pdfDocument.Template.Bottom = footer;
//Save the document
pdfDocument.Save("HeaderAndFooter.pdf");
//Close the document
pdfDocument.Close(true);
//This will open the PDF file so, the result will be seen in default PDF viewer
Process.Start("HeaderAndFooter.pdf");

 

VB.NET

'Create a new PDF document
Dim pdfDocument As PdfDocument = New PdfDocument()
'Add a page to the PDF document
Dim pdfPage As PdfPage = pdfDocument.Pages.Add()
'Create a header and draw an image
Dim bounds As RectangleF = New RectangleF(0, 0, pdfDocument.Pages(0).GetClientSize().Width, 50)
Dim header As PdfPageTemplateElement = New PdfPageTemplateElement(bounds)
Dim image As PdfImage = New PdfBitmap("Logo.png")
'Draw an image in the header
header.Graphics.DrawImage(image, New PointF(0, 0), New SizeF(100, 50))
'Add the header at the top
pdfDocument.Template.Top = header
'Create a page template that is used as footer
Dim footer As PdfPageTemplateElement = New PdfPageTemplateElement(bounds)
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 7)
Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)
'Create page number field
Dim pageNumber As PdfPageNumberField = New PdfPageNumberField(font, brush)
'Create page count field
Dim count As PdfPageCountField = New PdfPageCountField(font, brush)
'Add the fields in composite fields
Dim compositeField As PdfCompositeField = New PdfCompositeField(font, brush, "Rattlesnake Canyon Grocery")
Dim compositeField1 As PdfCompositeField = New PdfCompositeField(font, brush, "Milton Dr.")
Dim compositeField2 As PdfCompositeField = New PdfCompositeField(font, brush, "Albuquerque 87110 USA")
Dim compositeField3 As PdfCompositeField = New PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count)
compositeField.Bounds = footer.Bounds
'Draw the composite field in footer
compositeField.Draw(footer.Graphics, New PointF(200, 0))
compositeField1.Draw(footer.Graphics, New PointF(230, 10))
compositeField2.Draw(footer.Graphics, New PointF(205, 20))
compositeField3.Draw(footer.Graphics, New PointF(228, 40))
'Add the footer template at the bottom
pdfDocument.Template.Bottom = footer
'Save the document
pdfDocument.Save("HeaderAndFooter.pdf")
'Close the document
pdfDocument.Close(True)
'This will open the PDF file so, the result will be seen in default PDF viewer
Process.Start("HeaderAndFooter.pdf")

 

Download the work sample from HeaderAndFooterSample.Zip

By executing the program, you will get the PDF document as follows. Screenshot of output PDF document

Take a moment to peruse the documentation, where you can find drawing the headers and footers in a PDF file and other features like adding shapes to PDF, inserting image in a PDF, and drawing text to PDF with code examples.

Refer here to explore the rich set of Syncfusion Essential PDF features.

An online sample link to add headers and footers with images, text and shapes in all pages of document.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied