Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm new to vb.net, and I'm trying to print from a form that was filled out. I've got it to work, but I'm 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

Posted

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.

 

I'm 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 If's in your sub to determine when and what to print.

 

 

:)

Posted
I've played around with that a little bit, but I'm not sure that's what I need. I'm 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?
Posted

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

Posted (edited)

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

 


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.xtremedotnettalk.com/showthread.php?s=&threadid=75229


e.HasMorePages = False
End If
End Sub

Edited by bfellwock

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...