使用PrintDocument和HasMorePages打印多个页面 [英] Printing multiple pages using PrintDocument and HasMorePages

查看:179
本文介绍了使用PrintDocument和HasMorePages打印多个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在列表框中打印项目列表.我有284件物品.其中大约四分之一被打印,其余部分不打印,最后一行的输入是截止值的一半.我在网上阅读有关使用e.HasMorePages跟踪您离开的位置并打印到下一页的信息,但现在没有任何打印,它只是说其打印页1,2,3,4,5 .... etc.并没有任何反应.我必须按ctrl + c并关闭程序.如何获得所需的打印输出?

I'm trying to print a list of items in a listbox. I have 284 items. About a quarter of them get printed and the rest don't print and at the bottom the last entry is half way cutoff. I read online about keeping track of where you left off and printing to the next page by utilizing e.HasMorePages but nothing prints now and it just says its print page 1,2,3,4,5....etc. and nothing happens. I have to ctrl+c it and close program. How can I achieve desired print out?

Private Sub Print_Click(sender As Object, e As EventArgs) Handles Print.Click
  Dim PrintDialog1 As New PrintDialog
  Dim result As DialogResult = PrintDialog1.ShowDialog()
  If result = DialogResult.OK Then PrintDocument1.Print()

  ' PrintPreviewDialog1.Document = PrintDocument1
  ' PrintPreviewDialog1.ShowDialog()
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
  '  e.HasMorePages = True
  Dim itemCount As Integer
  Dim startX As Integer = 10
  Dim startY As Integer = 10
  Dim n As Integer
  For x As Integer = 0 To SoftwareLBox.Items.Count - 1
    e.Graphics.DrawString(SoftwareLBox.Items(x).ToString, SoftwareLBox.Font, Brushes.Black, startX, startY)
    startY += SoftwareLBox.ItemHeight
    If n = 150 Then
      e.HasMorePages = True
      n = 0
      startY = 10
    End If
    startY += e.PageBounds.Height
    n += 1
  Next
End Sub

推荐答案

您编写代码的方式告诉我,您认为PrintPage方法仅被调用一次,并且您正在使用该调用来打印所有内容.那不是它的工作方式.

The way you wrote your code tells me you think the PrintPage method is only getting called once, and that you are using that one call to print everything. That's not the way it works.

当需要打印新页面时,它将再次调用PrintPage方法,因此循环变量必须在PrintPage范围之外.打印下一页时,您需要知道当前正在打印的行号.

When a new page needs to be printed, it will call the PrintPage method again, so your loop variable has to be outside the PrintPage scope. When the next page prints, you need to know what line number you are currently printing.

尝试这样:

Private printLine As Integer = 0

Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs)
  Dim startX As Integer = e.MarginBounds.Left
  Dim startY As Integer = e.MarginBounds.Top
  Do While printLine < SoftwareLBox.Items.Count
    If startY + SoftwareLBox.ItemHeight > e.MarginBounds.Bottom Then
      e.HasMorePages = True
      Exit Do
    End If
    e.Graphics.DrawString(SoftwareLBox.Items(printLine).ToString, SoftwareLBox.Font, _
                          Brushes.Black, startX, startY)
    startY += SoftwareLBox.ItemHeight
    printLine += 1
  Loop
End Sub

在打印之前将printLine变量设置为零,或者在BeginPrint事件中将其设置为零.

Set the printLine variable to zero before you print, or set it to zero in the BeginPrint event.

这篇关于使用PrintDocument和HasMorePages打印多个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆