Note: The printout has been sent to a PDF-Printer to generate this sample.
Introduction
This routine allows to print out text, horizontal lines and images. It provides properties to specify the common used page settings (size, orientation, margins, etc.) and methods to set the text, lines and images plus the related formatting options (i.e., font, color). It uses internally the PrintDocument
(Namespace: System.Drawing.Printing
) and the StandardPrintController
to generate the printouts. The StandardPrintController
is further used to hide the default PrinterDialog
and to print directly to the user's standard printer.
Background
Often, it is required to have a simple routine that prints complete text files or application messages and handles more than one page. Furthemore, it should be easy enough to set the common used page settings and to use standard formattings for the output. It should set internally the correct x and y drawing positions while the user provides only the printing objects (plus optional object related options). To meet all these requirements, I decided to write this class.
Using the Code
To print out your content, you just have to create a new instance of TextPrinter
:
Using prt As New TextPrinter(Printing.PaperKind.Letter)
With prt
With .Header1stPage
.TextLeft = "Printed by: " & Environment.UserName
.TextRight = "Page: [PAGE]" & vbCrLf & Now.ToString
End With
.Landscape = True
End With
End Using
Additionally, you can set the following properties:
DefaultFont
: The default printing font if you do not specify a font for the printing text DocumentName
: The document name used for the print queue and print prepare dialog Footer1stPage
: The footer shown on the 1st page (including formatting - i.e., font, color) FooterPage
: The footer shown on all other pages excepting the 1st one (including formatting - i.e., font, color) Header1stPage
: The header shown on the 1st page (including formatting - i.e., font, color) HeaderPage
: The header shown on all other pages excepting the 1st one (including formatting - i.e., font, color) Landscape
: Defines the used page orientation PaperKind
: The used paper kind - i.e., A4, Letter, etc. TabCharWidth
: The number of space character which replaces the tab character within the text (default: 4).
For the page header and footer, you can set an individual text for the left, middle and/or right position. [PAGE]
is an auto-filled text variable (used in header/footer only) that shows the number of the page.
In the next step, you define the object that should be printed. In this case, a text from an external file via method SetText
. You can choose the used font, forecolor and/or horizontal alignment as optional parameters:
.SetText(My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\print.xml", _
System.Text.Encoding.Default), New Font("Courier New", 10), Color.Blue)
To print a horizontal line, you can define the line color, width and the horizontal alignment in method SetLine
. In case you provide a width value <=1
, the width will be interpreted as prozentual value. That means a value of 0.5
are 50% of the available page width:
.SetLine(Color.Red)
.SetLine(Color.Blue, 0.5, HorizontalAlignment.Center)
An image can be printed via SetImage
. As optional parameters, you can define the horiziontal alignment and the image size. In case the image size is greater than the page size, the image will automatically be resized to the page size:
.SetImage(My.Resources.IMG_3392, HorizontalAlignment.Center)
The following code generates the above sample document. It uses the methods: SetLine
, SetText
, SetImage
, and Print
to create the printout. Alternatively, you can use the method: ShowPreview
to show a PrinterPrintPreview
and to print out the document directly from that preview:
Dim fileContent As String = String.Empty
Using file As New IO.StreamReader(Application.StartupPath & "\print.xml", True)
fileContent = file.ReadToEnd
End Using
Using prt As New TextPrinter(PaperKind.A4)
With prt
With .Header1stPage
.TextLeft = "Printed by: " & Environment.UserName
.TextRight = "Page: [PAGE]" & vbCrLf & Now.ToString
End With
With .Footer1stPage
.TextCenter = "FOR INTERNAL USE ONLY!"
.ColorCenter = Color.Red
End With
With .HeaderPage
.TextRight = "Page: [PAGE]" & vbCrLf & Now.ToString
End With
With .FooterPage
.TextCenter = "FOR INTERNAL USE ONLY!"
.ColorCenter = Color.Red
.TextLeft = "Application:" & vbCrLf & Assembly.GetExecutingAssembly.Location
End With
.DefaultFont = New Font("Tahoma", 10)
.SetLine()
.SetText("START OF PRINTING.", New Font(.DefaultFont, FontStyle.Bold))
.SetLine()
.SetText(fileContent, New Font("Courier New", 10), Color.Blue)
.SetLine()
.SetText(vbCrLf & vbCrLf & "Now we are printing an image (taken in Nassfeld [Austria]:", _
New Font(.DefaultFont, FontStyle.Italic))
.SetImage(My.Resources.IMG_3392, HorizontalAlignment.Center)
.SetText("END OF PRINTING.", New Font(.DefaultFont, FontStyle.Bold))
.SetLine()
If isPreview Then
.ShowPreview(Me)
Else
.Print()
End If
End With
End Using
That's all!
Points of Interest (or a Deeper View)
All drawings are done in class TextPrinter
. The most important methods are _printPage
(it handles the headers and footers plus the printing objects) and _printCharacters
.
_printCharacters
is used to measure the text for the available drawing size and returns the number of characters that were printed:
Private Function _printCharacters(ByVal g As Graphics, ByVal text As String, _
ByVal font As Font, ByVal align As HorizontalAlignment, _
ByVal forecolor As Color, ByRef r As Rectangle) As Integer
Dim chrFitted As Integer
Using sf As New StringFormat
With sf
If text.Contains(vbTab) Then .FormatFlags = StringFormatFlags.MeasureTrailingSpaces
If align = HorizontalAlignment.Center Then
.Alignment = StringAlignment.Center
ElseIf align = HorizontalAlignment.Right Then
.Alignment = StringAlignment.Far
End If
End With
Dim sz As SizeF = g.MeasureString(text, font, New Size(r.Width, r.Height - font.Height), _
sf, chrFitted, 0)
With g
Using b As New SolidBrush(forecolor)
.DrawString(Left(text, chrFitted), font, b, r, sf)
End Using
End With
r.Y += sz.Height
r.Height -= sz.Height
End Using
Return chrFitted
End Function
History
- 1.0.0.0 - Initial post
- 1.0.0.1 - Small changes (Disposal of fonts and images; method
_print
uses the printer default pagesize
if provided paperkind is unsupported)