0

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

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

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

本例中的行数较少,它生成两页,其中第二页覆盖了第一页的内容。

4

2 回答 2

0
  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。

于 2013-10-07T18:54:46.817 回答
-1

当你执行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
于 2018-02-27T03:51:11.940 回答