Board index » Visual Studio » Using printdocument - prints over itself with no consistency

Using printdocument - prints over itself with no consistency

Visual Studio244
Hey everyone, I'm trying to output a text file to a printer, and for

some reason, it prints over itself, something that should be 15 pages

can be from 4-8 pages, and it changes each time I try it. Something

is definitely wrong.

I checked the printpage event, it gets called all 15 times for the 15

pages of data, but the number of pages spooling doesnt change at the

same pace. I dont get it.

Below is my code, any ideas why this would happen?





streamtoprint = New IO.StreamReader("C:\temp.dat")

Try

printFont = New Font("Times New Roman", 10)

AddHandler PrintDocument1.PrintPage, AddressOf

Me.PrintDocument1_PrintPage

PrintDocument1.Print()

Finally

streamtoprint.Close()

IO.File.Delete("C:\temp.dat")

End Try

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try



Then the Printpage arguement



Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object,

ByVal ev As System.Drawing.Printing.PrintPageEventArgs) Handles

PrintDocument1.PrintPage

Dim linesPerPage As Single = 0

Dim yPos As Single = 0

Dim count As Integer = 0

Dim leftMargin As Single = ev.MarginBounds.Left

Dim topMargin As Single = ev.MarginBounds.Top

Dim line As String = Nothing



' Calculate the number of lines per page.

linesPerPage = ev.MarginBounds.Height /

printFont.GetHeight(ev.Graphics)



' Print each line of the file.

While count < linesPerPage

line = streamtoprint.ReadLine()

If line Is Nothing Then

Exit While

End If

yPos = topMargin + count *

printFont.GetHeight(ev.Graphics)

ev.Graphics.DrawString(line, printFont, Brushes.Black,

leftMargin, yPos, New StringFormat())

count += 1

End While



' If more lines exist, print another page.

If (line IsNot Nothing) Then

ev.HasMorePages = True

Else

ev.HasMorePages = False

End If

End Sub



I've been programming for over a decade, and this is the first time

I;ve ever been so stumped I had to post my code. Any help would be

truly appreciated.


-
 

Re:Using printdocument - prints over itself with no consistency

Hi,



either remove the line 'AddHandler PrintDocument1.PrintPage, AddressOf

Me.PrintDocument1_PrintPage'

or the 'handles' part of the 'Private Sub PrintDocument1_PrintPage and the

problem should go away.



HTH







<tghamm@gmail.com>wrote in message

Quote
Hey everyone, I'm trying to output a text file to a printer, and for

some reason, it prints over itself, something that should be 15 pages

can be from 4-8 pages, and it changes each time I try it. Something

is definitely wrong.

I checked the printpage event, it gets called all 15 times for the 15

pages of data, but the number of pages spooling doesnt change at the

same pace. I dont get it.

Below is my code, any ideas why this would happen?





streamtoprint = New IO.StreamReader("C:\temp.dat")

Try

printFont = New Font("Times New Roman", 10)

AddHandler PrintDocument1.PrintPage, AddressOf

Me.PrintDocument1_PrintPage

PrintDocument1.Print()

Finally

streamtoprint.Close()

IO.File.Delete("C:\temp.dat")

End Try

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try



Then the Printpage arguement



Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object,

ByVal ev As System.Drawing.Printing.PrintPageEventArgs) Handles

PrintDocument1.PrintPage

Dim linesPerPage As Single = 0

Dim yPos As Single = 0

Dim count As Integer = 0

Dim leftMargin As Single = ev.MarginBounds.Left

Dim topMargin As Single = ev.MarginBounds.Top

Dim line As String = Nothing



' Calculate the number of lines per page.

linesPerPage = ev.MarginBounds.Height /

printFont.GetHeight(ev.Graphics)



' Print each line of the file.

While count < linesPerPage

line = streamtoprint.ReadLine()

If line Is Nothing Then

Exit While

End If

yPos = topMargin + count *

printFont.GetHeight(ev.Graphics)

ev.Graphics.DrawString(line, printFont, Brushes.Black,

leftMargin, yPos, New StringFormat())

count += 1

End While



' If more lines exist, print another page.

If (line IsNot Nothing) Then

ev.HasMorePages = True

Else

ev.HasMorePages = False

End If

End Sub



I've been programming for over a decade, and this is the first time

I;ve ever been so stumped I had to post my code. Any help would be

truly appreciated.







-