如何使多页打印正常工作。

huangapple go评论64阅读模式
英文:

How can i get multible page print working

问题

如何在一张纸满了之后获得多张纸的打印。

当我打印时,我希望根据需要打印的数量来打印当前所需的页面,现在我让它计算打印页面(参见图像),直到我按取消,然后不再打印。

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

    Dim Head As Integer = 0
    Dim List As Integer

    Head += 30
    For List = 0 To ListView1.Items.Count - 1
    Next
    e.Graphics.DrawString("口袋书收藏", New Drawing.Font("Times New Roman", 20), Brushes.Black, 250, Head)
    Head += 40
    e.Graphics.DrawString("口袋系列名称", New Drawing.Font("Times New Roman", 10), Brushes.Black, 50, Head)
    e.Graphics.DrawString("单/双", New Drawing.Font("Times New Roman", 10), Brushes.Black, 100, Head)
    e.Graphics.DrawString("口袋编号", New Drawing.Font("Times New Roman", 10), Brushes.Black, 200, Head)
    e.Graphics.DrawString("年份", New Drawing.Font("Times New Roman", 10), Brushes.Black, 300, Head)
    e.Graphics.DrawString("购买日期", New Drawing.Font("Times New Roman", 10), Brushes.Black, 450, Head)
    e.Graphics.DrawString("成本", New Drawing.Font("Times New Roman", 10), Brushes.Black, 550, Head)
    e.Graphics.DrawString("拥有", New Drawing.Font("Times New Roman", 10), Brushes.Black, 650, Head)

    List += 60

    '多页打印
    If List > 1000 Then
        e.HasMorePages = True
    End If

    For Each Itm As ListViewItem In ListView1.Items
        e.Graphics.DrawString(Itm.Text, New Drawing.Font("Times New Roman", 10), Brushes.Black, 50, List)
        e.Graphics.DrawString(Itm.SubItems(1).Text, New Drawing.Font("Times New Roman", 10), Brushes.Black, 100, List)
        e.Graphics.DrawString(Itm.SubItems(2).Text, New Drawing.Font("Times New Roman", 10), Brushes.Black, 200, List)
        e.Graphics.DrawString(Itm.SubItems(3).Text, New Drawing.Font("Times New Roman", 10), Brushes.Black, 300, List)
        e.Graphics.DrawString(Itm.SubItems(4).Text, New Drawing.Font("Times New Roman", 10), Brushes.Black, 450, List)
        e.Graphics.DrawString(Itm.SubItems(5).Text, New Drawing.Font("Times New Roman", 10), Brushes.Black, 550, List)
        e.Graphics.DrawString(Itm.SubItems(6).Text, New Drawing.Font("Times New Roman", 10), Brushes.Black, 650, List)

        List += 30
    Next

    e.HasMorePages = True

End Sub
英文:

How can I get multiple paper prints when one paper is full.

When I print, I want it to print the current pages needed depending on how much it needs to print, right now I get it to count print pages (See image) until I press cancel and then nothing more.

如何使多页打印正常工作。

The code I use.

Private Sub    PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

    Dim Head As Integer = 0
    Dim List As Integer

    Head += 30
    For List = 0 To ListView1.Items.Count - 1
    Next
    e.Graphics.DrawString("Pocket bok collection", New Drawing.Font("Times new roman", 20), Brushes.Black, 250, Head)
    Head += 40
    e.Graphics.DrawString("Pocket Serie Name", New Drawing.Font("Times new roman", 10), Brushes.Black, 50, Head)
    e.Graphics.DrawString("Single/Double", New Drawing.Font("Times new roman", 10), Brushes.Black, 100, Head)
    e.Graphics.DrawString("Pocket Nr", New Drawing.Font("Times new roman", 10), Brushes.Black, 200, Head)
    e.Graphics.DrawString("Year", New Drawing.Font("Times new roman", 10), Brushes.Black, 300, Head)
    e.Graphics.DrawString("Bought Date", New Drawing.Font("Times new roman", 10), Brushes.Black, 450, Head)
    e.Graphics.DrawString("Cost", New Drawing.Font("Times new roman", 10), Brushes.Black, 550, Head)
    e.Graphics.DrawString("Own", New Drawing.Font("Times new roman", 10), Brushes.Black, 650, Head)

    List += 60

    'Multiple page print
    If List > 1000 Then
        e.HasMorePages = True

    End If

    For Each Itm As ListViewItem In ListView1.Items
        e.Graphics.DrawString(Itm.Text, New Drawing.Font("Times new roman", 10), Brushes.Black, 50, List)
        e.Graphics.DrawString(Itm.SubItems(1).Text, New Drawing.Font("Times new roman", 10), Brushes.Black, 100, List)
        e.Graphics.DrawString(Itm.SubItems(2).Text, New Drawing.Font("Times new roman", 10), Brushes.Black, 200, List)
        e.Graphics.DrawString(Itm.SubItems(3).Text, New Drawing.Font("Times new roman", 10), Brushes.Black, 300, List)
        e.Graphics.DrawString(Itm.SubItems(4).Text, New Drawing.Font("Times new roman", 10), Brushes.Black, 450, List)
        e.Graphics.DrawString(Itm.SubItems(5).Text, New Drawing.Font("Times new roman", 10), Brushes.Black, 550, List)
        e.Graphics.DrawString(Itm.SubItems(6).Text, New Drawing.Font("Times new roman", 10), Brushes.Black, 650, List)

        List += 30

    Next

    e.HasMorePages = True


End Sub

答案1

得分: 2

PrintPage事件处理程序中的代码仅用于打印一页。如果您想计算将有多少页,需要在BeginPrint事件中处理并进行计算。

PrintPage事件处理程序中,您不能使用For Each循环遍历列表,因为您并未打印整个列表。您应该使用一个For循环,从当前页面的第一项索引开始,直到当前页面的最后一项索引结束。另一种方法是使用SkipTake方法来首先获取一页的项目,然后在其上使用For Each循环。您需要在PrintPage事件处理程序之外跟踪要打印的页码,因为它必须在多次调用之间保持不变。例如:

Private Const itemsPerPage As Integer = 10

Private pageCount As Integer
Private lastPrintedPageNumber As Integer

Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
    Dim itemCount = ListView1.Items.Count

    '将总项目数除以每页项目数,然后四舍五入以获取页数。
    pageCount = Convert.ToInt32(Math.Ceiling(itemCount / itemsPerPage))

    '我们还没有打印任何页面。
    lastPrintedPageNumber = 0
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    '获取要在当前页面上打印的项目。
    '跳过已在前几页打印的项目,然后获取此页的项目。
    Dim items = ListView1.Items.Cast(Of ListViewItem)().Skip(lastPrintedPageNumber * itemsPerPage).Take(itemsPerPage)

    For Each item In items
        '在此处打印项目。
    Next

    '我们已经打印了一页。
    lastPrintedPageNumber += 1

    '仅当我们没有刚刚打印最后一页时才有更多页面要打印。
    e.HasMorePages = (lastPrintedPageNumber < pageCount)
End Sub

请注意,您传递给Take的值是要获取的最大项目数。它可以处理情况,即可用项目较少,因此此代码将处理最后一页部分填充的情况。

还要注意,此代码基本上假设至少有一页要打印。如果没有要打印的项目,pageCount将为0,但仍会输出一个空白页。在调用Print之前,您应该确认是否有要打印的数据。

英文:

The code that goes in the PrintPage event handler is for printing one page only. If you want to calculate how many pages there are going to be, handle the BeginPrint event and do that there.

You cannot use a For Each loop over a list in the PrintPage event handler because you're not printing the entire list. You should be using a For loop that starts at the first item index for the current page and ends at the last item index for the current page. An alternative might be to use the Skip and Take methods to get one page worth of items first, then use a For Each loop over that. You need to keep track of the page number to print OUTSIDE the PrintPage event handler, because it has to persist across multiple invocations. E.g.

Private Const itemsPerPage As Integer = 10

Private pageCount As Integer
Private lastPrintedPageNumber As Integer

Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
    Dim itemCount = ListView1.Items.Count

    &#39;Divide the total item count by the item count per page and round up to get the number of pages.
    pageCount = Convert.ToInt32(Math.Ceiling(itemCount / itemsPerPage))

    &#39;We have not printed any pages yet.
    lastPrintedPageNumber = 0
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    &#39;Get the items to print on the current page.
    &#39;Skip the items already printed on previous pages and take the items for this page.
    Dim items = ListView1.Items.Cast(Of ListViewItem)().Skip(lastPrintedPageNumber * itemsPerPage).Take(itemsPerPage)

    For Each item In items
        &#39;Print item here.
    Next

    &#39;We have printed one more page.
    lastPrintedPageNumber += 1

    &#39;There are more pages to print if and only if we didn&#39;t just print the last page.
    e.HasMorePages = (lastPrintedPageNumber &lt; pageCount)
End Sub

Note that the value you pass to Take is the maximum number of items to take. It can handle situations where there are fewer items available, so this code will handle a partially full last page.

Also note that this code basically assumes that there is at least one page to print. If there are no items to print, pageCount would be 0 but a single blank page would be output. You should probably confirm that there is data to print before calling Print.

huangapple
  • 本文由 发表于 2023年6月29日 04:09:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定