Click here to Skip to main content
Click here to Skip to main content

A simple TextPrinter

, 26 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
A routine to print out text, images and lines (incl. complete text files).

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 should it 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
    '... further code
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 (incl. formatting - i.e. font, color)
  • FooterPage: The footer shown on all other pages excepting the 1st one (incl. formatting - i.e. font, color)
  • Header1stPage: The header shown on the 1st page (incl. formatting - i.e. font, color)
  • HeaderPage: The header shown on all other pages excepting the 1st one (incl. 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 replace 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 greather 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 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:

'read an external xml file as sample text file
Dim fileContent As String = String.Empty
Using file As New IO.StreamReader(Application.StartupPath & "\print.xml", True)
    fileContent = file.ReadToEnd
End Using

'create new instance and set paper size to A4
Using prt As New TextPrinter(PaperKind.A4)
    With prt
        'set header for the 1st page
        With .Header1stPage
            .TextLeft = "Printed by: " & Environment.UserName
            .TextRight = "Page: [PAGE]" & vbCrLf & Now.ToString
        End With
        'set footer for the 1st page
        With .Footer1stPage
            .TextCenter = "FOR INTERNAL USE ONLY!"
            .ColorCenter = Color.Red
        End With
        'for all other pages we are using a different header and footer:
        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)

        'set the objects those will be printed
        .SetLine() 'no parameters specified: color black is used
        .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()

        'print or show preview instead
        If isPreview Then
            'Show preview
            .ShowPreview(Me)
        Else
            'Prints directly:
            .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 ist used to measure the text for the available drawing size and returns the number of characters those 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
            'set the horizontal text alignment
            If align = HorizontalAlignment.Center Then
                .Alignment = StringAlignment.Center
            ElseIf align = HorizontalAlignment.Right Then
                .Alignment = StringAlignment.Far
            End If
        End With

        'get size of printable text
        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)
                'draw characters those are fitting the rectangle
                .DrawString(Left(text, chrFitted), font, b, r, sf)
            End Using
        End With
        'reduce the rectangle by drawn characters
        r.Y += sz.Height
        r.Height -= sz.Height
    End Using

    'return the drawn number of characters
    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).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Daniel Leykauf
Business Analyst
Austria Austria
I am working as Business Analyst and Development Specialist for an international bank since 1993.

Comments and Discussions

 
SuggestionThank you! Pinmembercode-dk10-Oct-12 12:00 
QuestionMy 5 Pinmemberrspercy6511-Jul-12 13:23 
GeneralMy vote of 5 Pinmemberrctaubert9-Jul-12 5:02 
QuestionWhy hardwire a paper size into your code? Pinmemberrctaubert8-Jul-12 13:23 
AnswerRe: Why hardwire a paper size into your code? PinmemberDanielLey8-Jul-12 14:01 
GeneralRe: Why hardwire a paper size into your code? Pinmemberrctaubert8-Jul-12 14:35 
GeneralRe: Why hardwire a paper size into your code? PinmemberDanielLey9-Jul-12 0:11 
GeneralRe: Why hardwire a paper size into your code? Pinmemberrctaubert9-Jul-12 5:01 
GeneralRe: Why hardwire a paper size into your code? PinmemberDanielLey26-Jul-12 7:49 
GeneralMy vote of 5 Pinmemberrctaubert8-Jul-12 11:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 26 Jul 2012
Article Copyright 2012 by Daniel Leykauf
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid