ProblemYou're ready to print. How do you do it? SolutionSample code folder: Chapter 11\TextAndGraphics Respond to the various events of the PrintDocument object, especially the PrintPage event. DiscussionThe following code sends a two-page document to the default printer when the Button1 button is clicked. Each page includes some simple text and graphics: Imports System.Drawing.Printing Public Class Form1 Private WithEvents SampleDoc As Printing.PrintDocument Private PageNumber As Integer Private Sub Button1_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click SampleDoc = New Printing.PrintDocument SampleDoc.Print() End Sub Private Sub SampleDoc_BeginPrint(ByVal sender As Object, _ ByVal e As System.Drawing.Printing.PrintEventArgs) _ Handles SampleDoc.BeginPrint ' ----- Start the page counting. PageNumber = 0 End Sub Private Sub SampleDoc_PrintPage(ByVal sender As Object, _ ByVal e As Printing.PrintPageEventArgs) _ Handles SampleDoc.PrintPage ' ----- Keep track of the current page. PageNumber += 1 If (PageNumber >= 2) Then e.HasMorePages = False Else _ e.HasMorePages = True ' ----- Let's use inches, a nice easy measurement system. e.Graphics.PageUnit = GraphicsUnit.Inch ' ----- Print some text and rectangles. e.Graphics.DrawString("This is page " & PageNumber & _ ".", New Font("Ariel", 48, FontStyle.Regular), _ Brushes.Black, 2, 2) e.Graphics.DrawRectangle(New Pen(Color.Blue, 0.005), _ 3.0!, 3.0!, 3.0!, 0.5!) e.Graphics.DrawRectangle(New Pen(Color.Red, 0.005), _ 3.25!, 3.25!, 3.0!, 0.5!) End Sub End Class This sample prints two pages similar to the pages in Figure 11-5. Figure 11-5. Output from the PrintPage eventMake sure that you include the WithEvents keyword in your PrintDocument variable's declaration, or you won't be able to attach an event handler with the Handles keyword. All printing for a document occurs in the PrintDocument object's PrintPage event. This event includes an e.Graphics property that exposes a full GDI+ graphics drawing surface for a single document page. Use any of the GDI+ drawing methods (such as DrawString(), FillPie(), or DrawImage()) or display transformation features (such as translateTransform()) that you need to organize and print your page. It's up to you to determine which pages should be included in the print document, and even which page numbers to use. The PrintDocument object does not know which pages should be included, so you need to do those calculations yourself, as was done in this recipe with the PageNumber class member. The PrintDocument. PrinterSettings object's PrintRange, FromPage, and ToPage properties indicate the user-selected pages to include in the output. Printing will continue until you tell it to stop. The PrintPage event's e.HasMorePages property controls everything. Set it to true if there are more pages to print after the current page or to False when you are printing the last page. Besides the PrintPage event, the PrintDocument object includes a few other useful events:
See AlsoChapter 9 includes examples of GDI+ features you can use on the graphics surface. |