0

The problem I'm having is that while printing a multi-page document, each page will over-strike the other with the exception of the last page. It's almost as if it's not clearing the contents of the page before starting a new one.

I've reduced it to the smallest amount of code and I still can't get it to behave properly.

Here's what the PrintPage code looks like:

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 is a simple list of strings that contains the lines of the output - it's declared as "dim docRec as new List(of String)" and populated earlier.

I'm all out of guesses at this point and apparently my Google Fu has failed me because I can't find another instance of this issue anywhere.

On the off chance that the code that initiates the print is relevant, here it is in all it's hacked up glory:

    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

Thanks for your help!

g.

[edit] I modified the following code to add a stream of debug info to see how things were being executed.

        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

Here's the output from that:

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

The line count is smaller in this example and it generates two pages, the second of which over-writes the contents of the first.

geneb
  • 103
  • 3
  • I don't see the problem, maybe you hacked it up too much. This can happen when the PrintPage event handler runs more than once for every page. Which will happen when you call AddHandler too often. – Hans Passant Oct 07 '13 at 18:27
  • I'm only calling AddHandler a single time per execution. I could understand a possible issue if the first print worked and the second didn't, but that's not the case here. – geneb Oct 07 '13 at 18:48

2 Answers2

0
  1. Instead of lineIndex = 1, set to 0 (the list(of string) its zero-based.
  2. delete "Handles printDoc.PrintPage" or "AddHandler printDoc.PrintPage, AddressOf printDoc_printExceptionPage". This is a duplication.

hints:

  1. printFont Is set twice.
  2. you can delete the condition If lineIndex = docRec.Count, And instead add before "End Sub" (after "End While") "e.HasMorePages = False"

Item #2 was the key to the whole issue - I'd actually discovered that due to the comment Hans had made. Thanks!

g.

dovid
  • 6,354
  • 3
  • 33
  • 73
-1

When you execute PrintDocument.print() this event will re-execute the PrintDocument_PrintPage event, so you need to reset lineIndex = 0. The print event should look like this:

Private Sub ButtonPrint_Click(sender As Object, e As EventArgs) Handles ButtonPrint.Click

    'resetting counter
    lineindex = 0

    'Execute Print event
    PrintDocument.Print()
End Sub
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
Neil
  • 1