How to add Header and Footer in WinForms PDF using C# and VB.NET?
The Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add multiline text to the header and footer in the PDF document using C# and VB.NET.
Steps to add multiline text to the header and footer in the PDF document programmatically:
- 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 namespace in 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 sample to add multi-line text in the header and footer of the PDF document.
C#
//Create a new PDF document PdfDocument pdfDocument = new PdfDocument(); //Add a page to the PDF document PdfPage pdfPage = pdfDocument.Pages.Add(); //Adding header PdfPageTemplateElement header = AddHeader(pdfDocument, "Syncfusion Essential PDF", "Header and Footer Demo"); //Add the header template at the top pdfDocument.Template.Top = header; //Add the footer template at the bottom PdfPageTemplateElement footer = AddFooter(pdfDocument); pdfDocument.Template.Bottom = footer; //Draw string in the PDF page pdfPage.Graphics.DrawString("Hello World", new PdfStandardFont(PdfFontFamily.TimesRoman, 20, PdfFontStyle.Bold), PdfBrushes.Black, new PointF(0, 20)); //Save the document pdfDocument.Save("Sample.pdf"); //Close the document pdfDocument.Close(true); //This will open the PDF file and the result will be seen in the default PDF Viewer Process.Start("Sample.pdf");
Helper Methods:
/// <summary> /// Add header to the PDF document /// </summary> /// <param name="doc"></param> /// <param name="title"></param> /// <param name="description"></param> /// <returns></returns> private 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); PdfPen pen = new PdfPen(Color.DarkBlue, 3f); PdfStandardFont pdfFont = new PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Regular); //Draw multiline text in the header string text = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company.he company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets."; header.Graphics.DrawString(text, pdfFont, PdfBrushes.Black, new RectangleF(0,20, doc.Pages[0].GetClientSize().Width, 50)); //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; } /// <summary> /// Add footer to the PDF document /// </summary> /// <param name="doc"></param> /// <returns></returns> private 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 pdfFont = new PdfStandardFont(PdfFontFamily.Helvetica, 7, PdfFontStyle.Italic); PdfPen pen = new PdfPen(Color.DarkBlue, 3f); //Draw some lines in the footer pen = new PdfPen(Color.DarkGray, 0.7f); footer.Graphics.DrawLine(pen, 0, 0, footer.Width, 0); pen = new PdfPen(Color.DarkGray, 2f); footer.Graphics.DrawLine(pen, 0, 03, footer.Width + 3, 03); pen = new PdfPen(Color.DarkGray, 2f); footer.Graphics.DrawLine(pen, 0, footer.Height - 3, footer.Width, footer.Height - 3); footer.Graphics.DrawLine(pen, 0, footer.Height, footer.Width, footer.Height); //Draw multi-line text string text = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European, and Asian commercial markets."; footer.Graphics.DrawString(text, pdfFont, PdfBrushes.Black, new RectangleF(0,20, doc.Pages[0].GetClientSize().Width, 50)); return footer; }
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() 'Adding header Dim header As PdfPageTemplateElement = AddHeader(pdfDocument, "Syncfusion Essential PDF", "Header and Footer Demo") pdfDocument.Template.Top = header 'Add the footer template at the bottom Dim footer As PdfPageTemplateElement = AddFooter(pdfDocument) pdfDocument.Template.Bottom = footer 'Draw string in the PDF page pdfPage.Graphics.DrawString("Hello World", New PdfStandardFont(PdfFontFamily.TimesRoman, 20, PdfFontStyle.Bold), PdfBrushes.Black, New PointF(0, 20)) 'Save the document pdfDocument.Save("Sample.pdf") 'Close the document pdfDocument.Close(True) 'This will open the PDF file and the result will be seen in the default PDF Viewer Process.Start("Sample.pdf")
Helper Methods:
''' <summary> ''' Add header to the PDF document ''' </summary> ''' <param name="doc"></param> ''' <param name="title"></param> ''' <param name="description"></param> ''' <returns></returns> Private Function AddHeader(ByVal doc As PdfDocument, ByVal title As String, ByVal description As String) As PdfPageTemplateElement 'Create a page template Dim rect As RectangleF = New RectangleF(0, 0, doc.Pages(0).GetClientSize().Width, 50) 'Draw multi-line text in the header Dim header As PdfPageTemplateElement = New PdfPageTemplateElement(rect) Dim pen As PdfPen = New PdfPen(Color.DarkBlue, 3.0F) Dim pdfFont As PdfStandardFont = New PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Regular) Dim text As String = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European, and Asian commercial markets." header.Graphics.DrawString(text, pdfFont, PdfBrushes.Black, New RectangleF(0, 20, doc.Pages(0).GetClientSize().Width, 50)) '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 ''' <summary> ''' Add footer to the PDF document ''' </summary> ''' <param name="doc"></param> ''' <returns></returns> Private 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 pdfFont As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 7, PdfFontStyle.Italic) Dim pen As PdfPen = New PdfPen(Color.DarkBlue, 3.0F) 'Draw some lines in the footer pen = New PdfPen(Color.DarkGray, 0.7F) footer.Graphics.DrawLine(pen, 0, 0, footer.Width, 0) pen = New PdfPen(Color.DarkGray, 2.0F) footer.Graphics.DrawLine(pen, 0, 3, footer.Width + 3, 3) pen = New PdfPen(Color.DarkGray, 2.0F) footer.Graphics.DrawLine(pen, 0, footer.Height - 3, footer.Width, footer.Height - 3) footer.Graphics.DrawLine(pen, 0, footer.Height, footer.Width, footer.Height) 'Draw multi-line text Dim text As String = "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European, and Asian commercial markets." footer.Graphics.DrawString(text, pdfFont, PdfBrushes.Black, New RectangleF(0, 20, doc.Pages(0).GetClientSize().Width, 50)) Return footer End Function
Download the work sample from HeaderFooterSample.zip.
By executing the program, you will get the output document as follows,
Take a moment to peruse the documentation. You can find how to add the headers and footers in a PDF file and other features like adding shapes to PDF, inserting images in a PDF, and drawing 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 product. 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 in WinForms PDF using C# and VB.NET.
You can refer to our Winforms PDF Featuretour 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!