rinting is an integral functionality found in almost every decent Windows application. For example, you might be working on a POS (Point of Sale) application and need to print receipts, or you may want to print out employee records from a database. In either case, you need to be able to send the output directly to the printer. What's more, you must have the capability to format the printout the way you want it to appear on paper.
Fortunately, the System.Drawing.Printing namespace in .NET contains all the necessary classes for printing-related services. Specifically, using the PrintDocument class, you can now print from your Windows-based application with ease.
In this article, I will show you how to print from your Windows-based .NET applications. To do so, I'll build a sample application that prints employee name cards using data from the SQL Server Northwind sample database. If you do not already have access to the Northwind database in your environment, you can install it from the freely downloadable SQL Server 2005 Express edition or you can download the Northwind and pubs sample databases for SQL Server 2000.
Creating the Application
|Author's Note: The printing logic described here should work in VS.NET 2003; but the drag-and-drop data-binding shown requires VS 2005.
Using Visual Studio 2005, create a new Windows application and name the project C:\Printing
. You will use the data-binding feature in Windows Forms 2.0 to display records from the Employees table in Northwind.
First, add a data source to the project by selecting Data>Add New Data Source
. The Data Source Configuration Wizard will appear. Select Database, and click Next. Click the "New Connection
" button to create a connection to the SQL Server 2005 Express database server (henceforth referred to as SQL Express).
In the Add Connection window, enter ".\SQLEXPRESS" as the server name (this refers to the SQL Express database server installed on the current computer). Then, select the Northwind database (see Figure 1). Click OK.
|Figure 1. Connecting to Northwind: The sample application uses the Northwind sample database.||
|Figure 2. Picking Your Data: Select the fields (as indicated) from the Employees table.||
Back in the Data Source Configuration Wizard, click Next. In the "Choose Your Database Objects" window, expand the Employees
table and check the following fields (see Figure 2
Click Finish. In the Data Sources window (Data>Show Data Sources), you will now see the Employees
table listed under the NorthwindDataset
item. Click the Employees drop-down ListBox and select Details (see Figure 3
). This will change the binding of the Employees
table from the default DataGridView to individual controls representing each individual field. Also, click on the Photo drop-down ListBox and select PictureBox. This will bind a PictureBox control to the Photo field.
You are now ready to display the selected records on the Windows form. Drag the Employees table from the Data Sources window and drop it onto the default Windows Form1. The default Form1 should now look like Figure 4.
|Figure 3. Select Bindings: Select the appropriate bindings for the Employees table and each field.||
|Figure 4. Drag-and-Drop Data: The figure shows the end result of dragging and dropping the Employees table onto the form.||
|Author's Note: Remember to set theSizeMode property of the PictureBox control (displayed next to the Photo label as a rectangle) to AutoSize. This will allow the photos of the employees to be displayed in their original size.
|Figure 5. Scanning Faces: You can add bar-coding capability to your Windows application.|
To make the application more interesting, I'll add a barcode to the form that contains information about the employee (in this case the employee ID and the employee name). To create it, I used the Barcode Professional control from Neodynamic
. You can download a free trial of this tool or purchase a Developer license for $200.
After installing this control, you can add it to your Toolbox and then drag and drop it onto the form, as shown in Figure 5. Using the barcode control's SmartTag, set the Barcode Symbology property to Code128. Don't worry about the symbology, it's sufficient to know that different barcode applications use different symbologies. For example, books use ISBN (another barcoding symbology), etc.
|Author's Note: You may need to shift the other controls around to make way for the barcode control.