Printing

mikef74

Member
Joined
Jul 30, 2003
Messages
9
Im new to vb.net, and Im trying to print from a form that was filled out. Ive got it to work, but Im not sure how to start a new page. This is the code I have so far. Any help would be appreciated. Thanks.

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(Title.Text, New Font("Arial", 18, FontStyle.Bold), Brushes.Black, 350, 100)
e.Graphics.DrawString(Address1.Text, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 350, 130)
If Address2.Text <> "" Then
e.Graphics.DrawString(Address2.Text, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 350, 150)
e.Graphics.DrawString(Address3.Text, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 350, 170)
Else
e.Graphics.DrawString(Address3.Text, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 350, 150)
End If
If CoverPic.Image Is Nothing Then
Else
e.Graphics.DrawImage(CoverPic.Image, 75, 300)
End If

End Sub
 
This was confusing to me at first also. In VB.NET the way you have it setup as far as using the System.Drawing.Printing.PrintDocument() class.

Im sure you have a top level variable dimmed like so:
Private PrintDocument1 As New System.Drawing.Printing.PrintDocument()

When you call PrintDocument1.Print() the sub gets called one time. If you want another page to print you must use your System.Drawing.Printing.PrintPageEventArgs.

So, to print an additional page or several you would have this code in your sub:

e.HasMorePages = True

Setting the HasMorePages property to True tells the PrintDocument class to keep printing.
To get it to stop printing you must set the property to false:

e.HasMorePages = False

So you have to put some counters and or flags maybe a Case or some Ifs in your sub to determine when and what to print.


:)
 
Ive played around with that a little bit, but Im not sure thats what I need. Im trying to specify when I want a new page to start. I got sections of the form printing on what will be the first (cover) page, now, I want the rest of the form to print on the second, third.... pages. Any suggestions on that?
 
in addition to what I just said.... this might help...

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawString(Title.Text, New Font("Arial", 18, FontStyle.Bold), Brushes.Black, 350, 100)
e.Graphics.DrawString(Address1.Text, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 350, 130)
If Address2.Text <> "" Then
e.Graphics.DrawString(Address2.Text, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 350, 150)
e.Graphics.DrawString(Address3.Text, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 350, 170)
Else
e.Graphics.DrawString(Address3.Text, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 350, 150)
End If
If CoverPic.Image Is Nothing Then
Else
e.Graphics.DrawImage(CoverPic.Image, 75, 300)
End If

START NEW PAGE HERE
AND CONTINUE PRINTING

End Sub
 
That is what I was talking about having to put like a flag in there to tell it what to print. It isnt like VB 4, 5, or 6. There is nothing to say ok I want a new page right here. See below:

[VB]

Private myFlag as Integer = 0

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

If myFlag = 0 Then
e.Graphics.DrawString(Title.Text, New Font("Arial", 18, FontStyle.Bold), Brushes.Black, 350, 100)
e.Graphics.DrawString(Address1.Text, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 350, 130)
If Address2.Text <> "" Then
e.Graphics.DrawString(Address2.Text, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 350, 150)
e.Graphics.DrawString(Address3.Text, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 350, 170)
Else
e.Graphics.DrawString(Address3.Text, New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 350, 150)
End If
If CoverPic.Image Is Nothing Then
Else
e.Graphics.DrawImage(CoverPic.Image, 75, 300)
End If

myFlag += 1
e.HasMorePages = True

ElseIf myFlag = 1 Then

START NEW PAGE HERE
AND CONTINUE PRINTING

*****Now here if you want to see when to go to the next page you can check the the margin bounds. There is a good example from aewarnick in C# that you can get some ideas from
*****http://www.computerhelp.forum/showthread.php?s=&threadid=75229


e.HasMorePages = False
End If
End Sub
[/VB]
 
Last edited by a moderator:
Back
Top