.NET Controls: The Print Dialog Box |
|
Another operation users perform on a file is to print it. Printing is the ability to render, on paper, the result of a control's content or the contents of various controls. This is performed using an external device called a peripheral. To do this, users need access to a printer device. There are two main ways users print a document or file. 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.
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 such an 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. That is the case when using WordPad; its Standard toolbar is equipped with such a 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 are more than one printer, the computer would use what is known as the default printer. The Print dialog box allows a user to select a printer if more than one are 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 Toolbox to your form. The PrintDialog control is implemented through the PrintDialog class of the System.Windows.Forms namespace. To present it 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. The PrintDocument class 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. This is a small but very useful class that you can use to fully prepare a document. A document can 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. To actually print the document, you can call the PrintDocument.Print() method. Its syntax is: public void Print(); When this 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 doesn't 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 list sometimes 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 this this characteristic. If you are writing an application for a special company 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 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 used clicks OK, a dialog box would come up, asking the user to specify the location and a name for the new file that will be created: 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 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 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 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 you or the user sets this number to 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 © 2004-2010 FunctionX, Inc. | |
|