WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Managing Printing in Your .NET Application
Pages: 1, 2, 3, 4

When the form is loaded, create an instance of the PrintDocument class and wire up the three main event handlers described earlier:



    Private Sub Form1_Load( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles MyBase.Load

        '---create an instance of the PrintDocument class---
        printDoc = New PrintDocument

        '---set the document name---
        printDoc.DocumentName = "Printing from Windows Forms"

        '---wire up the event handlers---
        AddHandler printDoc.BeginPrint, _
           New PrintEventHandler(AddressOf Me._beginPrint)
        AddHandler printDoc.PrintPage, _
           New PrintPageEventHandler(AddressOf Me._printPage)
        AddHandler printDoc.EndPrint, _
           New PrintEventHandler(AddressOf Me._endPrint)

    End Sub

In the event handler for the BeginPrint event, initialize the page counter as well as the fonts of the text to be used for printing the page:

    Private Sub _beginPrint( _
       ByVal sender As Object, _
       ByVal e As PrintEventArgs)

        '---initialize the page counter---
        pagecounter = 1

        '---initialize the fonts---
        f_title = New Font("Arial", 16, FontStyle.Bold)
        f_body = New Font("Times New Roman", 10)

    End Sub

In the EndPrint event handler, de-reference the font variables used:

    Private Sub _endPrint( _
       ByVal sender As Object, _
       ByVal e As PrintEventArgs)

        '---de-reference the fonts---
        f_title = Nothing
        f_body = Nothing

    End Sub

Finally, the event handler for the PrintPage event is the place where you do the bulk of the work sending the output to the printer. Basically, you use the Graphics object in the PrintPageEventArgs class to specify the output you want to print. For example, to draw a rectangle you would use the e.Graphics.DrawRectangle() method (where e is an instance of the PrintPageEventArgs class). To print a string, you use the e.Graphics.DrawString() method:

    Private Sub _printPage( _
       ByVal sender As Object, _
       ByVal e As PrintPageEventArgs)

        Dim g As Graphics = e.Graphics

        '---draws the title---
        g.DrawString("2D Barcode for Learn2Develop.net", _
           f_title, Brushes.Black, _
           20, 30)

        '---draws a border---
        Dim border As Rectangle = _
           New Rectangle(10, 10, _
           500, 240)
        g.DrawRectangle(Pens.Black, border)

        '---draws the barcode---
        If PictureBox1.Image IsNot Nothing Then
            g.DrawImage(PictureBox1.Image, 20, 60, _
            PictureBox1.Size.Width, _
            PictureBox1.Size.Height)
        End If

        '---draws the page count---
        g.DrawString("Page " & pagecounter, _
           f_body, Brushes.Black, _
           20, 220)

        '---increments the page counter---
        pagecounter += 1

    End Sub

When the user clicks on the Print button, send the output directly to the default printer using the Print() method of the PrintDocument object:

    Private Sub btnPrint_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles btnPrint.Click

        '---print straight to printer---
        printDoc.Print()
    End Sub

You can view the name of the document printed by selecting your default printer in Start | Settings | Printers and Faxes (see Figure 3).


Figure 3. Locating the document sent to the default printer for printing

Pages: 1, 2, 3, 4

Next Pagearrow