Visual Basic.NET 2008 > Printing with Visual Basic

Printing With Visual Basic 2008

Jump to Table of Contents

The topic of printing with Visual Basic is a not trivial, and many developers use third-party tools to add print capabilities to their applications. As you already know, there's no control with built-in printing capabilities. It would be nice if certain controls, such as the TextBox or the ListView control, would print their contents, but this is not the case. Even to print a few text paragraphs entered by the user on a TextBox control, you must provide your own code.

Printing with VB isn't complicated, but it requires a lot of code — most of it calling graphics methods. You must carefully calculate the coordinates of each graphic element placed on the paper, take into consideration the settings of the printer and the current page, and start a new page when the current one is filled. It's like generating graphics for the monitor, so you need a basic understanding of the graphics methods, even if you're only going to develop business applications. If you need to generate elaborate printouts, I suggest that you look into third-party controls with built-in printing capabilities, because the controls that come with Visual Studio have no built-in printing capabilities.

The examples of this chapter will address many of your day-to-day needs, and I'm including examples that will serve as your starting point for some of the most typical printing needs, from printing tabular data to bitmaps.

In this chapter, you'll learn how to do the following:

  • Use the printing controls and dialog boxes - To print with the .NET Framework, you must add an instance of the PrintDocument control to your form and call its Print method. To preview the same document, you simply assign the PrintDocument object to the Document property of the PrintPreviewDialog control and then call the ShowDialog method of the PrintPreviewDialog control to display the preview window. You can also display the Print dialog box, where users can select the printer to which the output will be sent, and the Page Setup dialog box, where users can specify the page's orientation and margins. The two dialog boxes are implemented with the PrintDialog and PageSetupDialog controls.

  • Print plain text and images - Typical business applications generate printouts with text and a few borders or grids. The DrawString method of the Graphics object can print a string at a specific location on the page. To print images, call the DrawImage method of the Graphics object, passing as an argument the image you want to print and the rectangle on the page where you want the image to appear.

  • Print tabular data - Business applications make heavy use of reports, and you should provide a mechanism to print these out. Printing tabular data isn't a simple task, but after you break the page into rows and columns, you can draw the appropriate string into its corresponding cell.

The Printing Components

We'll start our exploration of Visual Basic's printing capabilities with an overview of the printing process, which is the same no matterwhat you print. In this section, you'll find a quick overview of the printing controls (you'll find more information on them, as well as examples, in the following sections). You don't need to use all these components in your project. Only the PrintDocument component is required, and you will have to master the members of this control.

Get Started >>

 

Table of Contents

  1. Printing With Visual Basic
  2. The PrintDocument Control
  3. The PrintDialog Control
  4. The PageSetupDialog Control
  5. The PrintPreviewDialog Control
  6. Printer and Page Properties
  7. Retrieving the Printer Names & Page Geometry
  8. The SimplePrintout Example
  9. The PageSettings Example
  10. Printing Tabular Data
  11. Printing Plain Text
  12. Printing Bitmaps





     
 
W3computing.com Copyright 2011 All Rights Reserved
 
  Home | Useful links | Contact us