【问题标题】:PrintDocument over-writes all but last pagePrintDocument 覆盖除最后一页以外的所有页面
【发布时间】:2013-10-07 18:06:57
【问题描述】:

我遇到的问题是,在打印多页文档时,除最后一页外,每一页都会覆盖另一页。就好像在开始新页面之前没有清除页面内容一样。

我已将它减少到最少的代码量,但我仍然无法让它正常运行。

PrintPage 代码如下所示:

Private Sub printDoc_printExceptionPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles printDoc.PrintPage
    Dim printFont As New Font("Courier New", 9, FontStyle.Regular)
    Dim lineHeight As Single = 0 
    Dim xpos As Single = e.MarginBounds.Left
    Dim ypos As Single = e.MarginBounds.Top

    If lineIndex = docRec.Count Then
        e.HasMorePages = False
        Exit Sub
    End If

    printFont = New Font("Courier New", 9, FontStyle.Regular)
    lineHeight = printFont.GetHeight(e.Graphics)

    While lineIndex < docRec.Count
        e.Graphics.DrawString(docRec.Item(lineIndex), printFont, Brushes.Black, xpos, ypos, New StringFormat())
        ypos += lineHeight
        lineIndex += 1
        If lineIndex Mod 35 = 0 Then
            pageCount += 1
            e.HasMorePages = True
            Exit Sub
        End If
    End While
End Sub

docRec 是一个简单的字符串列表,其中包含输出的行 - 它被声明为“dim docRec as new List(of String)”并在之前填充。

在这一点上我完全猜不透,显然我的 Google Fu 让我失望了,因为我在任何地方都找不到这个问题的另一个实例。

如果启动打印的代码是相关的,那么这就是它的全部荣耀:

    PrintDlg.AllowPrintToFile = False
    PrintDlg.PrinterSettings = New PrinterSettings
    Dim test As New PrintPreviewDialog
    printDoc = New PrintDocument
    printDoc.PrinterSettings.DefaultPageSettings.Landscape = True
    If PrintDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
        AddHandler printDoc.PrintPage, AddressOf printDoc_printExceptionPage
        printDoc.DefaultPageSettings.Landscape = True
        BuildDoc()
        lineIndex = 1
        pageCount = 1
        'printDoc.Print()
        test.Document = printDoc
        test.ShowDialog()
    End If

感谢您的帮助!

g.

[编辑] 我修改了以下代码以添加调试信息流,以查看事情是如何执行的。

        While lineIndex < docRec.Count
        Debug.Print("ypos (" + CStr(ypos) + ") line:" + CStr(lineIndex))
        e.Graphics.DrawString(docRec.Item(lineIndex), printFont, Brushes.Black, xpos, ypos, New StringFormat())
        ypos += printFont.GetHeight(e.Graphics)
        lineIndex += 1
        If lineIndex Mod 35 = 0 Then 'If lineTop + lineHeight > e.MarginBounds.Bottom Then
            Debug.Print("done with page " + CStr(pageCount))
            pageCount += 1
            e.HasMorePages = True
            Exit Sub
        End If

    End While

输出如下:

ypos (0) line:1
ypos (14.16015) line:2
ypos (28.32031) line:3
ypos (42.48046) line:4
ypos (56.64062) line:5
ypos (70.80077) line:6
ypos (84.96093) line:7
ypos (99.12109) line:8
ypos (113.2812) line:9
ypos (127.4414) line:10
ypos (141.6015) line:11
ypos (155.7617) line:12
ypos (169.9219) line:13
ypos (184.082) line:14
ypos (198.2422) line:15
ypos (212.4023) line:16
ypos (226.5625) line:17
ypos (240.7226) line:18
ypos (254.8828) line:19
ypos (269.0429) line:20
ypos (283.2031) line:21
ypos (297.3633) line:22
ypos (311.5234) line:23
ypos (325.6836) line:24
ypos (339.8437) line:25
ypos (354.0039) line:26
ypos (368.164) line:27
ypos (382.3242) line:28
ypos (396.4843) line:29
ypos (410.6445) line:30
ypos (424.8047) line:31
ypos (438.9648) line:32
ypos (453.125) line:33
ypos (467.2851) line:34
done with page 1
ypos (0) line:35
ypos (14.16015) line:36
ypos (28.32031) line:37
ypos (42.48046) line:38
ypos (56.64062) line:39
ypos (70.80077) line:40
ypos (84.96093) line:41
ypos (99.12109) line:42
ypos (113.2812) line:43
ypos (127.4414) line:44
ypos (141.6015) line:45
ypos (155.7617) line:46
ypos (169.9219) line:47
ypos (184.082) line:48
ypos (198.2422) line:49
ypos (212.4023) line:50
ypos (226.5625) line:51
ypos (240.7226) line:52
ypos (254.8828) line:53
ypos (269.0429) line:54
ypos (283.2031) line:55
ypos (297.3633) line:56
ypos (311.5234) line:57
ypos (325.6836) line:58
ypos (339.8437) line:59
ypos (354.0039) line:60
ypos (368.164) line:61
ypos (382.3242) line:62

此示例中的行数较少,它会生成两个页面,第二个页面会覆盖第一个页面的内容。

【问题讨论】:

  • 我看不出问题所在,可能是你搞砸了太多。当 PrintPage 事件处理程序对每个页面运行不止一次时,就会发生这种情况。当您过于频繁地调用 AddHandler 时会发生这种情况。
  • 每次执行时我只调用一次 AddHandler。如果第一次打印有效而第二次无效,我可以理解可能存在的问题,但这里不是这种情况。

标签: vb.net printdocument


【解决方案1】:
  1. 不是 lineIndex = 1,而是设置为 0(字符串列表),它从零开始。
  2. 删除“处理 printDoc.PrintPage”或“AddHandler printDoc.PrintPage, AddressOf printDoc_printExceptionPage”。 这是重复的。

提示:

  1. printFont 设置了两次。
  2. 您可以删除条件 If lineIndex = docRec.Count, 而是在“End Sub”之前(“End While”之后)添加“e.HasMorePages = False”

第 2 项是整个问题的关键——实际上,由于 Hans 的评论,我发现了这一点。谢谢!

g.

【讨论】:

    【解决方案2】:

    当你执行PrintDocument.print()这个事件会重新执行PrintDocument_PrintPage事件,所以你需要重置lineIndex = 0。打印事件应如下所示:

    Private Sub ButtonPrint_Click(sender As Object, e As EventArgs) Handles ButtonPrint.Click
    
        'resetting counter
        lineindex = 0
    
        'Execute Print event
        PrintDocument.Print()
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-21
      • 2023-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多