【问题标题】:vb.NET: Printing Multiple Pagesvb.NET:打印多页
【发布时间】:2015-11-10 02:58:33
【问题描述】:

我未能向我的打印机发出开始新页面的信号。在我设置 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

【问题讨论】:

    标签: vb.net printing


    【解决方案1】:

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

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

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

    以下示例将打印Strings 的列表,每页五个:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多