繁体   English   中英

vb.NET:打印多个页面

[英]vb.NET: Printing Multiple Pages

我无法向打印机发送信号以开始新页面。 设置e.HasMorePages = True后,它将继续在第1页上打印。 使用我的字体,我可以干净地打印每页30行,并且仍然保持边框

    'print the text
    'create a string array from the listView equipment items
    Dim equip() As classReportObj
    ReDim equip(0 To lstReport.Items.Count - 1)
    For i = 0 To lstReport.Items.Count - 1
        equip(i) =
            New classReportObj() With {.name = lstReport.Items(i).Text, .type = lstReport.Items(i).SubItems(1).Text,
                                       .completeTests = lstReport.Items(i).SubItems(2).Text, .incompleteTests = lstReport.Items(i).SubItems(3).Text,
                                       .status = lstReport.Items(i).SubItems(4).Text}
    Next i

    'get the coordinates for the first row and the columns
    Dim y As Integer = e.MarginBounds.Top
    Dim x0 As Integer = e.MarginBounds.Left
    Dim x1 As Integer = x0 + 150
    Dim x2 As Integer = x1 + 150
    Dim x3 As Integer = x2 + 150
    Dim x4 As Integer = x3 + 120
    Dim headerFont As New Font("Times New Roman", 10)
    Dim maxLines As Integer 'maximum number of lines per page
    maxLines = 30 '30 lines per page, including headers
    Dim lineCount As Integer 'counts lines per printed page

    'make a new font to use
    Using theFont As New Font("Times New Roman", 10)
        'draw the column headers
        e.Graphics.DrawString("Project: " & project.name & " " & thisReportType & " Report, " & Now, theFont, Brushes.Black, x0, y)
        e.Graphics.DrawString("Name", headerFont, Brushes.Black, x0, y + 30)
        e.Graphics.DrawString("Type", headerFont, Brushes.Black, x1, y + 30)
        e.Graphics.DrawString("Complete Tests", headerFont, Brushes.Black, x2, y + 30)
        e.Graphics.DrawString("Incomplete Tests", headerFont, Brushes.Black, x3, y + 30)
        e.Graphics.DrawString("Status", headerFont, Brushes.Black, x4, y + 30)

        'mmove Y down for the next row
        y += 60

        Dim nameMax As Integer 'max characters for name
        Dim typeMax As Integer 'max characters for type
        'loop through each equipment to display the data
        For Each aEquip In equip

            'set the max character length for name and type
            If aEquip.name.Length < 23 Then
                nameMax = aEquip.name.Length
            Else
                nameMax = 23
            End If
            '
            If aEquip.type.Length < 23 Then
                typeMax = aEquip.type.Length
            Else
                typeMax = 23
            End If

            'display the equipment values
            e.Graphics.DrawString(aEquip.name.Substring(0, nameMax), theFont, Brushes.Black, x0, y)
            e.Graphics.DrawString(aEquip.type.Substring(0, typeMax), theFont, Brushes.Black, x1, y)
            e.Graphics.DrawString(aEquip.completeTests, theFont, Brushes.Black, x2, y)
            e.Graphics.DrawString(aEquip.incompleteTests, theFont, Brushes.Black, x3, y)
            e.Graphics.DrawString(aEquip.status, theFont, Brushes.Black, x4, y)
            'move Y down for the next row
            y += 30

            'increment the line counter for each piece of equipment
            lineCount = lineCount + 1
            'if we've reached the maximum number of lines per page
            If (lineCount Mod maxLines) = 0 Then
                'draw a box around it all
                e.Graphics.DrawRectangle(Pens.Black, x0, e.MarginBounds.Top + 30, x4 - x0 + 100, y - e.MarginBounds.Top - 30)
                e.HasMorePages = True 'But it doesn't start a new page, it continues printing page 1 unless I exit
            Else
                e.HasMorePages = False
            End If
        Next
    End Using

    'draw a box around it all
    e.Graphics.DrawRectangle(Pens.Black, x0, e.MarginBounds.Top + 30, x4 - x0 + 100, y - e.MarginBounds.Top - 30)
    'only printing one page (for now)
    e.HasMorePages = False
End Sub

您似乎不明白e.HasMorePages实际做什么。 它所做的只是导致PrintPage事件再次引发。 如果您所有的PrintPage事件处理程序所做的就是打印第一页,那就是您看到的一遍又一遍地打印了。

PrintPage事件处理程序应该打印一页。 通常在该事件处理程序中要做的第一件事就是确定需要打印的页面。 通常,这将要求您在事件处理程序之外保留某种计数。

该计数可能具体是已打印多少页,也可能是记录列表或其他内容的索引。 无论是什么,它都需要告诉您PrintPage事件处理程序内部人员如何获取需要在当前页面上打印的内容。

这是一个示例,该示例将在每个页面上打印五个String的列表:

Private lines As New List(Of String)
Private overallRecordIndex As Integer
Private pageNumber As Integer

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    overallRecordIndex = 0
    pageNumber = 1
    PrintDocument1.Print()
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim pageRecordIndex = 0
    Dim yOffset = 10

    Do While pageRecordIndex < 5 AndAlso overallRecordIndex < lines.Count
        e.Graphics.DrawString(String.Format("Page {0}, Record {1}: {2}",
                                            pageNumber,
                                            overallRecordIndex,
                                            lines(overallRecordIndex)),
                              Me.Font,
                              Brushes.Black,
                              10,
                              yOffset)

        overallRecordIndex += 1
        pageRecordIndex += 1
        yOffset += 20
    Loop

    e.HasMorePages = (overallRecordIndex < lines.Count)

    If e.HasMorePages Then
        pageNumber += 1
    End If
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM