Printing more than one page in VB.Net using PrintDocument object

G

Guest

Hello,

I'm trying to print more than one page using the PrintDocument object.
I set "e.HasMorePages = True" after completeing the first page in the
"PrintDocument1_PrintPage" subroutine, and then "exit sub". When
"PrintDocument1_PrintPage" is called for the second page, it skips to the
code to print the second page and then returns, setting "e.HasMorePages =
False".

Everything seems to work however, when I look at my printout the second page
overwrites the first page. How do I keep the second page from overwriting
the first page?
 
G

Guest

I'm trying to print more than one page using the PrintDocument object.
I set "e.HasMorePages = True" after completeing the first page in the
"PrintDocument1_PrintPage" subroutine, and then "exit sub". When
"PrintDocument1_PrintPage" is called for the second page, it skips to the
code to print the second page and then returns, setting "e.HasMorePages =
False".

Everything seems to work however, when I look at my printout the second page
overwrites the first page. How do I keep the second page from overwriting
the first page?

I don't know why you are having a problem, but I do know that I ran into
something fishy regarding e.HasMorePages. The documentation claims that it
is an ordinary boolean property, but I had problems when I accessed it more
than once during a PrintPage event. So, I now compute a local boolean
HasMorePages for my own purposes, and I guarantee one and only one execution
per event of
e.HasMorePages = HasMorePages
and no other accesses to e.HasMorePages. If you use e.HasMorePages more
widely than this, you may have the same kind of problem I did. FYI FW 1.1,
Windows XP, all SPs current.
 
G

Guest

Thanks for responding so quickly. I tried your suggestion but it didn't help.
I also noticed something else strange. My print routine is called more than
once even if I set "e.HasMorePages = false" before I exit after creating the
first page. I only access e.HasMorePages once in the routine. The second
page is still overwriting the first page.
 
G

Guest

I also found that VB.Net (VS.Net 2003, .Net Framework 1.1) HasMorePages is
not functioning as designed (or as documented). It appears that the
PrintPage event will cycle twice whether HasMorePages is set to "True" or
"False". In my case, when I had additional pages of information to print,
the page eject was not being sent to the printer until the second cycle.
This resulted in one page printing on-top of another page. After much hair
pulling, I determined that it was a bug and wrote code to work around this
bug.

Example:
' Dim a Class Variable such as:
Dim pageCycle as boolean = False

'in the PrintPage handler, include the following:
'***************************************
'This code is here to handle a bug in the PrintPage event that will
not
'eject a page until the event cycles through two event cycles when the
'System.Drawing.Printing.PrintPageEventArgs.HasMorePages property has
'been set to true.
If printcycle = True Then
printcycle = False
Exit Sub
End If
printcycle = True
'***************************************

To work around the "two Cycle" PrintPage event when HasMorePages is "False",
include code in your loop to exit the loop if the string buffer is nothing.

Example:
Dim line As String = Nothing

' Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics)

'Now iterate over the stream and printing out each line

While count < linesPerPage
line = stringToPrint.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

I hope Microsoft reads this disscussion forum because I do not know how to
report bug.

Hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top