PrintDocument覆盖了除最后一页之外的所有内容

时间:2013-10-07 18:06:57

标签: vb.net printdocument

我遇到的问题是,在打印多页文档时,除最后一页外,每个页面都会超过另一页。这几乎就像是在开始新页面之前没有清除页面内容。

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

这是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)”并在之前填充。

我在这一点上完全没有猜测,显然我的谷歌已经失败了,因为我无法在任何地方找到这个问题的另一个例子。

关于启动打印的代码是否相关的问题,这里的所有内容都被扼杀了荣耀:

    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

感谢您的帮助!

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

        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

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

1 个答案:

答案 0 :(得分:0)

  1. 而不是lineIndex = 1,设置为0(字符串列表)从零开始。
  2. delete“处理printDoc.PrintPage”或“AddHandler printDoc.PrintPage,AddressOf printDoc_printExceptionPage”。 这是一个重复。
  3. 提示:

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

    3. 项目#2是整个问题的关键 - 我实际上发现了由于Hans所做的评论。谢谢!