PrintPreviewDialog在页面外运行。我正在使用带有PrintDocument和PrintPreviewDialog的VB.net 2012。我从数据库中获取数据。数据不会转到下一页。它似乎只是从底部跑出来。
'Step through each record
'Load into memory
sHeader = objDataView(nRec).Item("PmtType").ToString
'Print the header
e.Graphics.DrawString(sHeader, ReportBodyFont, Brushes.Black, a, n)
n = n + 15
For nRec = 0 To nRecordCount
'Load into Text box
'e = refers to print page arguments
If sHeader = objDataView(nRec).Item("PmtType").ToString Then
'I'm in the same category, print it
dDate = objDataView(nRec).Item("DatePd").ToString
sString = Format(dDate, "MM/dd/yy")
e.Graphics.DrawString(sString, ReportBodyFont, Brushes.Black, b, n)
e.Graphics.DrawString(objDataView(nRec).Item("PaidTo").ToString, ReportBodyFont, Brushes.Black, c, n)
sPmt = objDataView(nRec).Item("Payment").ToString
sPmt = FormatNumber(sPmt, 2)
e.Graphics.DrawString(sPmt, ReportBodyFont, Brushes.Black, d, n)
e.Graphics.DrawString(objDataView(nRec).Item("Comments").ToString, ReportBodyFont, Brushes.Black, ee, n)
n = n + iSpace
nSubtot = nSubtot + objDataView(nRec).Item("Payment").ToString
nGrandTot = nGrandTot + objDataView(nRec).Item("Payment")
Else
'I moved to the next category, skip a line and print a new category
'Print the sub total, reset to 0, print the next category
e.Graphics.DrawString("Sub Total:", ReportBodyFont, Brushes.Black, c, n)
nSubtot = FormatNumber(nSubtot, 2)
e.Graphics.DrawString(nSubtot, ReportBodyFont, Brushes.Black, d, n)
nSubtot = 0
n = n + 15
sHeader = objDataView(nRec).Item("PmtType").ToString
e.Graphics.DrawString(sHeader, ReportBodyFont, Brushes.Black, a, n)
n = n + 15
dDate = objDataView(nRec).Item("DatePd").ToString
sString = Format(dDate, "MM/dd/yy")
e.Graphics.DrawString(sString, ReportBodyFont, Brushes.Black, b, n)
e.Graphics.DrawString(objDataView(nRec).Item("PaidTo").ToString, ReportBodyFont, Brushes.Black, c, n)
sPmt = objDataView(nRec).Item("Payment").ToString
sPmt = FormatNumber(sPmt, 2)
e.Graphics.DrawString(sPmt, ReportBodyFont, Brushes.Black, d, n)
e.Graphics.DrawString(objDataView(nRec).Item("Comments").ToString, ReportBodyFont, Brushes.Black, ee, n)
n = n + iSpace
nSubtot = nSubtot + objDataView(nRec).Item("Payment").ToString
nGrandTot = nGrandTot + objDataView(nRec).Item("Payment").ToString
End If
'Test for page break
If nRec < nRecordCount Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
Next
答案 0 :(得分:1)
'Test for page break
这不会测试分页符。首先,您需要将nRec
变量移出方法,并使用BeginPrint事件在0处启动它:
Private nRec As Integer
Private Sub PrintDocument1_BeginPrint(sender As Object, e As Printing.PrintEventArgs)
nRec = 0
End Sub
只有在检查文本仍然适合页面后,您的PrintPage事件处理程序才会增加nRec
:
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs)
'' Print header
''...
While nRec < nRecordCount
'' Does it still fit on the page?
If n + iSpace > e.PageBounds.Bottom Then
e.HasMorePages = True
Return
End If
'' Print next record
nRec += 1
''...
End While
End Sub