)
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: ASP.NET Web Forms |
Control: PDF |
Published Date: October 30, 2018 |
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 ASP.NET Web Forms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Dejan Lukic
Jun 19, 2019

this looked as if it work but it does not

            HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink);
            BlinkConverterSettings settings = new BlinkConverterSettings();
            string blinkPath = HttpContext.Current.Server.MapPath("~/bin/BlinkBinaries/");
            settings.BlinkPath = blinkPath;
            settings.Margin.All = 8;

            DataTable dtLogo = DataAccess.ProductData.DocumentLogo.GetDocumentLogosByDocumentTemplate(documentTemplateId);
            htmlConverter.ConverterSettings = settings;
            var p = HttpContext.Current.Server.MapPath("~/Content/TempDocs"); 
            PdfDocument document = htmlConverter.Convert(htmlText, p);
            PdfSection section = document.Sections.Add();
            section.Template.Top = SynchfusionDocAddHeader(document, dtLogo);
            section.Template.Bottom = SynchfusionDocAddFooter(document);

            string tmp = DateTime.Now.Millisecond.ToString();
            document.Save(HttpContext.Current.Server.MapPath("~/Content/TempDocs/") + tmp + "test.pdf");

it does create document but no images in header or footer

Reply
Dejan Lukic
Jun 19, 2019

it looks like we can't modify existing document page. just added ones. is it possible to modify existing page by adding sections to it?

Gowthamraj Kumar [Syncfusion]
Jun 24, 2019

Hi Dejan,

It is not possible to modifying the existing page by using sections and the images in the header is preserved properly in our end. We have created sample to add header and footer from seconds page with Blink HTML converter using PdfTemplate. We can directly add PDF header and footer when converting HTML to PDF using PdfHeader and PdfFooter properties in the converter settings. Refer below code snippet and online sample for more information.

settings.PdfHeader = AddHeader(converterSettings.PdfPageSize.Width, "Syncfusion Essential PDF", " "); settings.PdfFooter = AddFooter(converterSettings.PdfPageSize.Width, "@Copyright 2018");

Online Sample: https://mvc.syncfusion.com/demos/web/pdf/htmltopdf

The below sample is adding header height and footer height to the top and bottom margins to add the space for header and footer. After the conversion, we will draw the PdfHeader and PdfFooter as template in the top and bottom spaces in the page. For the first page, the header and footer space will be empty, we can draw the different PDF template for first page. Please find the sample from below link.
Sample: https://www.syncfusion.com/downloads/support/directtrac/239567/ze/HTMLConverter_HeaderFooter60701796

If the above sample is not suites your requirement, kindly let us know your exact requirement, so that we can analyze and create a sample to achieve your requirement.

Regards, Gowthamraj K

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