)
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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add header and footer from the second page of the PDF document using C# and VB.NET

Platform: WinForms |
Control: PDF |
Published Date: February 4, 2015 |
Last Revised Date: April 25, 2019
Tags: header, pdf, footer

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

Steps to add header and footer from the second page of the PDF document programmatically:

  1. Create a new C# console application project. Create a console application project
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NuGet package reference
  3. Use the following namespaces in 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 header and footer from the second page of the PDF document.

C#

//Create a new PDF document
PdfDocument document = new PdfDocument();
//Add the first page to the PDF document that does not contain header and footer
document.Pages.Add();
//Add a section - Header and footer can be added to this section
PdfSection section = document.Sections.Add();
//Add pages to section 
section.Pages.Add();
section.Pages.Add();
//Add the header template at the top of the section
section.Template.Top = AddHeader(document);
//Add the footer template at the bottom of the section 
section.Template.Bottom = AddFooter(document)
//Draw the text
foreach (PdfPage page in document.Pages)
{
    page.Graphics.DrawString("Hello World!!!", new PdfStandardFont(PdfFontFamily.TimesRoman, 20), PdfBrushes.Black, new PointF(0, 50));
}
//Save the document
document.Save("HeaderFooter.pdf");
//Close the document
document.Close();
//This will open the PDF file so, the result will be seen in default PDF viewer
Process.Start("HeaderFooter.pdf");

 

VB.NET

'Create a new PDF document
Dim document As New PdfDocument()
'Add the first page of the PDF document that does not contain header and footer
document.Pages.Add()
'Add a section - Header and footer can be added to this section
Dim section As PdfSection = document.Sections.Add()
'Add pages to section 
section.Pages.Add()
section.Pages.Add()
'Add the header template at the top of the section
section.Template.Top = AddHeader(document)
'Add the footer template at the bottom of the section 
section.Template.Bottom = AddFooter(document)
'Draw the text
For Each page As PdfPage In document.Pages
    page.Graphics.DrawString("Hello World!!!", New PdfStandardFont(PdfFontFamily.TimesRoman, 20), PdfBrushes.Black, New PointF(0, 50))
Next
'Save the document
document.Save("HeaderFooter.pdf")
'Close the document
document.Close()
'This will open the PDF file so, the result will be seen in default PDF viewer
Process.Start("HeaderFooter.pdf")

 

  1. Add the following code in AddHeader and AddFooter method to add header and footer to the section.

C#

private static PdfPageTemplateElement AddHeader(PdfDocument doc)
{
    RectangleF bounds = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50);
    //Create a page template for header
    PdfPageTemplateElement header = new PdfPageTemplateElement(bounds);
    //Draw the rectangle in header
    header.Graphics.DrawRectangle(PdfPens.DarkBlue, bounds);
    //Draw the image in header
    PdfImage image = new PdfBitmap(@"Logo.png");
    SizeF imageSize = new SizeF(110f, 35f);
    PointF imageLocation = new PointF(doc.Pages[0].GetClientSize().Width - imageSize.Width - 20, 5);
    header.Graphics.DrawImage(image, imageLocation, imageSize);
    return header;
}
 
private static PdfPageTemplateElement AddFooter(PdfDocument doc)
{
    RectangleF bounds = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50);
    //Create a page template that can be 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, "Page {0} of {1}", pageNumber, count);
    compositeField.Bounds = footer.Bounds;
    //Draw the composite field in footer
    compositeField.Draw(footer.Graphics, new PointF(470, 40));
    return footer;
}

 

VB.NET

Private Function AddHeader(doc As PdfDocument) As PdfPageTemplateElement
    Dim bounds As New RectangleF(0, 0, doc.Pages(0).GetClientSize().Width, 50)
    ‘Create a page template for header
    Dim header As New PdfPageTemplateElement(bounds)
    'Draw the rectangle in header
    header.Graphics.DrawRectangle(PdfPens.DarkBlue, bounds)
    'Draw the image in header
    Dim image As PdfImage = New PdfBitmap("Logo.png")
    Dim imageSize As New SizeF(110.0F, 35.0F)
    Dim imageLocation As New PointF(doc.Pages(0).GetClientSize().Width - imageSize.Width - 20, 5)
    header.Graphics.DrawImage(image, imageLocation, imageSize)
    Return header
End Function
 
Private Function AddFooter(doc As PdfDocument) As PdfPageTemplateElement
    Dim bounds As New RectangleF(0, 0, doc.Pages(0).GetClientSize().Width, 50)
    'Create a page template that can be used as footer
    Dim footer As 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 New PdfPageNumberField(font, brush)
    'Create page count field
    Dim count As New PdfPageCountField(font, brush)
    'Add the fields in composite fields
    Dim compositeField As 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(470, 40))
    Return footer
End Function

 

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

Take a moment to peruse the documentation, where you can find adding header and footer in a PDF document with code example.

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

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
Live Chat Icon