How to Add Headers and Footers to WinForms PDF?
The headers and footers of PDF appear on the top and bottom of the pages in a consistent way with text, image, or shapes. This includes title, page count, page numbers, author name, date, and time using dynamic fields such as PdfPageCountField and PdfPageNumberField in WinForms PDF.
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add headers and footers to a PDF file in C# and VB.NET.
Steps to add headers and footers to a PDF file programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespace in the 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
- Use the following code snippet to add headers and footers in a PDF file.
C#
//Create a new PDF document PdfDocument pdfDocument = new PdfDocument(); //Add a page to the PDF document PdfPage pdfPage = pdfDocument.Pages.Add(); //Create a header and draw an image RectangleF bounds = new RectangleF(0, 0, pdfDocument.Pages[0].GetClientSize().Width, 50); PdfPageTemplateElement header = new PdfPageTemplateElement(bounds); PdfImage image = new PdfBitmap(@"Logo.png"); //Draw an image in the header header.Graphics.DrawImage(image, new PointF(0, 0), new SizeF(100, 50)); //Add the header at the top pdfDocument.Template.Top = header; //Create a page template that is 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, "Rattlesnake Canyon Grocery"); PdfCompositeField compositeField1 = new PdfCompositeField(font, brush, "Milton Dr."); PdfCompositeField compositeField2 = new PdfCompositeField(font, brush, "Albuquerque 87110 USA"); PdfCompositeField compositeField3 = 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(200, 0)); compositeField1.Draw(footer.Graphics, new PointF(230, 10)); compositeField2.Draw(footer.Graphics, new PointF(205, 20)); compositeField3.Draw(footer.Graphics, new PointF(228, 40)); //Add the footer template at the bottom pdfDocument.Template.Bottom = footer; //Save the document pdfDocument.Save("HeaderAndFooter.pdf"); //Close the document pdfDocument.Close(true); //This will open the PDF file so, the result will be seen in default PDF viewer Process.Start("HeaderAndFooter.pdf");
VB.NET
'Create a new PDF document Dim pdfDocument As PdfDocument = New PdfDocument() 'Add a page to the PDF document Dim pdfPage As PdfPage = pdfDocument.Pages.Add() 'Create a header and draw an image Dim bounds As RectangleF = New RectangleF(0, 0, pdfDocument.Pages(0).GetClientSize().Width, 50) Dim header As PdfPageTemplateElement = New PdfPageTemplateElement(bounds) Dim image As PdfImage = New PdfBitmap("Logo.png") 'Draw an image in the header header.Graphics.DrawImage(image, New PointF(0, 0), New SizeF(100, 50)) 'Add the header at the top pdfDocument.Template.Top = header 'Create a page template that is used as footer Dim footer As PdfPageTemplateElement = 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 PdfPageNumberField = New PdfPageNumberField(font, brush) 'Create page count field Dim count As PdfPageCountField = New PdfPageCountField(font, brush) 'Add the fields in composite fields Dim compositeField As PdfCompositeField = New PdfCompositeField(font, brush, "Rattlesnake Canyon Grocery") Dim compositeField1 As PdfCompositeField = New PdfCompositeField(font, brush, "Milton Dr.") Dim compositeField2 As PdfCompositeField = New PdfCompositeField(font, brush, "Albuquerque 87110 USA") Dim compositeField3 As PdfCompositeField = 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(200, 0)) compositeField1.Draw(footer.Graphics, New PointF(230, 10)) compositeField2.Draw(footer.Graphics, New PointF(205, 20)) compositeField3.Draw(footer.Graphics, New PointF(228, 40)) 'Add the footer template at the bottom pdfDocument.Template.Bottom = footer 'Save the document pdfDocument.Save("HeaderAndFooter.pdf") 'Close the document pdfDocument.Close(True) 'This will open the PDF file so, the result will be seen in default PDF viewer Process.Start("HeaderAndFooter.pdf")
Download the work sample from HeaderAndFooterSample.Zip
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation, where you can find drawing the headers and footers in a PDF file and other features like adding shapes to PDF, inserting image in a PDF, and drawing text to PDF with code examples.
Refer here to explore the rich set of Syncfusion Essential PDF features.
An online sample link to add headers and footers with images, text and shapes in all pages of document.
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 headers and footers to WinForms PDF.
You can refer to our WinForms PDF featuretour page to know about its other groundbreaking feature representations and 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!