How to add PDF header and footer while converting HTML to PDF document
The Syncfusion® HTML to PDF converter is a .NET library for converting webpages, SVG, MHTML, and HTML to PDF using C#. It is reliable and accurate. The result preserves all graphics, images, texts, fonts, and the layout of the original HTML document or webpage.
Using this library, we can add header and footer while converting HTML to PDF document.
Assemblies Required
Syncfusion.Compression.Base.dll
Syncfusion.HtmlConverter.Base.dll
Syncfusion.Pdf.Base.dll
Steps to add PDF header and footer while converting HTML to PDF document
- Create a new C# console application project.
- Install the Syncfusion.HtmlToPdfConverter.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in Program.cs file.
C#
using Syncfusion.HtmlConverter; using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; |
- Include the following code example to add PDF header and footer while converting HTML to PDF document.
C#
//Initialize HTML to PDF converter HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Convert header HTML and set its template to Blink settings blinkConverterSettings.PdfHeader = CreateHeader();
//Convert footer HTML and set its template to Blink settings blinkConverterSettings.PdfFooter = CreateFooter();
//Assign the Blink converter settings htmlConverter.ConverterSettings = blinkConverterSettings;
//Convert URL to PDF PdfDocument document = htmlConverter.Convert("https://www.google.com/");
//Save and close the document document.Save("Sample.pdf"); document.Close(true);
//This will open the PDF file so, the result will be seen in default PDF viewer Process.Start("Sample.pdf"); |
//Create header for HTML to PDF converter public static PdfPageTemplateElement CreateHeader() { RectangleF bounds = new RectangleF(0, 0, PdfPageSize.A4.Width, 30);
//Create a new page template and assigning the bounds PdfPageTemplateElement header = new PdfPageTemplateElement(bounds);
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14); PdfBrush brush = new PdfSolidBrush(Color.Black);
string headerText = "Syncfusion HTML to PDF Converter"; SizeF textSize = font.MeasureString(headerText); string date = DateTime.Now.ToString("dd/M/yyyy");
//Create a text field to draw in header PdfCompositeField compositeField = new PdfCompositeField(font, brush, headerText);
//Drawing text field in header compositeField.Draw(header.Graphics, new PointF((bounds.Width - textSize.Width) / 2, 5));
//Drawing date text in header header.Graphics.DrawString(date, font, brush, new PointF(10, 5));
//Drawing line in header header.Graphics.DrawLine(PdfPens.Gray, new PointF(0, bounds.Height - 2), new PointF(bounds.Width, bounds.Height - 2));
return header; } |
//Create footer for HTML to PDF converter public static PdfPageTemplateElement CreateFooter() { RectangleF bounds = new RectangleF(0, 0, PdfPageSize.A4.Width, 30);
//Create a new page template and assigning the bounds PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds);
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 7); PdfBrush brush = new PdfSolidBrush(Color.Black); string footerText = "Copyright © 2001 - 2021 Syncfusion Inc. All Rights Reserved";
SizeF textSize = font.MeasureString(footerText);
//Create a text field to draw in footer PdfCompositeField compositeField = new PdfCompositeField(font, brush, footerText);
//Create page number field to show page numbering in footer, this field automatically get update for each page. PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush); PdfPageCountField count = new PdfPageCountField(font, brush); PdfCompositeField pageNumberField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count);
//Drawing line in footer footer.Graphics.DrawLine(PdfPens.Gray, new PointF(0, 2), new PointF(bounds.Width, 2));
//Drawing text field in footer compositeField.Draw(footer.Graphics, new PointF((bounds.Width - textSize.Width) / 2, 5));
//Drawing page number field in footer pageNumberField.Draw(footer.Graphics, new PointF((bounds.Width - 70), 5));
return footer; } |
You can download the working sample from HTMLHeaderFooter.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation, where you can find converting HTML pages to PDF document along with respective customization options and features.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
An online sample link for Converting HTML to PDF.
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
I hope you enjoyed learning about how to add PDF header and footer while converting HTML to PDF document.
You can refer to our Winforms PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.
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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!