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.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add HTML as header and footer in PDF while converting HTML to PDF

Adding a header/footer from HTML file while converting HTML to PDF is possible by using WebKit HTML converter.

Our WebKit HTML converter do not have direct support for adding header/footer from HTML file. As a workaround, we can convert each HTML data to PDF (Header, body (actual HTML), footer) separately and then draw it in the new PDF document as it looks like header and footer added. Please refer below steps and screen shot,

  1. Convert the header and footer HTML documents to PDF with required page height (header/footer height).
  2. Get the template of header and footer PDF documents.
  3. Then set the template to PdfHeader and PdfFooter properties of WebKit converter settings for main conversion.

Note: This requires three level of HTML to PDF conversion.

Assemblies Required

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

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:

HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);

 

WebKitConverterSettings webKitSettings = new WebKitConverterSettings();

 

//convert header HTML and set its template to WebKit settings

webKitSettings.PdfHeader = HeaderHTMLtoPDF();

 

//convert footer HTML and set its template to WebKit settings

webKitSettings.PdfFooter = FooterHTMLtoPDF();

 

//Set WebKit Path

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

 

htmlConverter.ConverterSettings = webKitSettings;

 

//Convert URL to PDF

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

    

//Save the document.

document.Save("Sample.pdf");

 

//Convert header HTML to PDF and get pdf page template element of the result

private PdfPageTemplateElement HeaderHTMLtoPDF()

{

 

HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);

 

WebKitConverterSettings webKitSettings = new WebKitConverterSettings();

 

webKitSettings.PdfPageSize = new SizeF(PdfPageSize.A4.Width,50);

 

//Set WebKit Path

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

 

htmlConverter.ConverterSettings = webKitSettings;

 

string url = System.IO.Path.GetFullPath("../../Data/header.html");

 

//Convert URL to PDF

PdfDocument document = htmlConverter.Convert(url);

 

RectangleF bounds = new RectangleF(0, 0, document.Pages[0].GetClientSize().Width, 50);

 

PdfPageTemplateElement header = new PdfPageTemplateElement(bounds);

 

header.Graphics.DrawPdfTemplate(document.Pages[0].CreateTemplate(), bounds.Location, bounds.Size);

 

return header;

 

}

 

//Convert footer HTML to PDF and get pdf page template element of the result

private PdfPageTemplateElement FooterHTMLtoPDF()

{

 

HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);

 

WebKitConverterSettings webKitSettings = new WebKitConverterSettings();

 

webKitSettings.PdfPageSize = new SizeF(PdfPageSize.A4.Width, 25);

            

//Set WebKit Path

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

 

htmlConverter.ConverterSettings = webKitSettings;

            

string url = System.IO.Path.GetFullPath("../../Data/footer.html");

 

//Convert URL to pdf

PdfDocument document = htmlConverter.Convert(url);

 

RectangleF bounds = new RectangleF(0, 0, document.Pages[0].GetClientSize().Width, 25);

 

PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds);

 

footer.Graphics.DrawPdfTemplate(document.Pages[0].CreateTemplate(), bounds.Location, bounds.Size);

 

return footer;

 

}

 

Working sample can be downloaded from the below link.

Sample : http://www.syncfusion.com/downloads/support/directtrac/general/ze/HTMLHeaderFooter1071669095

 

 

Article ID: Published Date: Last Revised Date: Platform: Control:
8196 11/29/2017 11/29/2017 Windows Forms 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