Printing multiple pages

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
I'm just learning how to convert my VB6 printing code to VB.NET. I've looked through most of the articles on here, but I cant find one that relates to my problem.

I want to print two pages. Standard laser printer, nothing special. When I call myPrinter.TestPrint(), what I actually get is one page with both pages printed onto one. In other words it shows "Test page 1" and "Test page 2" on the first page, and I dont get a second page.

I'm sure it's a basic error, but I cant find it. Any ideas please ?

VB.NET:
Class myPrinter
    Private WithEvents pd As New PrintDocument()
    Private shtPageNumber As Int16 = 0
    Private shtTotalPages As Int16 = 0

    Public Sub TestPrint()
        pd.DocumentName = "Test Document"
        pd.PrintController = New Printing.StandardPrintController()
        AddHandler pd.BeginPrint, AddressOf Begin_Printing
        AddHandler pd.PrintPage, AddressOf Test_PrintPage
        shtTotalPages = 2
        Try
            pd.Print()
        Catch e As Exception
            MessageBox.Show("An error occurred !" & vbCrLf & vbCrLf & e.Message)
        End Try
        RemoveHandler pd.PrintPage, AddressOf Test_PrintPage
        RemoveHandler pd.BeginPrint, AddressOf Begin_Printing
    End Sub

    Private Sub Begin_Printing(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles pd.BeginPrint
        shtPageNumber = 0
    End Sub

    Private Sub Test_PrintPage(ByVal sender As System.Object, ByVal e As PrintPageEventArgs) Handles pd.PrintPage
        shtPageNumber += 1
        Dim f As New Font("Arial", 18, FontStyle.Bold)
        e.Graphics.DrawString("TEST PAGE " & shtPageNumber.ToString(), f, Brushes.Black, 15, 12 * shtPageNumber)

        If shtPageNumber < shtTotalPages Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If
    End Sub

End Class
 
try taking out these two lines:
VB.NET:
AddHandler pd.BeginPrint, AddressOf Begin_Printing
AddHandler pd.PrintPage, AddressOf Test_PrintPage

RemoveHandler pd.PrintPage, AddressOf Test_PrintPage
RemoveHandler pd.BeginPrint, AddressOf Begin_Printing

Because you're using the WithEvents keyword when you declare the pd As New PrintDocument, the "pd" variable already has the PrintDocument events available to it. Also for the same reason, you dont need the remove handler lines there either.

Also when I'm building a print routine like this, I use a PrintPreviewDialog control instead of actually printing a ton of pages. The dialog control displays the page(s) exactly as it would on paper, saves time and money.
 
Many thanks for your help, and pointers as to what direction I should take.

You are right though - PrintPreview not only saves paper, but also energy - as the printer is a LONG walk down in the factory for me :)
 
come to think of it, this line might be causing you a problem:
pd.PrintController = New Printing.StandardPrintController()

I'm not 100% sure what it does.
 
Back
Top