How to Print Multiple Pages using PrintDocument

Posted on 2007-10-12
Last Modified: 2009-04-22
I have a program in which I want to print a postcard for each record in a datareader control.  Currently I am calling the PrintDocument.Print command after each record is read.  What I would like to do is to create a new "page" for each record and then only call the Print command once.  I've included some code below:

....while reader reads....
Dim custcitystatezip As String
                        Dim prn As New PrintDocument

                        custcitystatezip = StrConv(Trim(Reader("primcity")), VbStrConv.ProperCase)
                        custcitystatezip += ", " & Trim(Reader("primstate"))
                        custcitystatezip += "  " & Trim(Reader("primzipfirst5"))

                        PostcardAddress = custname & vbCrLf & custaddr & vbCrLf & custcitystatezip & vbCrLf & vbCrLf & vbCrLf & Reader("repairnum")

                        AddHandler prn.PrintPage, AddressOf Me.PrintPageHandler
                        Dim ps As System.Drawing.Printing.PaperSource
                        For Each ps In prn.PrinterSettings.PaperSources
                            If ps.SourceName = "Tray2" Then
                                prn.DefaultPageSettings.PaperSource = ps
                                Exit For
                            End If
                        prn.DefaultPageSettings.Landscape = True

                        RemoveHandler prn.PrintPage, AddressOf Me.PrintPageHandler
Question by:trbbhm
    LVL 27

    Accepted Solution

    Hi trbbhm,
    Here is a sample that I had, that shows how to print muiltible pages easy.

    'FORM 1

    Public Class frmPrintMultiSheets
        Inherits System.Windows.Forms.Form
        Private i As Short
        Private oPageFirst As Short
        Private oPageLast As Short
        Private bHit As Boolean
    #Region " Windows Form Designer generated code "

        Public Sub New()

            'This call is required by the Windows Form Designer.

            'Add any initialization after the InitializeComponent() call

        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                End If
            End If
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
        Private WithEvents docToPrint As New Printing.PrintDocument
        Dim PrintDialog1 As New PrintDialog

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents Button1 As System.Windows.Forms.Button
        Friend WithEvents LoanNumber As System.Windows.Forms.TextBox
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.Button1 = New System.Windows.Forms.Button
            Me.LoanNumber = New System.Windows.Forms.TextBox
            Me.Button1.Location = New System.Drawing.Point(184, 180)
            Me.Button1.Name = "Button1"
            Me.Button1.TabIndex = 0
            Me.Button1.Text = "Button1"
            Me.LoanNumber.Location = New System.Drawing.Point(12, 20)
            Me.LoanNumber.Name = "LoanNumber"
            Me.LoanNumber.TabIndex = 1
            Me.LoanNumber.Text = "123456"
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(292, 269)
            Me.Name = "frmPrintMultiSheets"
            Me.Text = "frmPrintMultiSheets"

        End Sub

    #End Region
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            PrintDialog1.AllowSomePages = True
            Dim printer As String

            PrintDialog1.ShowHelp = True
            PrintDialog1.Document = docToPrint

            If (PrintDialog1.ShowDialog() = DialogResult.OK) Then
                Select Case PrintDialog1.PrinterSettings.PrintRange
                    Case Printing.PrintRange.AllPages
                        oPageLast = 5
                        If PrintDialog1.PrinterSettings.FromPage = 0 Then
                            oPageFirst = 1
                        End If
                        docToPrint.PrinterSettings.ToPage = oPageLast
                    Case Printing.PrintRange.SomePages
                        If PrintDialog1.PrinterSettings.FromPage = 0 Then
                            oPageFirst = 1
                            oPageFirst = PrintDialog1.PrinterSettings.FromPage()
                        End If
                        oPageLast = PrintDialog1.PrinterSettings.ToPage()
                End Select
                'If PrintDialog1.PrinterSettings.PrintRange = Printing.PrintRange.AllPages Then
                '    oPageLast = PrintDialog1.PrinterSettings.ToPage()
                'End If
                printer = PrintDialog1.PrinterSettings.PrinterName
                For i = oPageFirst To oPageLast
            End If
        End Sub
        Dim DocLoanID() As String
        Private Sub document_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles docToPrint.PrintPage
            DocLoanID = Split(Me.LoanNumber.Text)
            Dim text As String = GetBusinessChannel()
            Dim LoanIDText As String = "Loan ID: "
            LoanIDText = LoanIDText & DocLoanID(0)
            Dim MortgageBarCode As String = "*MORTGAGE*"
            Dim LoanIDBarCode As String = "*" & DocLoanID(0) & "*"
            Dim DocumentType1 As String
            Dim DocumentTypeBarCode As String

            'Always page the first Page
            If oPageFirst = 1 Then
                '    DocumentType1 = "Document Type: LOAN APP"
                '    DocumentTypeBarCode = "*LOAN APP*"
                '    bHit = True
            End If
            Select Case i
                Case 1
                    DocumentType1 = "Document Type: LOAN APP"
                    DocumentTypeBarCode = "*LOAN APP*"
                Case 2
                    DocumentType1 = "Document Type: CREDIT APP"
                    DocumentTypeBarCode = "*CREDIT APP*"
                Case 3
                    DocumentType1 = "Document Type: CLOSING CONDITIONS"
                    DocumentTypeBarCode = "*CLOSING CONDITIONS*"
                Case 4
                    DocumentType1 = "Document Type: UW CONDITIONS"
                    DocumentTypeBarCode = "*UW CONDITIONS*"
                Case 5
                    DocumentType1 = "Document Type: TITLE COMMITMENT OR POLICY"
                    DocumentTypeBarCode = "*TITLE COMMITMENT OR POLICY*"
            End Select

            Dim printFont As New System.Drawing.Font("Times New Roman", 26, System.Drawing.FontStyle.Bold)
            e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, 250, 90)
            Dim printFont2 As New System.Drawing.Font("Times New Roman", 24, System.Drawing.FontStyle.Regular)
            e.Graphics.DrawString(LoanIDText, printFont2, System.Drawing.Brushes.Black, 15, 180)
            e.Graphics.DrawString(DocumentType1, printFont2, System.Drawing.Brushes.Black, 15, 220)
            Dim printFont3 As New System.Drawing.Font("Free 3 of 9 Extended", 48, System.Drawing.FontStyle.Regular)
            e.Graphics.DrawString(MortgageBarCode, printFont3, System.Drawing.Brushes.Black, 15, 360)
            e.Graphics.DrawString(LoanIDBarCode, printFont3, System.Drawing.Brushes.Black, 15, 560)
            e.Graphics.DrawString(DocumentTypeBarCode, printFont3, System.Drawing.Brushes.Black, 15, 760)
        End Sub

        Private Function GetBusinessChannel() 'FOR TESTING
            Static i As Short
            i += 1
            Return " Test Page " & i
        End Function

    End Class
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    This video discusses moving either the default database or any database to a new volume.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now