Add Header in PDF with C#/VB.NET

PDF header presents consistent information of a PDF document. It can be automatic page number, a date, a section title, an icon or an image. Whatever it is, it is an important part for a PDF file. This article will introduce how to add a header when programmatically creating a PDF document from scratch.

You can add both text and image in your PDF header by using Spire.PDF for .NET. In order to display the header content appropriately, you can also set text format and image size. The picture below shows the output after adding header in PDF:

Add Header in PDF with C#, VB.NET

Main Steps:

Step 1: Define a custom function CreateHeaderTemplate() to create a page template element that servers as header, and return a PdfPageDocumentElement object.

In the code below, we first initialize a page template object that can be used as header, then call DrawString() method, DrawImage() method and DrawLine() method to insert text,image and line into the header space.

static PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc, PdfMargins margins)
{
    //get page size
    SizeF pageSize = doc.PageSettings.Size;

    //create a PdfPageTemplateElement object as header space
    PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top);
    headerSpace.Foreground = false;

    //declare two float variables
    float x = margins.Left;
    float y = 0;

    //draw image in header space 
    PdfImage headerImage = PdfImage.FromFile("logo.png");
    float width = headerImage.Width / 3;
    float height = headerImage.Height / 3;
    headerSpace.Graphics.DrawImage(headerImage, x, margins.Top - height - 2, width, height);

    //draw line in header space
    PdfPen pen = new PdfPen(PdfBrushes.Gray, 1);
    headerSpace.Graphics.DrawLine(pen, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2);

    //draw text in header space
    PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Impact", 25f, FontStyle.Bold));
    PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
    String headerText = "HEADER TEXT";
    SizeF size = font.MeasureString(headerText, format);
    headerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Gray, pageSize.Width - x - size.Width-2, margins.Top- (size.Height+5), format);

    //return headerSpace
    return headerSpace;
}

Step 2: Create a PDF document, call the method CreateHeaderTemplate() to create a header 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 header template with content and apply it to page template
    doc.Template.Top = CreateHeaderTemplate(doc, margins);
    
    //apply blank templates to other parts of page template
    doc.Template.Bottom = new PdfPageTemplateElement(doc.PageSettings.Size.Width, margins.Bottom);
    doc.Template.Left = new PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height);
    doc.Template.Right = new PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height);

    //save the file
    doc.SaveToFile("PdfHeader.pdf");
}

Full Code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;


namespace AddPDFHeader
{
    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 header template with content and apply it to page template
            doc.Template.Top = CreateHeaderTemplate(doc, margins);

            //apply blank templates to other parts of page template
            doc.Template.Bottom = new PdfPageTemplateElement(doc.PageSettings.Size.Width, margins.Bottom);
            doc.Template.Left = new PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height);
            doc.Template.Right = new PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height);

            //save the file
            doc.SaveToFile("PdfHeader.pdf");
        }
        static PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc, PdfMargins margins)
        {
            //get page size
            SizeF pageSize = doc.PageSettings.Size;

            //create a PdfPageTemplateElement object as header space
            PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top);
            headerSpace.Foreground = false;

            //declare two float variables
            float x = margins.Left;
            float y = 0;

            //draw image in header space 
            PdfImage headerImage = PdfImage.FromFile("logo.png");
            float width = headerImage.Width / 3;
            float height = headerImage.Height / 3;
            headerSpace.Graphics.DrawImage(headerImage, x, margins.Top - height - 2, width, height);

            //draw line in header space
            PdfPen pen = new PdfPen(PdfBrushes.Gray, 1);
            headerSpace.Graphics.DrawLine(pen, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2);

            //draw text in header space
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Impact", 25f, FontStyle.Bold));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
            String headerText = "HEADER TEXT";
            SizeF size = font.MeasureString(headerText, format);
            headerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Gray, pageSize.Width - x - size.Width - 2, margins.Top - (size.Height + 5), format);

            //return headerSpace
            return headerSpace;
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing


Namespace AddPDFHeader
	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 header template with content and apply it to page template
			doc.Template.Top = CreateHeaderTemplate(doc, margins)

			'apply blank templates to other parts of page template
			doc.Template.Bottom = New PdfPageTemplateElement(doc.PageSettings.Size.Width, margins.Bottom)
			doc.Template.Left = New PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height)
			doc.Template.Right = New PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height)

			'save the file
			doc.SaveToFile("PdfHeader.pdf")
		End Sub
		Private Shared Function CreateHeaderTemplate(doc As PdfDocument, margins As PdfMargins) As PdfPageTemplateElement
			'get page size
			Dim pageSize As SizeF = doc.PageSettings.Size

			'create a PdfPageTemplateElement object as header space
			Dim headerSpace As New PdfPageTemplateElement(pageSize.Width, margins.Top)
			headerSpace.Foreground = False

			'declare two float variables
			Dim x As Single = margins.Left
			Dim y As Single = 0

			'draw image in header space 
			Dim headerImage As PdfImage = PdfImage.FromFile("logo.png")
			Dim width As Single = headerImage.Width / 3
			Dim height As Single = headerImage.Height / 3
			headerSpace.Graphics.DrawImage(headerImage, x, margins.Top - height - 2, width, height)

			'draw line in header space
			Dim pen As New PdfPen(PdfBrushes.Gray, 1)
			headerSpace.Graphics.DrawLine(pen, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2)

			'draw text in header space
			Dim font As New PdfTrueTypeFont(New Font("Impact", 25F, FontStyle.Bold))
			Dim format As New PdfStringFormat(PdfTextAlignment.Left)
			Dim headerText As [String] = "HEADER TEXT"
			Dim size As SizeF = font.MeasureString(headerText, format)
			headerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Gray, pageSize.Width - x - size.Width - 2, margins.Top - (size.Height + 5), format)

			'return headerSpace
			Return headerSpace
		End Function
	End Class
End Namespace