Basic Hard Copy Printing

 

This sample application demonstrates how to print a basic hard copy report using VB.NET's PrintDocument object. It should be stated that there are several options for printing reports through a VB.NET program – reports can be created through  "report designer" applications such as MS ReportViewer component and Crystal Reports (both the MS Report Viewer and Crystal Reports are available with the Professional versions of Visual Studio but not the Express versions), the MS-Access Reports feature, and various third-party products; the functionality of MS-Excel and MS-Word can also be harnessed to print from VB.NET. Still, there are times when a basic plain-text printout will do just fine, and the overhead of the aforementioned products is not necessary.

 

However, printing in VB.NET is not as straightforward as it was in classic VB (VB6). In VB6, we had the Printer object, which could be used to produce a basic plain-text printout with low processing overhead. The Printer object is not supported in VB.NET, so it is the PrintDocument object that we must deal with to accomplish the same functionality. The PrintDocument object uses an event-based model that we must follow rather than the straight-ahead processing employed when using the Printer object. The high-level explanation of how to use the PrintDocument object for this type of application is as follows:

 

·         Create (instantiate) a PrintDocument object from the PrintDocument class (objPrintDocument in this example).

·         Add an event handler for the PrintPage event of the PrintDocument object (the PrintPage event fires whenever a new page needs to be printed).

·         Invoke the Print method of the PrintDocument object.

 

The PrintDocument Print method "kicks off" the PrintPage event, and the PrintPage event will fire repeatedly as long as the HasMorePages property is True (this property should be set in a statement near the end of the PrintPage event procedure). It is the code in the PrintPage event that is the heart of the printing process – the code there should perform tasks such as setting the margins, determining the number of lines per page, resetting the line counter, printing the headings, and processing data from the input file until either the input file reaches the end or the page fills up with data. If there are still input records to process after a page has been filled, then the event should be repeated. The actual printing of a line is accomplished with the Graphics.DrawString method.

 

Note: In order to use the PrintDocument object, you must have the following Imports statement at the top of your form file:

Imports System.Drawing.Printing

 

The application reads in a comma-delimited text file of customer records. Each record contains the following fields: last name, first name, address, city, state, and zip code. These fields should be laid out on the printed line with spacing appropriate to facilitate readability. Main headings with date, time, and page number as well as column headings will appear at the top of each page. An excerpt of the printed report is shown below:

 

 

The user interface isn't much to speak of. There are two command buttons: "Print Customer List", which causes the above report to be printed, and an "Exit" button which ends the application.

 

 

 

Note: This application assumes that the customer data file ("customer.txt") resides in the same directory as the executable. Therefore, it should be placed in the bin\Debug folder of your project folder:

 

 

 

Download the project files for this sample application here.