We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add header on the first page of a PDF in C# and VB.NET

Platform: WinForms |
Control: PDF
Tags: header, pdf, text, c#, vb.net

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add header on the first page of a PDF document in C# and VB.NET.

Steps to add header on the first page of a PDF programmatically:

  1. Create a new C# console application project. Create a 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. NuGet reference

Install nuget packages
  3. Include the following namespace in the program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Grid;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Grid

 

  1. Use the following code snippet to add header on the first page of the PDF document as follows.

C#

//Create a PDF document
PdfDocument doc = new PdfDocument();
//Add a page
PdfPage page = doc.Pages.Add();
//Adding header
PdfTemplate header = AddHeader(doc, "Syncfusion Essential PDF", "Header and Footer Demo");
//Draw the header in the first page of PDF document
page.Graphics.DrawPdfTemplate(header, new PointF());
//Create a PdfGrid
PdfGrid grid = new PdfGrid();
//Add columns to the data table
DataTable table = new DataTable();
table.Columns.Add("Roll Number", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Class", typeof(string));
//Add rows to the data table
for (int i = 0; i < 200; i++)
{
    table.Rows.Add(i, "Maxim", "III");
}            
//Assign data source
grid.DataSource = table;
//Set properties to paginate the grid
PdfGridLayoutFormat format = new PdfGridLayoutFormat();
format.Break = PdfLayoutBreakType.FitPage;
format.Layout = PdfLayoutType.Paginate;
//Draw a grid to the PDF document
grid.Draw(page, new Point(0, (int)header.Height+10), format);
//Save the document
doc.Save("Header.pdf");
//Close the document
doc.Close(true);
//This will open the PDF file so, the result will be seen in default PDF viewer 
System.Diagnostics.Process.Start("Header.pdf");

 

VB.NET

'Create a PDF document
Dim doc As New PdfDocument()
'Add a page
Dim page As PdfPage = doc.Pages.Add()
'Adding header
Dim header As PdfTemplate = AddHeader(doc, "Syncfusion Essential PDF", "Header and Footer Demo")
'Draw the header in the first page of PDF document
page.Graphics.DrawPdfTemplate(header, New PointF())
'Create a PdfGrid
Dim grid As New PdfGrid()
'Add columns to the data table
Dim table As New DataTable()
table.Columns.Add("Roll Number", GetType(Integer))
table.Columns.Add("Name", GetType(String))
table.Columns.Add("Class", GetType(String))
'Add rows to the data table
For i As Integer = 0 To 199
    table.Rows.Add(i, "Maxim", "III")
Next
'Assign data source
grid.DataSource = table
'Set properties to paginate the grid
Dim format As New PdfGridLayoutFormat()
format.Break = PdfLayoutBreakType.FitPage
format.Layout = PdfLayoutType.Paginate
'Draw a grid to the PDF document
grid.Draw(page, New Point(0, CInt(header.Height) + 10), format)
'Save the document
doc.Save("Header.pdf")
'Close the document
doc.Close(True)
'This will open the PDF file so, the result will be seen in default PDF viewer 
System.Diagnostics.Process.Start("Header.pdf")

 

  1. Add the following code in AddHeader method to create a header.

C#

private static PdfTemplate AddHeader(PdfDocument doc, string title, string description)
{
SizeF rect = new SizeF(doc.Pages[0].GetClientSize().Width, 50);
 
//Create page template
PdfTemplate header = new PdfTemplate(rect);
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 24);
float doubleHeight = font.Height * 2;
Color activeColor = Color.FromArgb(44, 71, 120);
SizeF imageSize = new SizeF(110f, 35f);
//Locating the logo on the right corner of the drawing surface
PointF imageLocation = new PointF(doc.Pages[0].GetClientSize().Width - imageSize.Width - 20, 5);
 
 
PdfSolidBrush brush = new PdfSolidBrush(activeColor);
 
PdfPen pen = new PdfPen(Color.DarkBlue, 3f);
font = new PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold);
 
//Set formatting for the text
PdfStringFormat format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Center;
format.LineAlignment = PdfVerticalAlignment.Middle;
 
//Draw title
header.Graphics.DrawString(title, font, brush, new RectangleF(0, 0, header.Width, header.Height), format);
brush = new PdfSolidBrush(Color.Gray);
font = new PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold);
 
format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Left;
format.LineAlignment = PdfVerticalAlignment.Bottom;
 
//Draw description
header.Graphics.DrawString(description, font, brush, new RectangleF(0, 0, header.Width, header.Height - 8), format);
 
//Draw some lines in the header
pen = new PdfPen(Color.DarkBlue, 0.7f);
header.Graphics.DrawLine(pen, 0, 0, header.Width, 0);
pen = new PdfPen(Color.DarkBlue, 2f);
header.Graphics.DrawLine(pen, 0, 03, header.Width + 3, 03);
pen = new PdfPen(Color.DarkBlue, 2f);
header.Graphics.DrawLine(pen, 0, header.Height - 3, header.Width, header.Height - 3);
header.Graphics.DrawLine(pen, 0, header.Height, header.Width, header.Height);
 
return header;
}

 

VB.NET

Private Function AddHeader(ByVal doc As PdfDocument, ByVal title As String, ByVal description As String) As PdfTemplate
 
    Dim rect As SizeF = New SizeF(doc.Pages(0).GetClientSize().Width, 50)
 
    'Create page template
    Dim header As PdfTemplate = New PdfTemplate(rect)
    Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 24)
    Dim doubleHeight As Single = font.Height * 2
    Dim activeColor As Color = Color.FromArgb(44, 71, 120)
    Dim imageSize As SizeF = New SizeF(110.0F, 35.0F)
    'Locating the logo on the right corner of the Drawing Surface
    Dim imageLocation As PointF = New PointF(doc.Pages(0).GetClientSize().Width - imageSize.Width - 20, 5)
 
    Dim brush As PdfSolidBrush = New PdfSolidBrush(activeColor)
 
    Dim pen As PdfPen = New PdfPen(Color.DarkBlue, 3.0F)
    font = New PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold)
 
    'Set formatting for the text
    Dim format As PdfStringFormat = New PdfStringFormat()
    format.Alignment = PdfTextAlignment.Center
    format.LineAlignment = PdfVerticalAlignment.Middle
 
    'Draw title
    header.Graphics.DrawString(title, font, brush, New RectangleF(0, 0, header.Width, header.Height), format)
    brush = New PdfSolidBrush(Color.Gray)
    font = New PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold)
 
    format = New PdfStringFormat()
    format.Alignment = PdfTextAlignment.Left
    format.LineAlignment = PdfVerticalAlignment.Bottom
 
    'Draw description
    header.Graphics.DrawString(description, font, brush, New RectangleF(0, 0, header.Width, header.Height - 8), format)
 
    'Draw some lines in the header
    pen = New PdfPen(Color.DarkBlue, 0.7F)
    header.Graphics.DrawLine(pen, 0, 0, header.Width, 0)
    pen = New PdfPen(Color.DarkBlue, 2.0F)
    header.Graphics.DrawLine(pen, 0, 3, header.Width + 3, 3)
    pen = New PdfPen(Color.DarkBlue, 2.0F)
    header.Graphics.DrawLine(pen, 0, header.Height - 3, header.Width, header.Height - 3)
    header.Graphics.DrawLine(pen, 0, header.Height, header.Width, header.Height)
 
    Return header
End Function

 

A complete working sample can be downloaded from PDFHeaderSample.zip.

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

Take a moment to peruse the documentation, where you can find adding 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 in all pages of the 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.

 

 

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile