Adding A Header Or Footer To PDF In C#

Introduction

A PDF header and footer presents consistent information in the page margins throughout a PDF, for example, a date, page numbering, the title of the overall document, or author’s name. In this blog, you will learn how to insert text, image, page count, and page numbers in PDF header and footer space, by using free Spire.PDF component in C#.

Background

This library offers a class named PdfPageTemplateElement, which represents a page template that can be used as header, footer, watermark or stamp. The template can contain any type of elements including dynamic fields, such as PdfPageCountField, PdfPageNumberField, etc.

To make the code easy to read, I pre-created two customized functions AddHeader() and AddFooter() that generated the header template and the footer template respectively. Invoking the function in main method, we can apply the template to a newly-build or an existing PDF document to get the same information.

Using the code

Part 1. Add image and text in header space.

  1. static void AddHeader(PdfDocument doc, PdfMargins margin)  
  2. {  
  3.     //Get the size of first page  
  4.     SizeF pageSize = doc.Pages[0].Size;  
  5.   
  6.     //Create a PdfPageTemplateElement object that will be  
  7.     //use as header space  
  8.     PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);  
  9.     headerSpace.Foreground = true;  
  10.     doc.Template.Top = headerSpace;  
  11.       
  12. //Draw image at the top left of header space  
  13.     PdfImage headerImage = PdfImage.FromFile(@"logo.jpg");  
  14.     float width = headerImage.Width / 7;  
  15.     float height = headerImage.Height / 7;  
  16.     headerSpace.Graphics.DrawImage(headerImage, 0, 0, width, height);  
  17.   
  18.     //Draw text at the top right of header space  
  19.     PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Bold|FontStyle.Italic), true);  
  20.     PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);  
  21.     String headerText = "\nAnnual Financial Report\n2017/03/24";  
  22.     float x = pageSize.Width;  
  23.     float y = 0;  
  24.     headerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Black, x, y, format);     
  25. }  
  26. static void Main(string[] args)  
  27. {  
  28.     //Create a new PDF document  
  29.     PdfDocument doc = new PdfDocument();  
  30.   
  31.     //Add two pages in it  
  32.     PdfPageBase page = doc.Pages.Add();  
  33.     doc.Pages.Add();  
  34.   
  35.     //Set the margin  
  36.     PdfUnitConvertor unitCvtr = new PdfUnitConvertor();  
  37.     PdfMargins margin = new PdfMargins();  
  38.     margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);  
  39.     margin.Bottom = margin.Top;  
  40.     margin.Left = unitCvtr.ConvertUnits(4.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);  
  41.     margin.Right = margin.Left;  
  42.   
  43.     //Call AddHeader method to add header information  
  44.     AddHeader(doc, margin);  
  45.   
  46.     //Save the file  
  47. doc.SaveToFile("PDF_Header.pdf");  
  48. doc.Close();  
  49. }  

Result

Result

Part 2. Add text and automatic page numbering at the center of footer space.

  1. static void AddFooter(PdfDocument doc, PdfMargins margin)  
  2. {     
  3.     //Get the size of first page  
  4.     SizeF pageSize = doc.Pages[0].Size;  
  5.   
  6.     //Create a PdfPageTemplateElement object that will be  
  7.     //used as footer space  
  8.     PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margin.Bottom);  
  9.     footerSpace.Foreground = true;  
  10.     doc.Template.Bottom = footerSpace;  
  11.   
  12.     //Draw text at the center of footer space  
  13.     PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Bold), true);  
  14.     PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);  
  15.     String headerText = "Copyright © 2017 xxx. All Rights Reserved.";  
  16.     float x = pageSize.Width / 2;  
  17.     float y = 15f;  
  18.     footerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Black, x, y, format);  
  19.   
  20.     //Create page number automatic field  
  21.     PdfPageNumberField number = new PdfPageNumberField();  
  22.     //Create page count automatic field  
  23.     PdfPageCountField count = new PdfPageCountField();    
  24.     //Add the fields in composite field  
  25.     PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "Page {0} of {1}", number, count);  
  26.     //Align string of "Page {0} of {1}" to center   
  27.     compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Center,PdfVerticalAlignment.Middle);  
  28.     compositeField.Bounds = footerSpace.Bounds;  
  29.     //Draw composite field at footer space  
  30.     compositeField.Draw(footerSpace.Graphics);  
  31. }  
  32. static void Main(string[] args)  
  33. {  
  34.     //Create a new PDF document  
  35.     PdfDocument doc = new PdfDocument();  
  36.   
  37.     //Add two pages in it  
  38.     PdfPageBase page = doc.Pages.Add();  
  39.     doc.Pages.Add();  
  40.   
  41.     //Set the margin  
  42.     PdfUnitConvertor unitCvtr = new PdfUnitConvertor();  
  43.     PdfMargins margin = new PdfMargins();  
  44.     margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);  
  45.     margin.Bottom = margin.Top;  
  46.     margin.Left = unitCvtr.ConvertUnits(4.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);  
  47.     margin.Right = margin.Left;  
  48.   
  49.     //Call AddFooter method to add header information  
  50.     AddFooter(doc, margin);  
  51.   
  52.     //Save the file  
  53.     doc.SaveToFile("PDF_Footer.pdf");  
  54.     doc.Close();  
  55. }  

Result

Result