Calling PrintDocument1.PrintPage procedure

webwired

Member
Joined
Feb 9, 2009
Messages
9
Programming Experience
1-3
Hi, I think I have my code all done except for being able to call the PrintPage procedure... I'm not sure what to put in for the "e" when calling the procedure...

Here's my code... Notice the procedure on the bottom that is trying to call the PrintPage procedure...
VB.NET:
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim fileStreamReader As System.IO.StreamReader = New System.IO.StreamReader(OpenFileDialog1.FileName)
        If Not fileStreamReader Is Nothing Then
            Do Until fileStreamReader.EndOfStream
                Dim certificateHeadingFont = New Font("Times New Roman", 43, FontStyle.Regular)
                Dim receiverAndCompanyFont = New Font("Times New Roman", 18, FontStyle.Bold Or FontStyle.Italic)
                Dim nameAndCompanyLabelFont = New Font("Times New Roman", 14, FontStyle.Bold)
                Dim DateAndInstructorAndCompany2Font = New Font("Times New Roman", 14, FontStyle.Bold)
                Dim hasReceivedInstructionLabelFont = New Font("Times New Roman", 18, FontStyle.Bold)
                Dim cfrFont = New Font("Times New Roman", 22, FontStyle.Bold)
                Dim dateAndInstructorAndCompany2LabelFont = New Font("Times New Roman", 12, FontStyle.Regular)
                Dim noticeFont = New Font("Times New Roman", 10, FontStyle.Bold)

                PrintDocument1.DefaultPageSettings.Landscape = True

                ' Create outer border
                Dim blackPen As New Pen(Color.Black, 4)

                Dim point1 As New Point(40, 75)
                Dim point2 As New Point(1041, 75)
                Dim point3 As New Point(1041, 700)
                Dim point4 As New Point(40, 700)

                e.Graphics.DrawLine(blackPen, point1, point2)
                e.Graphics.DrawLine(blackPen, point2, point3)
                e.Graphics.DrawLine(blackPen, point3, point4)
                e.Graphics.DrawLine(blackPen, point4, point1)

                ' Create inner border
                Dim blackPen2 As New Pen(Color.Black, 2)

                Dim point5 As New Point(45, 80)
                Dim point6 As New Point(1036, 80)
                Dim point7 As New Point(1036, 695)
                Dim point8 As New Point(45, 695)

                e.Graphics.DrawLine(blackPen2, point5, point6)
                e.Graphics.DrawLine(blackPen2, point6, point7)
                e.Graphics.DrawLine(blackPen2, point7, point8)
                e.Graphics.DrawLine(blackPen2, point8, point5)

                Dim x As Integer = 545
                Dim y As Integer = 90

                Using string_format As New StringFormat()
                    string_format.Alignment = StringAlignment.Center
                    string_format.LineAlignment = StringAlignment.Near
                    e.Graphics.DrawString("CERTIFICATE OF ATTENDANCE", certificateHeadingFont, Brushes.Black, x, y, string_format)
                    e.Graphics.DrawString(fileStreamReader.ReadLine(), receiverAndCompanyFont, Brushes.Black, x, y + 120, string_format)
                    e.Graphics.DrawString("NAME", nameAndCompanyLabelFont, Brushes.Black, x, y + 150, string_format)
                    e.Graphics.DrawString(companyTextBox.Text, receiverAndCompanyFont, Brushes.Black, x, y + 200, string_format)
                    e.Graphics.DrawString("COMPANY", nameAndCompanyLabelFont, Brushes.Black, x, y + 230, string_format)
                    e.Graphics.DrawString("has received instruction in the requirements of", hasReceivedInstructionLabelFont, Brushes.Black, x, y + 300, string_format)
                    e.Graphics.DrawString(cfrTextBox.Text, cfrFont, Brushes.Black, x, y + 370, string_format)
                    e.Graphics.DrawString(dateTextBox.Text, DateAndInstructorAndCompany2Font, Brushes.Black, 200, y + 490, string_format)
                    e.Graphics.DrawString("Date", dateAndInstructorAndCompany2LabelFont, Brushes.Black, 200, y + 515, string_format)
                    e.Graphics.DrawString(instructorTextBox.Text, DateAndInstructorAndCompany2Font, Brushes.Black, 440, y + 490, string_format)
                    e.Graphics.DrawString("Instructor", dateAndInstructorAndCompany2LabelFont, Brushes.Black, 440, y + 515, string_format)
                    e.Graphics.DrawString(company2TextBox.Text, DateAndInstructorAndCompany2Font, Brushes.Black, 740, y + 490, string_format)
                    e.Graphics.DrawString("Company", dateAndInstructorAndCompany2LabelFont, Brushes.Black, 740, y + 515, string_format)
                    e.Graphics.DrawString(extraInfoTextBox.Text, noticeFont, Brushes.Black, x, y + 589, string_format)
                End Using

                ' Create line under recipient's name
                Dim point9 As New Point(245, 240)
                Dim point10 As New Point(845, 240)

                e.Graphics.DrawLine(blackPen2, point9, point10)

                ' Create line under company name
                Dim point11 As New Point(245, 320)
                Dim point12 As New Point(845, 320)

                e.Graphics.DrawLine(blackPen2, point11, point12)

                ' Create line under date
                Dim point13 As New Point(117, 605)
                Dim point14 As New Point(281, 605)

                e.Graphics.DrawLine(blackPen2, point13, point14)

                ' Create line under instructor name
                Dim point15 As New Point(380, 605)
                Dim point16 As New Point(580, 605)

                e.Graphics.DrawLine(blackPen2, point15, point16)

                ' Create line under Company name 2
                Dim point17 As New Point(580, 605)
                Dim point18 As New Point(950, 605)

                e.Graphics.DrawLine(blackPen2, point17, point18)
            Loop
        End If
    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            PrintDocument1_PrintPage(sender, e)
        End If
    End Sub
 
VB.NET:
PrintDocument1.Print()
PrintDocument Class (System.Drawing.Printing)

You never call event handlers directly, you do something that cause the event to be raised. There might be a chain of events raised for particular actions. Only the class that declared the event can raise it and you should not try to interfere with that. If you need to call the "same code" as in a event handler from other places the natural thing to do is place that code in a method of its own, then call that method both from event handler and any other places you need to call it.
 
VB.NET:
PrintDocument1.Print()
PrintDocument Class (System.Drawing.Printing)

You never call event handlers directly, you do something that cause the event to be raised. There might be a chain of events raised for particular actions. Only the class that declared the event can raise it and you should not try to interfere with that. If you need to call the "same code" as in a event handler from other places the natural thing to do is place that code in a method of its own, then call that method both from event handler and any other places you need to call it.

Thank you very much for that... Lessoned learned... It works great...
 
Well, I kind of spoke a little too soon about it working great... when I try to open a text file with a list of names, it just locks up with no errors... I've been over it and over it and just don't see anything wrong with it... As soon as you click "Open" on the OpenFileDialog form... I tried stepping through it, but it won't let me do that either when it comes opening the file...

Edit: I've attached the project file, its really small if anyone would like to take a look...
 

Attachments

  • CertificationsCreator.zip
    19.4 KB · Views: 21
Last edited by a moderator:
Private Sub PrintDocument1_PrintPage(...
Dim certificateHeadingFont = New Font("Times New Roman", 43, FontStyle.Regular)
Dim receiverAndCompanyFont = New Font("Times New Roman", 18, FontStyle.Bold Or FontStyle.Italic)
Dim nameAndCompanyLabelFont = New Font("Times New Roman", 14, FontStyle.Bold)
Dim DateAndInstructorAndCompany2Font = New Font("Times New Roman", 14, FontStyle.Bold)
Dim hasReceivedInstructionLabelFont = New Font("Times New Roman", 18, FontStyle.Bold)
Dim cfrFont = New Font("Times New Roman", 22, FontStyle.Bold)
Dim dateAndInstructorAndCompany2LabelFont = New Font("Times New Roman", 12, FontStyle.Regular)
Dim noticeFont = New Font("Times New Roman", 10, FontStyle.Bold)

PrintDocument1.DefaultPageSettings.Landscape = True

' Create outer border
Dim blackPen As New Pen(Color.Black, 4)
You have to set the page settings before you print/preview. All Pen and Font objects you create in code you have to Dispose with when done using them. For example blackPen.Dispose().
PrintDialog1.Document = PrintDocument1
If you're not creating multiple instances in code just select the document in Designer properties.

Notice that the job of PrintPage handler is to print a single page, if you want to print more pages you have to keep track of your data externally to that method and set e.HasMorePages. There are many ways to manage this, the essence of one of them is presented in this sample that reads one line for a file for each printed page, here the external reference is simply the StreamReader instance:
VB.NET:
Private Sub SelectFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectFileToolStripMenuItem.Click
    If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
        Me.PrintPreviewDialog1.ShowDialog()
    End If
End Sub

Private reader As IO.StreamReader 

Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
    reader = New IO.StreamReader(Me.OpenFileDialog1.FileName)
End Sub

Private Sub PrintDocument1_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint
    reader.Close()
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) 'Handles PrintDocument1.PrintPage
    '...other code
    e.Graphics.DrawString(reader.ReadLine(), receiverAndCompanyFont, Brushes.Black, x, y + 120, string_format)
    '...other code
    e.HasMorePages = Not reader.EndOfStream
End Sub
 
Well, I think I made the appropriate changes according to your instructions, but it still locks up and doesn't do anything... I'm getting this error now, "Object reference not set to an instance of an object." but it's not pointing to what part of code as it would usually do...

I did set "PrintDialog1.Document = PrintDocument1" and "PrintPreviewDialog1.Document = PrintDocument1" in the designer... The PrintPreviewDialog1 is just for testing purposes, instead of wasting a bunch of paper... I'll remove it and go straight to PrintDialog1 upon completion... Here's the revised code...

VB.NET:
Option Strict On
Option Explicit On

Public Class MainForm

    Private reader As IO.StreamReader

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        companyTextBox.Text = "Thomas Industrial Coatings, Inc."
        company2TextBox.Text = "Thomas Industrial Coatings, Inc."
        instructorTextBox.Text = "Wayne Long"
        dateTextBox.Text = "December 29, 2008"
        cfrTextBox.Text = "OSHA 29 CFR 1926 Subpart M, Fall Protection 500-503."
        receiverNameTextBox.Text = "Michael K. Conklin"
        extraInfoTextBox.Text = "TIC OSHA CONFINED SPACE  STANDARDS and CD TRAINING MATERIAL, © MARCH, 2005"
    End Sub

    'Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
    '    PrintDialog2.ShowDialog()
    'End Sub

    Private Sub SelectFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectFileToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            'PrintDialog1.ShowDialog()
            PrintPreviewDialog1.ShowDialog()
        End If
    End Sub

    Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
        reader = New IO.StreamReader(Me.OpenFileDialog1.FileName)
    End Sub

    Private Sub PrintDocument1_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint
        reader.Close()
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) 'Handles PrintDocument1.PrintPage

        Dim certificateHeadingFont = New Font("Times New Roman", 43, FontStyle.Regular)
        Dim receiverAndCompanyFont = New Font("Times New Roman", 18, FontStyle.Bold Or FontStyle.Italic)
        Dim nameAndCompanyLabelFont = New Font("Times New Roman", 14, FontStyle.Bold)
        Dim DateAndInstructorAndCompany2Font = New Font("Times New Roman", 14, FontStyle.Bold)
        Dim hasReceivedInstructionLabelFont = New Font("Times New Roman", 18, FontStyle.Bold)
        Dim cfrFont = New Font("Times New Roman", 22, FontStyle.Bold)
        Dim dateAndInstructorAndCompany2LabelFont = New Font("Times New Roman", 12, FontStyle.Regular)
        Dim noticeFont = New Font("Times New Roman", 10, FontStyle.Bold)

        PrintDocument1.DefaultPageSettings.Landscape = True

        Dim blackPen As New Pen(Color.Black, 4)
        Dim blackPen2 As New Pen(Color.Black, 2)

        Dim point1 As New Point(40, 75)
        Dim point2 As New Point(1041, 75)
        Dim point3 As New Point(1041, 700)
        Dim point4 As New Point(40, 700)
        Dim point5 As New Point(45, 80)
        Dim point6 As New Point(1036, 80)
        Dim point7 As New Point(1036, 695)
        Dim point8 As New Point(45, 695)
        Dim point9 As New Point(245, 240)
        Dim point10 As New Point(845, 240)
        Dim point11 As New Point(245, 320)
        Dim point12 As New Point(845, 320)
        Dim point13 As New Point(117, 605)
        Dim point14 As New Point(281, 605)
        Dim point15 As New Point(380, 605)
        Dim point16 As New Point(580, 605)
        Dim point17 As New Point(580, 605)
        Dim point18 As New Point(950, 605)

        Dim x As Integer = 545
        Dim y As Integer = 90

        While e.HasMorePages = Not reader.EndOfStream

            e.Graphics.DrawLine(blackPen, point1, point2)
            e.Graphics.DrawLine(blackPen, point2, point3)
            e.Graphics.DrawLine(blackPen, point3, point4)
            e.Graphics.DrawLine(blackPen, point4, point1)

            e.Graphics.DrawLine(blackPen2, point5, point6)
            e.Graphics.DrawLine(blackPen2, point6, point7)
            e.Graphics.DrawLine(blackPen2, point7, point8)
            e.Graphics.DrawLine(blackPen2, point8, point5)

            Using string_format As New StringFormat()
                string_format.Alignment = StringAlignment.Center
                string_format.LineAlignment = StringAlignment.Near
                e.Graphics.DrawString("CERTIFICATE OF ATTENDANCE", certificateHeadingFont, Brushes.Black, x, y, string_format)
                e.Graphics.DrawString(reader.ReadLine(), receiverAndCompanyFont, Brushes.Black, x, y + 120, string_format)
                e.Graphics.DrawString("NAME", nameAndCompanyLabelFont, Brushes.Black, x, y + 150, string_format)
                e.Graphics.DrawString(companyTextBox.Text, receiverAndCompanyFont, Brushes.Black, x, y + 200, string_format)
                e.Graphics.DrawString("COMPANY", nameAndCompanyLabelFont, Brushes.Black, x, y + 230, string_format)
                e.Graphics.DrawString("has received instruction in the requirements of", hasReceivedInstructionLabelFont, Brushes.Black, x, y + 300, string_format)
                e.Graphics.DrawString(cfrTextBox.Text, cfrFont, Brushes.Black, x, y + 370, string_format)
                e.Graphics.DrawString(dateTextBox.Text, DateAndInstructorAndCompany2Font, Brushes.Black, 200, y + 490, string_format)
                e.Graphics.DrawString("Date", dateAndInstructorAndCompany2LabelFont, Brushes.Black, 200, y + 515, string_format)
                e.Graphics.DrawString(instructorTextBox.Text, DateAndInstructorAndCompany2Font, Brushes.Black, 440, y + 490, string_format)
                e.Graphics.DrawString("Instructor", dateAndInstructorAndCompany2LabelFont, Brushes.Black, 440, y + 515, string_format)
                e.Graphics.DrawString(company2TextBox.Text, DateAndInstructorAndCompany2Font, Brushes.Black, 740, y + 490, string_format)
                e.Graphics.DrawString("Company", dateAndInstructorAndCompany2LabelFont, Brushes.Black, 740, y + 515, string_format)
                e.Graphics.DrawString(extraInfoTextBox.Text, noticeFont, Brushes.Black, x, y + 589, string_format)
            End Using

            e.Graphics.DrawLine(blackPen2, point9, point10)
            e.Graphics.DrawLine(blackPen2, point11, point12)
            e.Graphics.DrawLine(blackPen2, point13, point14)
            e.Graphics.DrawLine(blackPen2, point15, point16)

        End While

        reader.Close()
        blackPen.Dispose()
        blackPen2.Dispose()

    End Sub
 
Back
Top