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 : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add PDF Header and PDF Footer while converting HTML to PDF

Adding header/footer for each PDF page while converting HTML to PDF is possible by using WebKit HTML converter.

Assemblies Required

  1. Syncfusion.Compression.Base.dll
  2. Syncfusion.HtmlConverter.Base.dll
  3. Syncfusion.Pdf.Base.dll
  4. QtBinaries assemblies

All the above mentioned assemblies can be get by installing the WebKit installer available in the below link.

WebKit HTML Converter: https://www.syncfusion.com/downloads/latest-version

Code Snippet:

//Initialize HTML to PDF converter with WebKit Rendering Engine.

HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);

WebKitConverterSettings webKitSettings = new WebKitConverterSettings();

 

//Set WebKitPath

webKitSettings.WebKitPath = @"../../QtBinaries";

 

//Add PDF Header

webKitSettings.PdfHeader = AddPdfHeader(webKitSettings.PdfPageSize.Width, "Syncfusion Essential PDF", " ");

 

//Add PDF Footer

webKitSettings.PdfFooter = AddPdfFooter(webKitSettings.PdfPageSize.Width, "@Copyright 2015");

 

htmlConverter.ConverterSettings = webKitSettings;

 

//Convert URL to PDF

PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");

 

//Save and close the document.

document.Save("Sample.pdf");

document.Close();

 

private PdfPageTemplateElement AddPdfHeader(float width, string title, string description)

{

RectangleF rect = new RectangleF(0, 0, width, 50);

//Create a new instance of PdfPageTemplateElement class.    

PdfPageTemplateElement header = new PdfPageTemplateElement(rect);

dfGraphics g = header.Graphics;

 

//Draw title.

PdfFont font = new PdfTrueTypeFont(new Font("Helvetica", 16, FontStyle.Bold), true);

PdfSolidBrush brush = new PdfSolidBrush(Color.FromArgb(44, 71, 120));

float x = (width / 2) - (font.MeasureString(title).Width) / 2;

g.DrawString(title, font, brush, new RectangleF(x, (rect.Height / 4) + 3, font.MeasureString(title).Width, font.Height));

 

//Draw description

brush = new PdfSolidBrush(Color.Gray);

font = new PdfTrueTypeFont(new Font("Helvetica", 6, FontStyle.Bold), true);

PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Bottom);

g.DrawString(description, font, brush, new RectangleF(0, 0, header.Width, header.Height - 8), format);

 

//Draw some lines in the header

PdfPen pen = new PdfPen(Color.DarkBlue, 0.7f);

g.DrawLine(pen, 0, 0, header.Width, 0);

pen = new PdfPen(Color.DarkBlue, 2f);

g.DrawLine(pen, 0, 03, header.Width + 3, 03);

pen = new PdfPen(Color.DarkBlue, 2f);

g.DrawLine(pen, 0, header.Height - 3, header.Width, header.Height - 3);

g.DrawLine(pen, 0, header.Height, header.Width, header.Height);

 

return header;

}

 

private PdfPageTemplateElement AddPdfFooter(float width, string footerText)

{

RectangleF rect = new RectangleF(0, 0, width, 50);

//Create a new instance of PdfPageTemplateElement class.

PdfPageTemplateElement footer = new PdfPageTemplateElement(rect);

PdfGraphics g = footer.Graphics;

 

// Draw footer text.

PdfSolidBrush brush = new PdfSolidBrush(Color.Gray);

PdfFont font = new PdfTrueTypeFont(new Font("Helvetica", 6, FontStyle.Bold), true);

float x = (width / 2) - (font.MeasureString(footerText).Width) / 2;

g.DrawString(footerText, font, brush, new RectangleF(x, g.ClientSize.Height - 10, font.MeasureString(footerText).Width, font.Height));

 

//Create page number field

PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);

 

//Create page count field

PdfPageCountField count = new PdfPageCountField(font, brush);

 

PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count);

compositeField.Bounds = footer.Bounds;

compositeField.Draw(g, new PointF(470, 40));

 

return footer;

}

 

Sample can be downloaded from the below link.

Sample: http://www.syncfusion.com/downloads/support/directtrac/general/ze/HTMLtoPDF_HeaderFooter-1901900018

Article ID: Published Date: Last Revised Date: Platform: Control:
8381 01/31/2018 01/31/2018 WinForms PDF
Did you find this information helpful?
Add Comment
You must log in to leave a comment

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