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 section introduces a solution to add header in PDF with C#, VB.NET.

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 SetDocumentTemplate() to create a PDF template that only contains header text and header image. By invoking this method, the template will be applied to all pages in your PDF document.

In the code below, we firstly initialize a page template object that can be used as header/footer, set the object as a top page template. Then call DrawString(string s, PdfFontBase font, PdfBrush brush, float x, float y, PdfStringFormat format) method and DrawImage(PdfImage image, float x, float y, float width, float height) method to insert formatted text and image with a custom size into the template.

static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
{
    //set top page template
    PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
    topSpace.Foreground = true;
    doc.Template.Top = topSpace;
    //draw text in header 
    PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Trebuchet MS", 14f, FontStyle.Italic));
    PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
    String label = "This is a PDF text header";
    SizeF size = font1.MeasureString(label, format);
    float y = 0;
    float x = PdfPageSize.A4.Width;         
    topSpace.Graphics.DrawString(label, font1, PdfBrushes.PaleVioletRed, x,y, format);
    //drew an image in header with custom size
    PdfImage headerImage = PdfImage.FromFile(@"logo.png");
    float width = headerImage.Width;
    float height = headerImage.Height;
    PointF pageLeftTop = new PointF(0 , 0);
    topSpace.Graphics.DrawImage(headerImage,0,0,width/2,height/2);           
}

Step 2: Create a PDF document and call the method SetDocumentTemplate() to apply the header template on it.

//create new PDF document
PdfDocument doc = new PdfDocument();
//set 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;        
//apply template in PDF document
SetDocumentTemplate(doc, PdfPageSize.A4, margin);
//add two pages in PDF
PdfPageBase page = doc.Pages.Add();
doc.Pages.Add();
doc.SaveToFile("header.pdf");
System.Diagnostics.Process.Start("header.pdf");

Full Code:

[C#]
static void Main(string[] args)
{
    //create new PDF document
    PdfDocument doc = new PdfDocument();
    //set 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;        
    //apply template in PDF document
    SetDocumentTemplate(doc, PdfPageSize.A4, margin);
    //add two pages in PDF
    PdfPageBase page = doc.Pages.Add();
    doc.Pages.Add();
    doc.SaveToFile("header.pdf");
    System.Diagnostics.Process.Start("header.pdf");
}
static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
{            
    //set a top page template
    PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
    topSpace.Foreground = true;
    doc.Template.Top = topSpace;
    //draw text header in template
    PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Trebuchet MS", 14f, FontStyle.Italic));
    PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
    String label = "This is a PDF text header";
    SizeF size = font1.MeasureString(label, format);
    float y = 0;
    float x = PdfPageSize.A4.Width;         
    topSpace.Graphics.DrawString(label, font1, PdfBrushes.PaleVioletRed, x,y, format);
    //drew an image header with custom size in template
    PdfImage headerImage = PdfImage.FromFile(@"logo.png");
    float width = headerImage.Width;
    float height = headerImage.Height;
    PointF pageLeftTop = new PointF(0 , 0);
    topSpace.Graphics.DrawImage(headerImage,0,0,width/2,height/2);           
}
[VB.NET]
Private Shared Sub Main(args As String())
	'create new PDF document
	Dim doc As New PdfDocument()
	'set 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
	'aplly template in PDF document
	SetDocumentTemplate(doc, PdfPageSize.A4, margin)
	'add two pages in PDF
	Dim page As PdfPageBase = doc.Pages.Add()
	doc.Pages.Add()
	doc.SaveToFile("header.pdf")
	System.Diagnostics.Process.Start("header.pdf")
End Sub
Private Shared Sub SetDocumentTemplate(doc As PdfDocument, pageSize As SizeF, margin As PdfMargins)
	'set a top page template
	Dim topSpace As New PdfPageTemplateElement(pageSize.Width, margin.Top)
	topSpace.Foreground = True
	doc.Template.Top = topSpace
	'draw text header in template
	Dim font1 As New PdfTrueTypeFont(New Font("Trebuchet MS", 14F, FontStyle.Italic))
	Dim format As New PdfStringFormat(PdfTextAlignment.Right)
	Dim label As [String] = "This is a PDF text header"
	Dim size As SizeF = font1.MeasureString(label, format)
	Dim y As Single = 0
	Dim x As Single = PdfPageSize.A4.Width
	topSpace.Graphics.DrawString(label, font1, PdfBrushes.PaleVioletRed, x, y, format)
	'drew an image header with custom size in template
	Dim headerImage As PdfImage = PdfImage.FromFile("logo.png")
	Dim width As Single = headerImage.Width
	Dim height As Single = headerImage.Height
	Dim pageLeftTop As New PointF(0, 0)
	topSpace.Graphics.DrawImage(headerImage, 0, 0, width / 2, height / 2)
End Sub