Windows Control: The Print Dialog Box
Introduction to Printing
Besides saving or opening files, another operation users perform on a document consists of printing it. Printing is the ability to render, on paper, the result of a document or the contents of various controls. This is performed using an external device called a printer peripheral or simply a printer. To do this, users need access to a printer device.
There are two main ways users print a document. They can ask the application they are using to send the document directly to a printer or they can use a dialog box to decide how the printing should be done.
One of the ways users print consists of sending the document to the printer. To directly send a document to the printer, you need to make sure that the control, whose value needs to be printed, supports printing. To accommodate the users of your application, you can provide a menu item or a button they would click. An example of such a button would be . To print, the user can click this button. With this type of printing, when the user decides to print, the whole document would be printed "as is", in color if the document is colored and if the printer supports colors. If there is more than one printer, the computer would use what is known as the default printer.
If you want users to be able to configure or customize the printing process, Microsoft Windows provides a common dialog box called Print. Here is an example:
The Print dialog box allows a user to select a printer if more than one is available. The user can decide either to print the whole document, to print a range of pages, or to print a portion of the document that was previously selected. The user can also decide on the number of copies to print from the document, the range specified, or the selected portion. Furthermore, the user can access the particular characteristics of the selected printer and specify how the printer should perform the job. For example, if the selected printer can print in color and the document is in color but the user wants to print in black and white, he or she can specify this using the Properties button.
To provide the users with the ability to customize printing through the Print dialog box, you can add a PrintDialog object from the Dialogs section of the Toolbox to your form. The PrintDialog control is implemented through the PrintDialog class of the System.Windows.Forms namespace. To programmatically create a PrintDialog object, you can declare a variable of type PrinterDialog. Here is an example:
Imports System.Drawing Imports System.Windows.Forms Module Exercise Public Class Starter Inherits Form Friend WithEvents btnPrint As Button Dim components As System.ComponentModel.Container Public Sub New() InitializeComponent() End Sub Public Sub InitializeComponent() btnPrint = New Button() btnPrint.Location = New Point(12, 12) btnPrint.Text = "&Print..." Controls.Add(btnPrint) End Sub Private Sub PrintClicked(ByVal sender As Object, ByVal e As EventArgs) Handles btnPrint.Click Dim dlgPrint As PrintDialog = New PrintDialog End Sub End Class Function Main() As Integer Dim frmStart As Starter = New Starter Application.Run(frmStart) Return 0 End Function End Module
To present the Print dialog box to the user, you can call its ShowDialog() method.
In order to print, the Print dialog box must be given a document to print. This means that you must first prepare a document prior to printing. To support this, the .NET Framework provides the PrintDocument class that is defined in the System.Drawing.Printing namespace. This class is represented in the Toolbox by the PrintDocument button . Based on this, to prepare a document for printing, you can either add a PrintDocument object to your project or declare a variable of type PrintDocument. Here is an example:
Imports System.Drawing Imports System.Windows.Forms Imports System.Drawing.Printing Module Exercise Public Class Starter Inherits Form Friend WithEvents btnPrint As Button Dim components As System.ComponentModel.Container Public Sub New() InitializeComponent() End Sub Public Sub InitializeComponent() btnPrint = New Button() btnPrint.Location = New Point(12, 12) btnPrint.Text = "&Print..." Controls.Add(btnPrint) End Sub Private Sub PrintClicked(ByVal sender As Object, ByVal e As EventArgs) Handles btnPrint.Click Dim dlgPrint As PrintDialog = New PrintDialog Dim docPrint As PrintDocument = New PrintDocument End Sub End Class Function Main() As Integer Dim frmStart As Starter = New Starter Application.Run(frmStart) Return 0 End Function End Module
After creating the document to print through a PrintDocument object, you can associate it with a PrintDialog. To support this, the PrintDialog class is equipped with the Document property. To specify the object that would carry the printing, you can assign the PrintDocument object to the PrintDialog.Document property. Here is an example:
Private Sub PrintClicked(ByVal sender As Object, ByVal e As EventArgs) Handles btnPrint.Click Dim dlgPrint As PrintDialog = New PrintDialog Dim docPrint As PrintDocument = New PrintDocument dlgPrint.Document = docPrint dlgPrint.ShowDialog() End Sub
A document to print can be made of only one or many pages. Each page has a number of characteristics. The characteristics of a page are controlled by the PrintDocument.PageSettings property which itself is based on the PageSettings class. The PageSettings class is defined in the System.Drawing.Printing namespace. This class holds the dimensions of the page, the values of the margins applied on the page, the tray that would supply the paper (since some printers have many trays), the printer resolution, whether the page would be printed in color or black and white, whether the page would be printed in Portrait or Landscape orientation, etc. If you don't want to specify these characteristics, you can set the PrintDocument.PageSettings property to DefaultPageSettings.
If you know the name of the document to be printed, you can assign it to the PrintDocument.DocumentName property. Here is an example:
Private Sub PrintClicked(ByVal sender As Object, ByVal e As EventArgs) Handles btnPrint.Click Dim dlgPrint As PrintDialog = New PrintDialog Dim docPrint As PrintDocument = New PrintDocument docPrint.DocumentName = "Example.htm" dlgPrint.Document = docPrint dlgPrint.ShowDialog() End Sub
To actually print the document, you can call the PrintDocument.Print() method. Its syntax is:
Public Sub Print
When the PrintDocument.Print() method is called, the printing process would start by firing the BeginPrint event but this event occurs before the first page is printed. The BeginPrint event is of type PrintEventArgs which does not hold any particular information, especially for the BeginPrint event. This event allows you to take some early actions, if necessary, before the printer receives the job.
Once the printer is ready, the application would then need to know what needs to be printed on the paper. At this time, the PrintPage event is fired. The PrintPage event is of type PrintPageEventArgs. The PrintPageEventArgs class allows you to fully customize the page of the document to be printed. For example, it is equipped with a Graphics property that allows you to "draw" anything you want on the paper. The PrintPageEventArgs class also allows you to get the location and dimensions of the area to be printed. This is represented by the PageBounds property, which produces a Rectangle object.
Once the PrintDocument object is ready, you must pass it to the Print dialog box. To do that, assign the name of the PrintDocument variable to the PrintDialog.Document property.
In the above example, we saw a somewhat simplistic way of making the Print dialog box available to the user. This dialog box offers many options defined as the Printer Settings. To support the various options of a Print dialog box, the PrintDialog class is equipped with a property called PrinterSettings, which itself is defined from the PrinterSettings class, which holds all possible characteristics of a printer.
The first option presented to the user is the name of the printer to be used. Because there can be many printers available to the user, the printers are presented as a combo box:
The available printers may also be presented as a list view:
The Name combo box in the Printer section or the Select Printer list view allows the user to select the printer that will handle the job. If you are writing a universal application and cannot predict what printer(s) the user would have, you would not be concerned with this characteristic. If you are writing an application for a special company or you are creating a particular application and you know for sure what printer should be used to print the current document, then you can specify the printer to use. To do this, assign the (exact) name of the printer to the PrinterSettings.PrinterName property. On the other hand, if for some reason you want to know what printer the user selected to print the document, you can get the value of this PrinterName property.
Under the Name combo box, the labels provide the status of the selected printer (whether it is ready or not), the type of printer (such as its manufacturer), its location (such as where in the building the printer is located; the person who installed the printer or may have provided this printer), and an optional comment (this information is created by the person who installed the printer or it can be changed in the printer's properties).
After selecting the printer, the user can access the properties of that particular printer. Different printers support different options. To configure the printed paper based on the selected printer, the user can click either Properties or Preferences. This opens the Document Properties or the Printing Preferences dialog box. The content of this dialog box (highly) depends on the printer that was selected but some characteristics are shared among printers.
On the lower-right side of the Printer section or of the Select Printer section, there is a check box labeled Print To File. When this check box is checked, the document is transformed into a file rather than being printed. In this case, if the user clicks OK or Print, a dialog box would come up, asking the user to specify the path and a name for the new file that will be created. The appearance of that dialog box depends. Here is an example:
If you want the Print To File check box to be checked, set the PrinterSettings.PrintFoFile Boolean property to true. Its default value is false, which lets the user decide whether to check it or not.
After selecting the printer and deciding whether to physically print or to only create a printed file, the user can click OK.
If the document is made of only one page, it would be printed. If the document contains more than one page, the user may want to print only one page, a range of pages, or all pages. The user can also select a section in the document and print only that section. This decision is made using the Page Range section. This section provides four radio buttons. The first radio button labeled All is selected by default and allows the user to print the whole document. By default, the second radio button is disabled. If the user had selected a section of the document to print, then the second radio button, labeled Selection would be enabled:
This allows the user to still specify whether to print the whole document or only the section that was selected. If the document contains more than one page, the user can navigate to a particular page and decide to print only that page using the Current Page radio button. Again, if the document contains more than one page, the user can specify a range of pages to print. All these options are usually left up to the user. On the other hand, if you want to specify the range of pages to print, you can use the PrinterSettings.FromPage and the ToPage properties. If you want to specify the limits of ranges allowed to the user, use the MinimumPage and the MaximumPage properties.
On the right side of the Page Range section, a spin button allows the user to specify the number of copies to make when printing. If you want to specify this number by default, assign the desired value to the Copies property. If the user (or you) set this number to a value higher than 1, then the printed papers can be collated or not. This is specified using the Collate check box. If you want to programmatically collate the pages or not, change the Boolean value of the Collate property.
|Home||Copyright © 2008-2012 FunctionX|