1. Home
  2. Computing & Technology
  3. Visual Basic

The Printing Concept in VB.NET

By Dan Mabbutt, About.com

Now you're probably starting to see why Petroutsos writes that many developers just use third party utilities! It takes a lot of code to do all this. You can download an example from the Petroutsos book from the Sybex web site. Search for the ISDN number of the book (0-7821-4349-0) and download the example code for Chapter 20. If you're aware of a simpler way to print, please let me know.

The new, .NET way of printing is based around the PrintDocument object. (In fact, many sources, such as the Petroutsos book referenced above, say that, "you must add ... the PrintDocument control to the form." If you're using a form, this is a convenient way to accomplish the goal (see the illustration below) but if you're not, then you can just create a New instance as I did in my code above. Note that I also wrapped the instance of the PrintDocument object in a ...

Using (...)

End Using

... block to manage memory better. Lesson 8 of my series on VBE shows another example of a Using block.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

PrintDocument, in turn, exposes a Graphics object that you can think of as the paper that will be sent to the printer. The address of the Graphics object is passed by the e parameter from events that are raised by PrintDocument. The Graphics object isn't available directly from PrintDocument. The illustration below shows details.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The end result ... (In most programs. The goal is flexibility so there are lots of ways to do this.) ... is a chain of interlocked objects and code that you add your code into. Beginners, especially those used to the relatively straightforward printing in VB 6, can find this especially confusing. The illustration below shows how the code and the printing objects in VB.NET work together.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Although it adds complexity, there are huge benefits with this approach to printing. To begin, VB.NET supports printing with more sophisticated controls, such as the PageDialog, the PrintPreviewDialog, the PrintDialog and the PrintPreview control. The illustration below shows the PageSetupDialog control.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

But the real reason for this architecture is to give you the ability to control your printing in literally any way that you need to.

When the Print() method of the PrintDocument object is called, the page isn't printed immediately. Instead, a series of events are raised that allow you to compose the whole page. Since the page is treated as a graphics object, you can add content in any order and in any position. This gives you enormous flexibility and power. You can mix text and graphics and place them anywhere on the page - even overlapping them. If you're reading a database and some of the information should be printed at the bottom of the page, some at the top, and some converted into a graph - no problem! To print the next page, simply change the HasMorePages property to true in the PrintPage event and PrintPage will be called again. (Be careful of infinite loops!) The illustration shows how events are raised and demonstrates how the HasMorePages property is used.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The flip side, of course, is that you must provide at least some of the information for printing graphically on a whole page. Note that in my example above, I had to specify the printer that would be used, a font style, a brush style, and a printing position. This is the minimum information that you have to supply to print anything.

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Printing in Visual Basic .NET

©2009 About.com, a part of The New York Times Company.

All rights reserved.