PrintPreviewDialog 在页面外运行

PrintPreviewDialog runs off page

PrintPreviewDialog 在页面外运行。我将 VB.net 2012 与 PrintDocument 和 PrintPreviewDialog 一起使用。我正在从数据库中获取数据。数据不会转到下一页。看起来好像刚好 运行 离底部。

    '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
    '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