1. Technology

Printing in Visual Basic .NET

Printing ... Another thing that's not as easy as it used to be


"Maybe we have finally reached the holy grail of the paperless office because Microsoft has removed the ability to print."

This complaint (from one of the Microsoft forums) has been heard a lot since VB.NET was introduced. Printing in VB.NET is tough, but this article will show you how to get started.

I recently received an email from Geoff Miles, a Curriculum Development Officer in Auchinleck, Scotland: "Do you have a solution to simple printing from Visual Basic 2005 Express Edition? We have quite a lot of programs for our students which do a simple ... "

Printer.Print "Hello"

Geoff was writing about the lost simplicity of VB 6. To print a line of text, that's literally all you have to code. But those days are past. In his massive, and really great book Mastering Visual Basic 2005 (ISBN: 0-7821-4349-0), Evangelos Petroutsos writes, "The topic of printing with Visual Basic is a non-trivial topic." To add to your printing problems, Evangelos notes that none of the controls in VB.NET have built-in printing even though a few did have their own printing methods in VB 6. Although his book has some of the best example code for VB.NET printing that I know about, Petroutsos recommends that you invest in some good third party printing software.

In Microsoft's defense, there is a wealth of new power that comes with the new printing technology in .NET. But the simple stuff just isn't simple anymore. To demonstrate the point (and also answer Geoff's question), here's the best I can do in duplicating the "simple" VB 6 Print function:

Dim Print As New myPrinter
Print.prt("About Visual Basic")

Not too bad, eh what Geoff?

Unfortunately, in order for this to work, I also had to write this class and also include it in my project:

Public Class myPrinter
   Friend TextToBePrinted As String
   Public Sub prt(ByVal text As String)
      TextToBePrinted = text
      Dim prn As New Printing.PrintDocument
      Using (prn)
         prn.PrinterSettings.PrinterName _
            = "PrinterName"
         AddHandler prn.PrintPage, _
            AddressOf Me.PrintPageHandler
         RemoveHandler prn.PrintPage, _
            AddressOf Me.PrintPageHandler
      End Using
   End Sub
   Private Sub PrintPageHandler(ByVal sender As Object, _
      ByVal args As Printing.PrintPageEventArgs)
      Dim myFont As New Font("Microsoft San Serif", 10)
         args.Graphics.DrawString(TextToBePrinted, _
            New Font(myFont, FontStyle.Regular), _
            Brushes.Black, 50, 50)
   End Sub
End Class

Not to be too picky, but why couldn't Microsoft create a class like this ... just to throw a bone to the VB 6 programmers?

If you decide to use this code, be aware that the class above also has to be modified to use the actual PrinterName for your computer. And, in addition, if you print a line that is too long, it will simply go past the edge of the paper. In real printing applications, you have to keep track of both the length of lines (using the MeasureString method of the Graphics object) and also the ends of words so you can issue new lines in your program code and not break a line in the middle of a word.

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. Printing in Visual Basic .NET

©2014 About.com. All rights reserved.