Hot News!

Not Available

Click Like or share to support us!

Jan 13, 2012

Printing Component

Providing support for Printing is one of the common tasks during application development. The .NET Framework provides excellent support for Printing documents.
PrintDocument Component
In the .NET Framework, a printed document is represented by the PrintDocument component. The PrintDocument object encapsulates all the information needed to print a page.  They handle the events and operations of printing. The PrintDocument object exposes three properties which are as follows:
PrinterSettings Property: Contains information about the capabilities and settings of the printers.
DefaultPageSettings Property: Encapsulates the configuration for printing each printed page.
PrintController Property: Describes how each page is guided through the printing process.
How Printing Works?
Printing content is provided directly by the application logic in the .NET Framework. You add a PrintDocument object to the project and handle the PrintPage event which is called every time a new page is to be printed. A print job is initiated by the PrintDocument's Print method. This starts the print job and raises one or more events. When the print job begins, aBeginPrint event occurs, followed by the PrintPage event for each page, followed by the EndPage event when the job is done. If the print job contains multiple pages, one PrintPage event will be raised for each page in the job making the PrintPage event to execute multiple times. The PrintPage event is the main event involved in printing documents. To send content to the printer you must handle this event and provide code to render the content in the PrintPage event handler.
PrintDialogs
Print dialogs are supported by the PrintDialog class and they allow us to print the document. To print a document we need to set the Document property of the PrintDialog to PrintDocument object and PrinterSettings to PrinterSettings object. You can print the document by assigning PrintDialog object's PrinterSettings property to the PrinterSettings property of the PrintDocument object and use the PrintDocument object's Print method.
Properties of the Print Dialog are as follows:
AllowPrintTofile: Gets/Sets whether the Print to file checkbox is enabled.
AllowSelection: Gets/Sets whether the selection radio is enabled.
AllowSomePages: Gets/Sets whether the From...To...Page radio button is enabled.
Document: Gets/Sets the PrintDocument used to obtain PrinterSettings.
PrinterSettings: Gets/Sets the PrinterSettings dialog box to modify.
PrintToFile: Gets/Sets whether the Print to file checkbox is enabled.
ShowHelp: Gets/Sets whether the Help button is displayed.
ShowNetwork: Gets/Sets whether the network button is displayed.
PrintPreviewDialog
Print Preview dialogs are supported by the PrintPreviewDialog class and they allow us to preview the document before printing. You can preview a document by setting the Documentproperty of the PrintPreviewDialog object to the PrintDocument object. Once set, the PrintPreviewDialog provides functionality to zoom, print, to view multiple pages of the preview, etc.
PrintPreviewControl
PrintPreview control allows to create our own custom previews. They display print previews and you can use it to create your own custom print preview windows. To use this control you need to set the print document to it's Document property. 
Notable properties of the PrintPreviewControl are as follows:
AutoZoom: When True (default), resizing the control automatically zooms to make all contents visible.
Columns: Gets/Sets the number of pages displayed horizontally.
Rows: Gets/Sets the number of pages displayed vertically.
StartPage: Gets/Sets the apge number of the upper left page.
Zoom: Gets/Sets a value specifying how large the pages will appear.
PageSetupDialog
Page Setup dialogs are used to specify page details for printing. This dialog allows us to set borders and adjustments, headers and footers, portraits, orientation, etc. You use thePrinterSettings property of this dialog to get a Printer Settings object that holds settings the user specified. The PageSetupDialog exposes a Document property that specifies the PrintDocument to use. Setting the Document property binds the specifed PrintDocument to the PageSetupDialog and any changes made in this dialog are updated in the PrinterSettings property.