• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2254
  • Last Modified:

How to Print Multiple Pages using PrintDocument

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
                        Next
                        prn.DefaultPageSettings.Landscape = True

                        prn.Print()
                        RemoveHandler prn.PrintPage, AddressOf Me.PrintPageHandler
0
trbbhm
Asked:
trbbhm
1 Solution
 
planoczCommented:
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()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(184, 180)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'LoanNumber
        '
        Me.LoanNumber.Location = New System.Drawing.Point(12, 20)
        Me.LoanNumber.Name = "LoanNumber"
        Me.LoanNumber.TabIndex = 1
        Me.LoanNumber.Text = "123456"
        '
        'frmPrint
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 269)
        Me.Controls.Add(Me.LoanNumber)
        Me.Controls.Add(Me.Button1)
        Me.Name = "frmPrintMultiSheets"
        Me.Text = "frmPrintMultiSheets"
        Me.ResumeLayout(False)

    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
                    Else
                        oPageFirst = PrintDialog1.PrinterSettings.FromPage()
                    End If
                    oPageLast = PrintDialog1.PrinterSettings.ToPage()
            End Select
            'If PrintDialog1.PrinterSettings.PrintRange = Printing.PrintRange.AllPages Then
            'Else
            '    oPageLast = PrintDialog1.PrinterSettings.ToPage()
            'End If
            printer = PrintDialog1.PrinterSettings.PrinterName
            For i = oPageFirst To oPageLast
                docToPrint.Print()
            Next
        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
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now