Print Functionality

Printing will undoubtedly play a major part in your business applications. Although electronic file and document storage has become increasingly popular, printed material will not be replaced in the near future. Thus, you must be able to implement print capability in your applications. The .NET Framework provides classes and components that allow you to quickly and easily implement print support for your programs.

The PrintDocument Component

A printed document is represented by the PrintDocument component in the .NET Framework. Although it is not a control with a visual component, the PrintDocument component can be found on the Windows Forms tab of the Toolbox, from where it can be added to your application in the designer.

The PrintDocument object encapsulates all of the information necessary to print a page. It exposes a PrinterSettings property that contains information about the capabilities and settings of the printers; a DefaultPageSettings property that encapsulates the configuration for printing each printed page; and a PrintController property that describes how each page is guided through the printing process. Through these properties, it is possible to attain a very exacting level of control over the print process.

Creating a PrintDocument Object

You can create a PrintDocument object either in the designer at design time or in code at run time. You can drag an instance of the PrintDocument object directly from the Toolbox to the designer. The new instance appears in the component tray and is automatically configured to work with the system's default printer.

How Printing Works

In the .NET Framework printing model, printed content is provided directly by the application logic. A print job is initiated by the PrintDocument.Print method. This starts the print job and then raises one or more PrintPage events. If there is no client method to handle this event, printing does not take place. By providing a method to handle this event, you can specify the content to be printed.

If the print job contains multiple pages, one PrintPage event will be raised for each page in the job. This, in turn, will cause the method handling the PrintPage event to be executed multiple times. Thus, that method must implement some functionality to track the print job and ensure that successive pages of a multi-page document are printed. Otherwise, the first page of the document will be printed multiple times.

The PrintPage Event

The PrintPage event is the main event involved in printing documents. To actually send content to the printer, you must handle this event and provide code to render the content in the PrintPage event handler. All of the objects and information you need to print content to the printer is wrapped in the PrintPageEventArgs object, which is received by this event handler.

The MarginBounds and PageBounds properties represent areas of the page printing surface. You can specify printing to occur inside the margin bounds of the page by calculating printing coordinates based on the MarginBounds rectangle. Printing that is to take place outside of the margin bounds, such as headers or footers, can be specified by calculating the printing coordinates based on the PageBounds rectangle. As with painting to the screen, print coordinates are in pixels by default.

You can specify that a print job has multiple pages by using the HasMorePages property. By default, this property is set to false. When your program logic determines that multiple pages are required to print a job, you should set this property to true. When the last page is printed, the property should be reset to false.

You can also cancel a print job without finishing the current page by setting the Cancel property to true.

You can create an event handler for the PrintPage event by either double-clicking the PrintDocument instance in the designer to create a default event handler or by declaring the event handler in code as described in Chapter 3.

Printing Content

Initiating a print job is as easy as calling the PrintDocument.Print method. In this next section, you will learn how to create the code that determines what content is sent to the printer.

Printing Graphics

Printing graphics is just as easy as rendering them to the screen. You use the Graphics object supplied by PrintPageEventArgs to render graphics to the screen.

Using Color

If your printer supports color printing, you might want to use a different set of printing options than when you are printing to a black and white printer. For example, some colors that are easily discernable on the screen might be virtually indistinguishable when printed. In such a case, you would want to provide alternative logic to accommodate black and white printers.

You can determine if the current printer supports printing color by retrieving the PrinterSettings.SupportsColor property. If the printer supports color, the DefaultPageSettings.Color property will also be set to true, and the printer will print in color by default. To force black and white printing, set the DefaultPageSettings.Color property to false.

To enable printing in your application

From the Toolbox, drag an instance of PrintDocument to the designer. The PrintDocument component appears in the component tray and is automatically configured with the default printer.

You can also create a new PrintDocument in code.

Create a method to handle the PrintDocument.PrintPage event.

Add application logic to the PrintPage event handler to render the content to the printed screen. Use the PrintPageEventArgs.Graphics object to render the content to the printer.

If working with multipage documents, add logic to your application to keep track of paging.

Using PrintPreviewControl

The .NET Framework provides a PrintPreviewControl control to allow you to graphically preview print content before it is actually sent to the printer. The PrintPreviewControl control can be found on the Windows Forms tab of the Toolbox, and you can add an instance of it by dragging it onto your form.

In order to preview a page, the PrintPreviewControl control must be associated with a PrintDocument instance. This association is created by setting the PrintPreviewControl.Document property.

Configuring Printing

The .NET Framework printing interface provides a vast array of configurability options. The PrintDocument.PrinterSettings property contains information about the available printers on a system.

The PrintDocument.DefaultPageSettings property contains the page settings that will be used unless otherwise configured in the PrintPage event handler. The properties exposed by these two members are numerous. Fortunately, they are also self-documenting for the most part.

Furthermore, when a PrintDocument object is created, the default configuration for the default printer is loaded into the PrinterSettings property, and the default page settings are loaded into the DefaultPageSettings property. Thus, it is possible (as you have seen) to create and execute a print job without ever changing the default configuration.

If you want to allow users to set the configuration of their print jobs, Microsoft Visual Studio .NET provides two controls that seamlessly integrate the user interface (UI) with the printer configuration: The PrintDialog control and the PageSetupDialog control.

PrintDialog

The PrintDialog box allows users to set the PrinterSettings property of a PrintDocument object at run time. You can add a PrintDialog box to your application by dragging an instance of the PrintDialog control from the Windows Forms tab of the Toolbox to the designer. At run time, you can display the PrintDialog box by calling the ShowDialog method.

related post

DAY 11 OOPS INTRODUCTION

DAY 12 POLYMORPHISM

DAY 13 INHERITANCE AND POLYMORPHISM

DAY 14 EBUGGING TOOLS IN DOT NET

DAY 15 DEBUG AND TRACE IN CLASSES

DAY 16 UNIT TEST PLAN

DAY 17 EXCEPTIONS IN VISUAL STUDIO

DAY 19 ADO.NET INTRODUCTION

DAY 20 DATA ACCESSING IN DOT NET

DAY 21 DATA BASE OBJECTS


No comments:

Post a Comment