Winform Reports for .NET
   Product Comparison
     PrintForm .NET
     PrintControls .NET
     PrintAdapters .NET
     PrintPreviewDialog .NET
     Localizer ASP.NET
     WebExtenders ASP.NET
     Free Trial Versions
   Documents & Papers
   Support Index
   Press Releases
   Contact Us
   Privacy Statement
Go to TMG Development Ltd Website
Microsoft Certified Partner

HowTo: Draw Headers and Footers from Code

ID: W3298014



In some situations you may want to draw headers and footers on the printed page manually. PrintForm can do this for you, but if you are using PrintAdapters or PrintControls in isolation, or if you are using PrintChainManager to join the output of more than one of these components into a single print job you will need to add the following code to ensure that header and footer text is drawn on the printed page.

Drawing Text Using Graphics.DrawString

This code illustrates how to calculate the location for header and footer based on the PrintPageEventArgs.MarginBounds rectangle, and then uses the Graphics.DrawString method to render the text. Note that this code is for the PrintChainManager, but since all our printing controls including PrintChainManager are derived from the framework's PrintDocument class, they all have a PrintPage event you can handle to execute this code:

Private Sub PrintChainManager1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintChainManager1.PrintPage
  ' TODO: if not using PrintChainManager you may need to keep track of the page
  ' in a member variable. In this case though PrintChainManager does it for us:
  Dim pageNumber As Integer = Me.PrintChainManager1.PageNumber

  ' Calculate a location for the header in the top margin space
  Dim locationHeader As Point = e.MarginBounds.Location
  locationHeader.Offset(e.MarginBounds.Width / 2 - 30, -15)
  ' Draw the header text using the default Font of the Form
  e.Graphics.DrawString("Heading of Page " + pageNumber.ToString(), Me.Font, Brushes.Black, locationHeader.X, locationHeader.Y)
  ' Calculate a location for the fotter in the bottom margin space
  Dim locationFooter As Point = e.MarginBounds.Location
  locationFooter.Offset(e.MarginBounds.Width / 2 - 30, e.MarginBounds.Height)
  ' Draw the footer text
  e.Graphics.DrawString("Footer of Page " + pageNumber.ToString(), Me.Font, Brushes.Black, locationFooter.X, locationFooter.Y)
End Sub

Last updated 30-Mar-2019