Articles in this section
Category / Section

How to add header and footer in PDF document using C# and VB.NET?

3 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 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.

  3. NuGet package reference
  4. 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.


Conclusion:

A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.

The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.

You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied