PrintHelperPages VB Class

by Bill Dowling 29. July 2011 12:49

Printing in a .Net (or any VB) program has always been a challenge. I developed the following VB.Net Class (with guidance from others) using Visual Studio 2010 to help simplify the printing process. This Class was developed to handle multi-page printed reports.

Formatting and creating a multi-page printed report is done by a two step process.

  • First, create the PrintHelperPages Class and place the Class in "Layout Mode" by setting the LayoutMode property to True. Then create the report by using the Print Functions. This will create a list of phLayout structures that define where on each page your text will be printed. This first pass will detect when a line will exceed the allowable printable region for a page. If a line will exceed the printable region, a new page is forced and the line will be located at the top of the next page.
  • Next, turn off LayoutMode and call the Print Routines with the phLayout structures to print a specific page.

The class can be created with:

  • Dim php As New PrintHelperPages(_event, _font, _debug)

    ParameterTypeDescription
    _event System.Drawing.Printing.PrintPageEventArgs From the Event Handler
    _font Font (optional) Default Font
    _debug Boolean (optional) Debug Flag (draw boxes around text) - Default is False

Text can be Laid-Out or Printed by calling one of the following routines. These routines will return a formatted Print Helper Page Layout Structure:

  • Dim phl As phLayout = php.Print(_text, _UpdateX, _HeaderFooter, _AlignToX)
  • Dim phl As phLayout = php.Print(_text, _x, _y, _UpdateX, _HeaderFooter, _AlignToX)
  • Dim phl As phLayout = php.Print(_text, _fontIndex, _UpdateX, _HeaderFooter, _AlignToX)
  • Dim phl As phLayout = php.Print(_text, _fontIndex, _x, _y, _UpdateX, _HeaderFooter, _AlignToX)
  • Dim phl As phLayout = php.Print(_text, _fontIndex, _sfIndex, _UpdateX, _HeaderFooter, _AlignToX)
  • Dim phl As phLayout = php.Print(_text, _fontIndex, _sfIndex, _y, _UpdateX, _HeaderFooter, _AlignToX)
  • Dim phl As phLayout = php.Print(_text, _fontIndex, _sfIndex, _x, _y, _UpdateX, _HeaderFooter, _AlignToX)

    ParameterTypeDescription
    _text String String to print
    _fontIndex Integer Index of Font in Font List to use for this string
    _sfIndex Integer Index of StringFormat in StringFormat List to left/right/center justify the text
    _x Single X or horizontal position on the page to begin printing
    _y Single Y or vertical position on the page to begin printing
    _UpdateX Boolean (optional) Update X Coordinate after Print - Default is False
    _HeaderFooter Boolean (optional) Are we printing a Header or Footer outside usable range - Default is False
    _AlignToX Boolean (optional) Alignment is based on X rather than margin width - Default is False

To add a formatted phLayout structure to the list, the following is an example:

  • php.LayoutMode = True
  • Dim phl As New List(Of PrintHelperPages.phLayout)
  • phl.Add(m_php.Print("First Line on the Report"))
  • phl.Add(m_php.Print("Second Line on the Report"))
  • php.LayoutMode = False
  • php.Print(phl, 1)     ' Actually print page one

If no X position is given, the string will begin at the left margin unless _UpdateX option was used on a prior Print statement. The _UpdateX option will reset the default X position to the end of the current Text printed. Don't forget to reset the default X position by using the xPos property, usually setting it to e.MarginBounds.X.

If no Y position is given, the current vertical position will be used, which is updated after each line is printed.

A new feature has been added to allow the alignment to be based on the X position rather than the usable region width. For example, if AlignToX is set the False (default value) and you have a string format set to center, your text will be centered on the page. But, if set AlignToX to True, then your text will be centered on your X coordinate.

Print Helper Page Layout Structure (phLayout):

  • Dim phl As New phLayout

    VariableTypeDescription
    Text String Text to Print
    fIndex Integer Font List Index
    sfIndex Integer StringFormat List Index
    Page Integer Page Number of Text
    x Single X Position to start
    y Single Y Position to start
    AlignToX Boolean Alignment is based on X rather than margin width

The following Routines can be called after your print pages have been Laid-Out to actually print your pages. You pass as a parameter either a single phLayout record or the entire list of phLayout structures to print a specific page.

  • php.Print(_phLayouts As List(Of phLayout), _page As Integer)
  • php.Print(_phl As phLayout)

    ParameterTypeDescription
    _phLayouts List(Of phLayout) List Array of formatted Print Helper Page Layout Structures
    _page Integer Specific page to print
    _phl phLayout Formatted Print Helper Page Layout Structure

Class Properties:

Property NameTypeDescription
Debug Boolean Get or Set the Debug Flag
LayoutMode Boolean Get or Set whether we are in Layout Mode or Not
DefaultFont Font Get or Set the Default Font
GetFont(_Index As Integer) Font Get the Specific Indexed Font from List
FontHeight Single Get or Set the Default Font Height
FontSpacing Single Get or Set the Default Font Spacing or Padding
DefaultStringFormat StringFormat Get or Set the Default StringFormat
GetStringFormat(_Index As Integer) StringFormat Get the Specific Indexed StringFormat
SetBrush Brush Set the Default Brush
xPos Single Get or Set the Current x (horizontal) Position
yPos Single Get or Set the Current y (vertical) Position
Page Integer Get the Current Page Number

Class Routines:

Routine NameParametersDescription
AddFont System.Drawing.Font Add a Font to the List
AddStringFormat System.Drawing.StringFormat Add a StringFormat to the List
AddBlankLine   Add a Line Height to the Current Y Location

The PrintHelperPages.vb Class:

Tags:

.Net Coding Examples

Add comment

About the author

Bill Dowling has been a VB and .Net programmer for as long as he can remember.

Month List