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

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

Result

Result