如何在“打印预览”对话框中显示多个页面?

时间:2019-12-02 08:21:55

标签: vb.net printing

我只想问一下我的代码有什么问题?它不在下一页进行,而是在当前页中重叠。

Private Sub PD_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PD.PrintPage
    Try
        Dim font As New Font("Times New Roman", 12, FontStyle.Regular)
        Dim font2 As New Font("Times New Roman", 14, FontStyle.Bold)
        Dim count As Integer
        count = dgDailyReport.RowCount - 1
        Dim i As Integer = 0
        Dim Date_Saved As String
        Dim Total_Income As String
        Dim Location As Integer = 50
        Dim Location2 As Integer = 180
        Dim Location3 As Integer = 250

        While i <= count
            Date_Saved = dgDailyReport.Item("Date", i).Value.ToString
            Total_Income = dgDailyReport.Item("Total_Income", i).Value.ToString

            e.Graphics.DrawString("BPC-FASA", font, Brushes.Black, 50, 50)
            e.Graphics.DrawString("Bulihan City of Malolos, Bulacan", font, Brushes.Black, 50,70)
            e.Graphics.DrawString("CONTRIBUTION DAILY REPORT", font2, Brushes.Black, 50, 90)
            e.Graphics.DrawString("Date", font, Brushes.Black, 50, 150)
            e.Graphics.DrawString(Date_Saved, font, Brushes.Black, Location, Location2)
            e.Graphics.DrawString("Total Income", font, Brushes.Black, 250, 150)
            e.Graphics.DrawString("₱ " + Total_Income, font, Brushes.Black, Location3, Location2)
            i += 1
            Location2 += 25

            If Location2 >= 1100 Then
                Location = 180
                e.HasMorePages = True
            Else
                e.HasMorePages = False
            End If
        End While
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

这是“打印”按钮

 Private Sub cmdPrint_Click_1(sender As Object, e As EventArgs) Handles cmdPrint.Click
      Dim csz As New System.Drawing.Printing.PaperSize("Legal", 850, 1400)
      PD.PrinterSettings.DefaultPageSettings.PaperSize = csz
      PPD.ShowDialog()
 End Sub

1 个答案:

答案 0 :(得分:0)

考虑一下。您在该代码中的哪个位置退出该While循环?除了到达记录的末尾,无处不在。当您到达记录的末尾或页面的末尾时,您需要退出循环。此时,您需要确定是否要打印更多页面并设置HasMorePages。如果还有更多页面,则还需要记住您要完成的记录,因此您可以从那里开始,而不必从头开始。 PrintPage事件处理程序用于打印一页,因此您需要:

  1. 确定要转到哪个页面。
  2. 打印该页面的数据。
  3. 确定是否有更多页面要打印。