From: Ralph Malph on
I am having a problem printing multiple pages with a common header/footer etc
in VB 2008.

I have the following code which will print a single page exactly as I need it.


'***************** Print button click event ************

Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdPrint.Click

pdPrintCal.PrinterSettings.PrinterName = lblCurPrt.Text
pdPrintCal.DefaultPageSettings.Landscape = True

If chkPrtPView.Checked Then

pvPrintCal.Document = pdPrintCal
pvPrintCal.Icon = Me.Icon
pvPrintCal.ShowDialog()

Else

pdPrintCal.Print()

End If

End Sub

'************************************

'***** _PrintPage Sub *************************

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

Dim PageNum as Integer = 1
Dim PDate As Date = Today


Dim CenterString As New StringFormat
CenterString.Alignment = StringAlignment.Center
CenterString.LineAlignment = StringAlignment.Near

Dim RightAlignString As New StringFormat
RightAlignString.Alignment = StringAlignment.Far
RightAlignString.LineAlignment = StringAlignment.Near

'***** Start of Header section, this is same for all Pages *****

Dim newImage As Image = Image.FromFile("C:\Documents and
Settings\UserName\My Documents\Visual Studio 2008\Projects\EMS Clinical
V2\EMS SPC Logo.bmp")
Dim ImgW As Integer = newImage.Width * 0.85
Dim ImgH As Integer = newImage.Height * 0.85

e.Graphics.DrawImage(newImage, 50, 9, ImgW, ImgH)


e.Graphics.DrawString("Sandford College Clinical Schedule for:", New
Font("Times New Roman", 16, FontStyle.Bold), Brushes.Black, 550, 10,
CenterString)

e.Graphics.DrawString(lblCalName.Text, New Font("Times New Roman",
16, FontStyle.Bold), Brushes.Black, 550, 42, CenterString)

e.Graphics.DrawString("Date Printed: " &
PDate.Date.ToShortDateString, New Font("Times New Roman", 12,
FontStyle.Bold), Brushes.Black, 1050, 50, RightAlignString)

e.Graphics.DrawLine(Pens.Red, 50, 70, 1050, 70)

'******** End of header section ********

'******** Print unique data here, may take more than one 8 1/2 x 11 page to
print all data ***************

e.Graphics.DrawString("Print a bunch of Data here.", New Font("Times New
Roman", 16, FontStyle.Bold), Brushes.Black, 550, 100, CenterString)

'************* End of unique data ************

'************** Start of Footer which just prints a Page Num *******

e.Graphics.DrawString("Page number: " & PageNum, New Font("Times New
Roman", 16, FontStyle.Bold), Brushes.Black, 550, 750, CenterString)

'******************** End of Footer *********

'******* Here I put code to see if I need another page to finish printing my
data ****

if MoreData = true then

PageNum += 1

'******* Code I need help with to cause new page etc. goes here ******

' *** Have tried... e.HasMorePages = True ... then looping back to top of
routing etc...

end if


End Sub


I have tried several different ways to get it to print more than one page
but It either fails to print one page or seems to print an infinate number of
pages.

As a simple test I tried the following....


'***** _PrintPage Sub *************************

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

Dim PageNum as Integer = 1
Dim PDate As Date = Today


Dim CenterString As New StringFormat
CenterString.Alignment = StringAlignment.Center
CenterString.LineAlignment = StringAlignment.Near

Dim RightAlignString As New StringFormat
RightAlignString.Alignment = StringAlignment.Far
RightAlignString.LineAlignment = StringAlignment.Near

For PageNum = 1 to 3 '**** This For next loop is used to create 3 pages
with PageNum being only difference ******


'***** Start of Header section, this is same for all Pages *****

Dim newImage As Image = Image.FromFile("C:\Documents and
Settings\UserName\My Documents\Visual Studio 2008\Projects\EMS Clinical
V2\EMS SPC Logo.bmp")
Dim ImgW As Integer = newImage.Width * 0.85
Dim ImgH As Integer = newImage.Height * 0.85

e.Graphics.DrawImage(newImage, 50, 9, ImgW, ImgH)


e.Graphics.DrawString("Sandford College Clinical Schedule for:", New
Font("Times New Roman", 16, FontStyle.Bold), Brushes.Black, 550, 10,
CenterString)

e.Graphics.DrawString(lblCalName.Text, New Font("Times New Roman",
16, FontStyle.Bold), Brushes.Black, 550, 42, CenterString)

e.Graphics.DrawString("Date Printed: " &
PDate.Date.ToShortDateString, New Font("Times New Roman", 12,
FontStyle.Bold), Brushes.Black, 1050, 50, RightAlignString)

e.Graphics.DrawLine(Pens.Red, 50, 70, 1050, 70)

'******** End of header section ********

'******** Print unique data here, may take more than one 8 1/2 x 11 page to
print all data ***************

e.Graphics.DrawString("Print a bunch of Data here.", New Font("Times New
Roman", 16, FontStyle.Bold), Brushes.Black, 550, 100, CenterString)

'************* End of unique data ************

'************** Start of Footer which just prints a Page Num *******

e.Graphics.DrawString("Page number: " & PageNum, New Font("Times New
Roman", 16, FontStyle.Bold), Brushes.Black, 550, 750, CenterString)

'******************** End of Footer *********

'******* Here I put code to see if I need another page to finish printing my
data ****



'******* For my test to see if I can control multi page printing I used the
following in conjunction with the For Next Loop.... ******

If PageNum < 3 then

e.HasMorePages = True

Else

e.HasMorePages = False

End If

Next

End Sub

To make a long story short this did not work. All I got was one page and the
3 page numbers were writen on top of each other.

How can I make this work?

Any help would be greatly appreciated.

I would also like to know how to put the header and footer in to a seperate
Sub I could call as needed in the _PrintPage Sub in a maner similar to the
following one but that is not as important as just getting it to create new
pages as needed....

'****** Psudo Code..... ***


Private Sub PrintHeader ()

' ***** Do all the printing for the Header section used above ****

End Sub

Private Sub PrintFooter (ByVal PageNum as Integer)

' ***** Do all the printing for the Footer section used above ****

End Sub


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


Define Variable and constants.....

'**** Send Header to the printer....

PrintHeader()

'******* Send Data to Printer ......

e.Graphics.DrawString("Print a bunch of Data here.", New Font("Times New
Roman", 16, FontStyle.Bold), Brushes.Black, 550, 100, CenterString)


'*********** Send Footer to Printer......

PrintFooter(PageNum)

'******** Test for more data to print *****

If MoreData = True

PageNum = PageNum + 1

Loop back to Print New page....

Else

End Print Job

End Sub


This was a lot eaisier in VB6. I am currently converting a program I wrote
in VB6 to VB2008.Net which is why I am here needing help. This is my first
VB.Net project and the printing of my data is the last hurdle I need to get
over.


Thanks again for all the help,

Ralph Malph
From: OmegaSquared on
Hello, Ralph Malph,

What you need to do is set e.HasMorePages = True and then exit the routine.
That is, you don't loop back to the top. If e.HasMorePages is True the
PrintPage routine will be called again (and continue to be called until
e.HasMorePages is False). After the last page has been printed set
e.HasMorePages = False so that the PrintPage routine won't be called again.

One issue that you will have to deal with is that your PageNum variable is
local (i.e. private) to the PrintPage routine. So the value will be reset to
one for each new page -- probably not what you want.

Re: "I would also like to know how to put the header and footer in to a
seperate
Sub "
I can't see any reason why you would have a problem doing this. Just make a
real implementation of what you have shown as pseudo code.

Cheers,
Randy

From: Ralph Malph on
Randy,

Thank you for the info. I needed to know how the "Flow" logic worked for the
e.HasMorePages function. Exiting the Sub and making the PageNum variable good
for the whole form got my print routine working AOK.

I knew it had to be something simple.

Now to get my Datagrid to print....

Thanks again,

Ralph Malph


"OmegaSquared" wrote:

> Hello, Ralph Malph,
>
> What you need to do is set e.HasMorePages = True and then exit the routine.
> That is, you don't loop back to the top. If e.HasMorePages is True the
> PrintPage routine will be called again (and continue to be called until
> e.HasMorePages is False). After the last page has been printed set
> e.HasMorePages = False so that the PrintPage routine won't be called again.
>
> One issue that you will have to deal with is that your PageNum variable is
> local (i.e. private) to the PrintPage routine. So the value will be reset to
> one for each new page -- probably not what you want.
>
> Re: "I would also like to know how to put the header and footer in to a
> seperate
> Sub "
> I can't see any reason why you would have a problem doing this. Just make a
> real implementation of what you have shown as pseudo code.
>
> Cheers,
> Randy
>