How to add header and footer on existing and new WinForms PDF?
The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add the header and footer on each page of an existing PDF document including a newly added page in our Winforms PDF.
Steps to add the header and footer on each page of an existing PDF document including a newly added page,
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Parsing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Parsing
- Include the following code sample to add the header and footer on each page of an existing PDF document including a newly added page.
C#
//Load the PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf"); //Create a new PDF document PdfDocument document = new PdfDocument(); //Import pages from an existing document document.ImportPageRange(loadedDocument, 0, loadedDocument.Pages.Count - 1); //Add page to a new PDF document document.Pages.Add(); //Include the header document.Template.Top = AddHeader(document, "Syncfusion Essential PDF", "Header and Footer Demo"); //Include the footer document.Template.Bottom = AddFooter(document); //Save and close the document document.Save("Output.pdf"); document.Close(true); //This will open the PDF file and the result will be seen in the default PDF viewer System.Diagnostics.Process.Start("Output.pdf");
Helper Methods:
public static PdfPageTemplateElement AddHeader(PdfDocument doc, string title, string description) { RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50); //Create a page template PdfPageTemplateElement header = new PdfPageTemplateElement(rect); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 24); float doubleHeight = font.Height * 2; Color activeColor = Color.FromArgb(44, 71, 120); SizeF imageSize = new SizeF(110f, 35f); //Locating the logo on the right corner of the Drawing Surface PointF imageLocation = new PointF(doc.Pages[0].GetClientSize().Width - imageSize.Width - 20, 5); PdfImage img = new PdfBitmap("logo.png"); //Draw the image in the Header header.Graphics.DrawImage(img, imageLocation, imageSize); PdfSolidBrush brush = new PdfSolidBrush(activeColor); PdfPen pen = new PdfPen(Color.DarkBlue, 3f); font = new PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold); //Set formattings for the text PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; format.LineAlignment = PdfVerticalAlignment.Middle; //Draw title header.Graphics.DrawString(title, font, brush, new RectangleF(0, 0, header.Width, header.Height), format); brush = new PdfSolidBrush(Color.Gray); font = new PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold); format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Left; format.LineAlignment = PdfVerticalAlignment.Bottom; //Draw description header.Graphics.DrawString(description, font, brush, new RectangleF(0, 0, header.Width, header.Height - 8), format); //Draw some lines in the header pen = new PdfPen(Color.DarkBlue, 0.7f); header.Graphics.DrawLine(pen, 0, 0, header.Width, 0); pen = new PdfPen(Color.DarkBlue, 2f); header.Graphics.DrawLine(pen, 0, 03, header.Width + 3, 03); pen = new PdfPen(Color.DarkBlue, 2f); header.Graphics.DrawLine(pen, 0, header.Height - 3, header.Width, header.Height - 3); header.Graphics.DrawLine(pen, 0, header.Height, header.Width, header.Height); return header; }
public static PdfPageTemplateElement AddFooter(PdfDocument doc) { RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50); //Create a page template PdfPageTemplateElement footer = new PdfPageTemplateElement(rect); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 7, PdfFontStyle.Bold); PdfSolidBrush brush = new PdfSolidBrush(Color.Black); //Create the page number field PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush); //Create the page count field PdfPageCountField count = new PdfPageCountField(font, brush); //Add the fields in the composite fields PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count); compositeField.Bounds = footer.Bounds; //Draw the composite field in the footer compositeField.Draw(footer.Graphics, new PointF(470, 40)); return footer; }
VB.NET
'Load the PDF document Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("input.pdf") 'Create a new PDF document Dim document As PdfDocument = New PdfDocument() 'Import pages from an existing document document.ImportPageRange(loadedDocument, 0, loadedDocument.Pages.Count - 1) 'Add page to a new PDF document document.Pages.Add() 'Include the header document.Template.Top = AddHeader(document, "Syncfusion Essential PDF", "Header and Footer Demo") 'Include the footer document.Template.Bottom = AddFooter(document) 'Save and close the document document.Save("Output.pdf") document.Close(True) 'This will open the PDF file and the result will be seen in the default PDF viewer System.Diagnostics.Process.Start("Output.pdf")
Helper Methods:
Public Function AddHeader(ByVal doc As PdfDocument, ByVal title As String, ByVal description As String) As PdfPageTemplateElement Dim rect As RectangleF = New RectangleF(0, 0, doc.Pages(0).GetClientSize().Width, 50) 'Create a page template Dim header As PdfPageTemplateElement = New PdfPageTemplateElement(rect) Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 24) Dim doubleHeight As Single = font.Height * 2 Dim activeColor As Color = Color.FromArgb(44, 71, 120) Dim imageSize As SizeF = New SizeF(110.0F, 35.0F) 'Locating the logo on the right corner of the Drawing Surface Dim imageLocation As PointF = New PointF(doc.Pages(0).GetClientSize().Width - imageSize.Width - 20, 5) Dim img As PdfImage = New PdfBitmap("logo.png") 'Draw image header.Graphics.DrawImage(img, imageLocation, imageSize) Dim brush As PdfSolidBrush = New PdfSolidBrush(activeColor) Dim pen As PdfPen = New PdfPen(Color.DarkBlue, 3.0F) font = New PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold) 'Set formattings for the text Dim format As PdfStringFormat = New PdfStringFormat() format.Alignment = PdfTextAlignment.Center format.LineAlignment = PdfVerticalAlignment.Middle 'Draw title header.Graphics.DrawString(title, font, brush, New RectangleF(0, 0, header.Width, header.Height), format) brush = New PdfSolidBrush(Color.Gray) font = New PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold) format = New PdfStringFormat() format.Alignment = PdfTextAlignment.Left format.LineAlignment = PdfVerticalAlignment.Bottom 'Draw description header.Graphics.DrawString(description, font, brush, New RectangleF(0, 0, header.Width, header.Height - 8), format) 'Draw some lines in the header pen = New PdfPen(Color.DarkBlue, 0.7F) header.Graphics.DrawLine(pen, 0, 0, header.Width, 0) pen = New PdfPen(Color.DarkBlue, 2.0F) header.Graphics.DrawLine(pen, 0, 3, header.Width + 3, 3) pen = New PdfPen(Color.DarkBlue, 2.0F) header.Graphics.DrawLine(pen, 0, header.Height - 3, header.Width, header.Height - 3) header.Graphics.DrawLine(pen, 0, header.Height, header.Width, header.Height) Return header End Function
Public Function AddFooter(ByVal doc As PdfDocument) As PdfPageTemplateElement Dim rect As RectangleF = New RectangleF(0, 0, doc.Pages(0).GetClientSize().Width, 50) 'Create a page template Dim footer As PdfPageTemplateElement = New PdfPageTemplateElement(rect) Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 7, PdfFontStyle.Bold) Dim brush As PdfSolidBrush = New PdfSolidBrush(Color.Black) 'Create the 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 the composite fields Dim compositeField As PdfCompositeField = New PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count) compositeField.Bounds = footer.Bounds 'Draw the composite field in the footer compositeField.Draw(footer.Graphics, New PointF(470, 40)) Return footer End Function
A complete working sample can be downloaded from HeaderFooterSample.zip.
Take a moment to peruse the documentation. You can find how to draw the header and footers in a PDF document and other features like adding shapes to PDF, inserting images in a PDF, and draw text to PDF with code examples.
Refer to here to explore a rich set of Syncfusion Essential® PDF features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.
See Also:
Add the header and footer in an existing PDF document
Add the header and footer from the second page of the PDF document
Add the header and footer in PDF file
Add the header and footer on the first page of the PDF document
Conclusion
I hope you enjoyed learning about how to add header and footer on existing and new WinForms PDF.
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!