Printing

Previous to .NET, printing with Visual Basic involved either using the common print dialog control and/or the Win32 API. The former did not always provide enough flexibility and the later sapped the productivity level to which VB developers have become accustomed. System.Drawing.Printing to the rescue! This namespace provides us with a ton of flexibility while encapsulating the low-level stuff that can often bog down programmers.

Sending Output to the Printer

Printing in Windows involves a printer device context. The good news is that this context can be used the same way as any other device context. All the drawing functions presented in the following chapter, for instance, work the same way for printers as they do for display devices. This is one of the reasons you find the Printing namespace tucked under Drawing. This device independence is one of the powerful features that Windows provides. It is possible to render output to either the screen or the printer just by switching device contexts.

Typically, programmers execute the following basic steps when writing printing functionality using the .NET Class Library:

1.
Create a new instance of the PrintDocument class from the System.Drawing.Printing namespace. This is the principal object used to control our printing operation.

Dim printDocument As New System.Drawing.Printing.PrintDocument

2.
Set properties of the PrintDocument class to describe how and where to print. Of course, in an actual application, we would not hard-code the printer's name but would query the system for the default printer instead.

'tell the print document object the name of the printer
printDocument.PrinterSettings.PrinterName = _
    "Epson Stylus COLOR 640 ESC/P 2"

'set the page orientation to landscape
printDocument.PrinterSettings.DefaultPageSettings.Landscape = False

3.
Set an event handler to intercept calls from a delegate when a page is printed. We do this by telling PrintDocument that we want to intercept its PrintPage event. This ensures that the procedure PrintPage_handler will receive the PrintDocument's PrintPage event. This event gets fired for every page that needs to be printed.

AddHandler printDocument.PrintPage, AddressOf Me.printPage_handler

4.
Call the Print method to print the document and raise the print event.

printDocument.Print()

5.
Define an event handler routine to process the printing. Note: The handler's PrintPageEventArgs passes the appropriate Graphics object used to send output to the printer.

Private Sub printPage_handler(ByVal sender As Object, _
    ByVal ev As System.Drawing.Printing.PrintPageEventArgs)

      'draw the string to the printer device
    ev.Graphics.DrawString(s:="Hello World!", _
     font:= New Font("Arial", 10), brush:=Brushes.Black, _
     x:= ev.MarginBounds.Left, y:=ev.MarginBounds.Top, _
     format:=New StringFormat())

End Sub

Printer Configuration

The Printing namespace provides a number of enumerations that enable us to manage various printer configuration settings. This section provides an overview of what is available. It is arranged into a number of tables that describe the enumerations and their members.

Table 6.7 lists the Duplex enumeration members. This enables you to read and write the printer's duplex setting. Duplex, in printing, describes how printers print on both sides of the paper. This enumeration provides values for the PrinterSettings class's Duplex property. A PrinterSettings instance is used to control how a printer is configured to send output.

Table 6.7. Duplex Enumeration Members
Member Description
Default Select the printer's default duplex setting.
Horizontal Select double-sided, horizontal (landscape) printing.
Simplex Select single-sided printing (do not use duplex functionality).
Vertical Select double-sided, vertical (portrait) printing.

PaperKind refers to the physical type of paper loaded into the printer. The enumeration is used by the PaperSize class for its Kind property. PaperSize itself is used by the PrinterSettings class when specifying the PaperSizes property. The PaperSizes property is a collection of PaperSize objects that indicate the various sizes of paper the given printer supports. Table 6.8 lists some of the key PaperKind members most commonly used in the United States.

Table 6.8. PaperKind Enumeration
Member Description
Executive Standard executive paper (7.5 in. by 10.5 in.)
Folio Standard folio paper (8.5 in. by 13 in.)
Ledger Standard ledger paper (17 in. by 11 in.)
Legal Standard legal paper (8.5 in. by 14 in.)
Letter Standard letter paper (8.5 in. by 11 in.)
Tabloid Standard tabloid paper (11 in. by 17 in.)

Table 6.9 documents the PaperSourceKind enumeration members. Paper sources can be thought of as paper trays and the like on a physical printer. The enumeration is used by the PaperSource class for its Kind property. This property is used to both return and to set the source of the paper used when printing. PaperSource itself is used by the PrinterSettings class when specifying the PaperSources property. The PaperSources property is a collection of PaperSource objects that indicates the various paper sources a given printer supports. The most common setting is AutomaticFeed, which tells most printers that they should handle the source from where the paper comes.

Table 6.9. PaperSourceKind Enumeration Members
Member Description
AutomaticFeed Indicates that the source of the paper is automatically fed paper.
Cassette Indicates that the source of the paper is a paper cassette.
Custom Indicates that the source of the paper is a printer-specific paper source.
Envelope Indicates that the source of the paper is an envelope.
FormSource Indicates that the source of the paper is the printer's default input bin.
LargeCapacity Indicates that the source of the paper is the printer's large-capacity bin.
LargeFormat Indicates that the source of the paper is large-format paper.
Lower Indicates that the source of the paper is the lower bin of a printer. Note: If the printer has only one bin, it will be used.
Manual Indicates that the source of the paper is manually fed paper.
ManualFeed Indicates that the source of the paper is manually fed envelopes.
Middle Indicates that the source of the paper is the middle bin of a printer.
SmallFormat Indicates that the source of the paper is small-format paper.
TractorFeed Indicates that the source of the paper is a tractor feed.
Upper Indicates that the source of the paper is the upper bin of a printer. Note: If the printer has only one bin, it will be used.

Table 6.10 lists the various printer resolution settings that are available to your applications. The PrinterResolutionKind enumeration members enable you to tell the printer to output documents based on some standard resolutions. The enumeration is used by the PrinterResoultion class for its Kind property. PrinterResoultion itself is used by the PrinterSettings class when specifying the PrinterResolutions property. The PrinterResolutions property is a collection of PrinterResolution objects that indicate the various resolutions supported by a given printer. These properties are handy when users want a quick version, or draft, of their documents for proofing, or a slower, but high-quality version for release.

Table 6.10. System.Printing and PrinterResolutionKind Enumeration Members
Member Description
Custom Specifies a custom printer resolution setting. If this is set to custom, use the x and y properties of the PrinterResolution class to determine the printer resolution in the horizontal and vertical directions, respectively.
Draft Specifies the draft printer resolution setting.
High Specifies the high printer resolution setting.
Low Specifies the low printer resolution setting.
Medium Specifies the medium printer resolution setting.

Table 6.11 lists the PrintRange enumeration members. The enumeration is used to represent the portions of the document that should be output to the printer. This enumeration is used by the PrinterSettings class to indicate the range that should be printed.

Table 6.11. System.Printing and PrintRange Enumeration Members
Member Description
AllPages Indicates that all pages in the document should be printed.
Selection Indicates that only the selected pages in the document should be printed.
SomePages Indicates that only some pages (those from x to y) in the document should be printed. If using this value, reference the PrinterSettings class, fromPage and toPage properties.

Previewing Documents Prior to Printing

Before sending documents to the printer, users often need to see how their output is going to look. This saves paper, ink, and time. For instance, Microsoft Word provides the print previewing functionality. .NET exposes two classes that enable developers to simulate this functionality. The PreviewPrintController class is a print controller that displays documents to the screen as a series of images. This class makes extensive use of the PreviewPageInfo class. The PreviewPageInfo class represents the print preview information of one specific page. The following is a step-by-step walkthrough of adding print preview capabilities to your application:

1.
First, you declare a number of variables to represent the classes you will need.

Dim printDocument As System.Drawing.Printing.PrintDocument
Dim previewController As System.Drawing.Printing.PreviewPrintController
Dim pageInfoArray() As System.Drawing.Printing.PreviewPageInfo
Dim i As Integer

2.
Next, create an instance of a PrintDocument class:

printDocument = New System.Drawing.Printing.PrintDocument()

3.
Then, create an instance of the PreviewPrintController class.

previewController = New System.Drawing.Printing.PreviewPrintController()

4.
Set the PrintDocument object's PrintController property to your PreviewPrintController object. This ensures that your output is sent to a set of images rather than to the printer.

printDocument.PrintController = previewController

5.
Next, indicate an event handler for each page that is output. Note: The event handler can be identical to the one defined in the basic printing walkthrough. Perhaps one exception is that you might want to add the line ev.Graphics.ScaleTransform(sx:=0.25, sy:=0.25) to the printPage_handler routine. This code scales the output of the Graphics object by 25%, which makes viewing print images a little easier.

AddHandler printDocument.PrintPage, AddressOf Me.printPage_handler

6.
Now call the Print method of the PrintDocument instance. This raises a call to the event handler and forces the printed content into the preview print controller.

printDocument.Print()

7.
All that is left is to view the preview images. After the document is finished printing, the PreviewPrintController instance that you set as the target of the print output is filled with a collection of PreviewPageInfo objects. There is one item in the collection per printed page. Return the images by calling the GetPreviewPageInfo method of the PreviewPrintController object. You can then loop through the array and output each image into a picture box defined on a form.

'return an array of PreviewPageInfo objects from the print controller
pageInfoArray = previewController.GetPreviewPageInfo()

'loop through the array and display each image
For i = 0 To UBound(pageInfoArray)
     Me.pictureBoxPreview.Image = pageInfoArray(i).Image
Next

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.227.231