Articles in this section
Category / Section

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

4 mins read

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.

 

 

 

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