我很难在打印文档上打印多个页面。即使我在代码中设置了新页面,它仍会在第一页上打印所有内容。请帮忙。这是我的代码:`
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim yTop As Single = e.MarginBounds.Top
Dim leftmargin = e.MarginBounds.Left
Dim MyPen As New Pen(Color.Black, 1)
Dim regFont = New Font("Arial", 10)
Dim smallFont = New Font("Arial", 8)
Dim xsFont = New Font("Arial", 6)
Dim micrFont = New Font("MICR", 10)
Dim myprintfont = New Font("Arial", 12)
Dim Bigfont = New Font("Arial", 14, FontStyle.Bold)
Dim BldFnt = New Font("Arial", 9, FontStyle.Bold)
Dim fCtr As New StringFormat
fCtr.Alignment = StringAlignment.Center
Dim frt As New StringFormat
frt.Alignment = StringAlignment.Near
Dim flft As New StringFormat
flft.Alignment = StringAlignment.Far
'Dim mrc As String = ""
Dim lpos As Integer = My.Settings.ChkLeft
Dim blackPen As New Pen(Color.Black, 1)
Dim y As Integer = 0
Dim mrc As String = ""
Dim p As Integer = 0
Dim j As Integer = 0
Dim ct As Integer = dgChecks.SelectedRows.Count
Static totaChecksPrinted As Integer
If totaChecksPrinted < dgChecks.SelectedRows.Count - 1 Then
For Each row As DataGridViewRow In dgChecks.SelectedRows
st = row.Cells("CheckNumber").Value
Dim strSQL1 As String
strSQL1 = "SELECT * FROM QryCheckPrint WHERE CheckNumber In( " & st & ")"
If HQCon.State = ConnectionState.Closed Then HQCon.Open()
Dim ccmda As New Data.SqlClient.SqlCommand(strSQL1, HQCon)
Dim drc2 As Data.SqlClient.SqlDataReader
Dim HasRows As Boolean = False
drc2 = ccmda.ExecuteReader
If drc2.HasRows Then
While drc2.Read
y += My.Settings.ChkTop
e.Graphics.DrawString(drc2.Item("CorpName"), Bigfont, Brushes.Black, lpos, y, frt)
e.Graphics.DrawString(drc2.Item("CheckNumber"), Bigfont, Brushes.Black, lpos + 640, y + 1, frt)
y += 40
e.Graphics.DrawString(drc2.Item("Date"), smallFont, Brushes.Black, lpos + 640, y + 6, frt)
e.Graphics.DrawString("Date", myprintfont, Brushes.Black, lpos + 580, y + 5, frt)
e.Graphics.DrawLine(blackPen, lpos + 620, y + 25, lpos + 730, y + 25)
y += 60
e.Graphics.DrawString("Pay to the", smallFont, Brushes.Black, lpos, y, frt)
y += 15
e.Graphics.DrawString("order of", smallFont, Brushes.Black, lpos + 10, y, frt)
e.Graphics.DrawString(drc2.Item("Name"), myprintfont, Brushes.Black, lpos + 90, y - 10, frt)
If Not IsDBNull(drc2.Item("Amount")) Then
e.Graphics.DrawString("$" & Format(drc2.Item("Amount"), "n2"), myprintfont, Brushes.Black, lpos + 640, y - 5, frt)
End If
y += 15
e.Graphics.DrawLine(blackPen, lpos + 50, y, lpos + 640, y)
e.Graphics.DrawLine(blackPen, lpos + 50, y + 30, lpos + 640, y + 30)
e.Graphics.DrawLine(blackPen, lpos + 640, y - 25, lpos + 640, y)
e.Graphics.DrawString("**" & Functions.AmountInWords(drc2.Item("Amount")) & "**", regFont, Brushes.Black, lpos + 100, y + 10, frt)
y += 40
e.Graphics.DrawString(drc2.Item("BankName"), smallFont, Brushes.Black, lpos, y, frt)
y += 15
e.Graphics.DrawString(drc2.Item("BankAddress"), smallFont, Brushes.Black, lpos, y, frt)
y += 15
e.Graphics.DrawString(drc2.Item("City") & " " & drc2.Item("State") & ", " & drc2.Item("Zip"), smallFont, Brushes.Black, lpos, y, frt)
mrc = "o" & drc2.Item("CheckNumber") & "o T" & drc2.Item("ABA") & "T" & drc2.Item("AccountNumber") & "o"
y += 30
e.Graphics.DrawString(drc2.Item("Description"), smallFont, Brushes.Black, lpos + 80, y, frt)
e.Graphics.DrawString("For", regFont, Brushes.Black, lpos + 30, y, frt)
e.Graphics.DrawString("By", regFont, Brushes.Black, lpos + 480, y, frt)
e.Graphics.DrawString(drc2.Item("StoreCode"), xsFont, Brushes.Black, lpos + 400, y, frt)
e.Graphics.DrawLine(blackPen, lpos + 30, y + 20, lpos + 440, y + 20)
e.Graphics.DrawLine(blackPen, lpos + 500, y + 20, lpos + 720, y + 20)
y = y + My.Settings.MicrPos
e.Graphics.DrawString(mrc, micrFont, Brushes.Black, lpos + 100, y, frt)
y = y + My.Settings.ChkBottom
j += 1
totaChecksPrinted += 1
End While
End If
drc2.Close()
ccmda = Nothing
If (y > e.MarginBounds.Bottom) Then 'Print new page
e.HasMorePages = True
y = 0
End If
Next
End If
If HQCon.State = ConnectionState.Open Then HQCon.Close()
End Sub`
答案 0 :(得分:1)
每次引发PrintPage
事件时,您都应该只打印该页面上的数据。如果您要打印更多数据,请将HasMorePages
设置为True,然后设置为Exit Sub
。然后将再次为下一页调用PrintPage
方法。您需要以某种方式跟踪页面之间已打印的内容。像这样:
'Class level variable perhaps
Dim pageNumberToPrint As Integer = 1
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'Print the data that goes on the current page (pageNumberToPrint)
If pageNumberToPrint = 1 Then
'Print page number 1
Else
If pageNumberToPrint = 2 Then
'Print page number 2
Else
'Print page number 3
End If
End If
'If you're at the end of the page, and still have more data to print
If (y > e.MarginBounds.Bottom) Then 'Print new page
e.HasMorePages = True
y = 0
pageNumberToPrint += 1
Exit Sub 'The PrintPage event handler will be raised again
End If
End Sub