preview multiple pages in PrintPreview

how to print multiple pages.

Page 1 of 1

6 Replies - 21059 Views - Last Post: 10 December 2009 - 07:11 PM Rate Topic: -----

#1 matyus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 22-February 09

preview multiple pages in PrintPreview

Post icon  Posted 23 November 2009 - 05:33 AM

how to preview multiple pages in Printpreview? :/
this is my example :

Public Class Form1

	Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click
		PrintPreviewDialog1.Document = PrintDocument1
		PrintPreviewDialog1.ShowDialog()
	End Sub

	Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
		Dim PrintFont As New Font("Arial", 10)
		Dim LineHeightSingle As Single = PrintFont.GetHeight + 2
		Dim VerticalPrintLocationSingle As Single = e.MarginBounds.Top
		Dim HorizontalPrintLocationSingle As Single = e.MarginBounds.Left
		Dim TextString As String
		Dim NumberInteger As Integer = 0

		Do
			NumberInteger += 1
			TextString = NumberInteger.ToString
			e.Graphics.DrawString(TextString, PrintFont, Brushes.Black, HorizontalPrintLocationSingle, VerticalPrintLocationSingle)
			VerticalPrintLocationSingle += LineHeightSingle

		Loop Until NumberInteger = 80
	End Sub
End Class


i want print number 1 to 80 in each line but it only shows number from 1 to 62 in one page. it needs another page to print all of these numbers. what should i do to print all of them?

Is This A Good Question/Topic? 0
  • +

Replies To: preview multiple pages in PrintPreview

#2 bigbug   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 97
  • Joined: 27-October 09

Re: preview multiple pages in PrintPreview

Posted 23 November 2009 - 12:21 PM

Use 'hasmorepages'.

The below code is not all my work, it was derived of help from another D.I.C (Sothrie).

Logically, hasmorepages would seem to be a great toggle to print the next page, but in reality it is a dumb toggle. It has no idea where to start or what to print, that is up to you.

You provide the condition via For Next, Select Case, If Then etc... and fill the condition with the information to be printed.

As you can see in the code below I Select a 'pagenumber' and then print the predefined contents of that page. Then I increment pagenumber by one and repeat the Select statement until all three pages have been printed. Notice that in each Case I set e.hasmorepages to true to refire the print event. Setting the e.hasmorepages to false is not necessary but does help illustrate waht is taking place.

Dim PageNumber as Integer = 0

Private Sub PrintDoc2_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc2.PrintPage
		Dim reportFont As New Font("Arial", 12, FontStyle.Regular)
		Dim titleFont As New Font("Arial", 22, FontStyle.Regular)
		
		Select Case PageNumber
		 Case 0
				e.Graphics.DrawString(Me.Label1.Text, reportFont, Brushes.Black, 100, 100)
				e.Graphics.DrawString(Me.Label1A.Text, reportFont, Brushes.Black, 250, 100)
				e.Graphics.DrawString(Me.Label1B.Text, reportFont, Brushes.Black, 320, 100)
				e.HasMorePages = True
		Case 1
				e.Graphics.DrawString(Me.Label2.Text, reportFont, Brushes.Black, 100, 100)
				e.Graphics.DrawString(Me.Label2A.Text, reportFont, Brushes.Black, 250, 100)
				e.Graphics.DrawString(Me.Label2B.Text, reportFont, Brushes.Black, 320, 100)
				e.HasMorePages = True
		Case 2
				e.Graphics.DrawString(Me.Label3.Text, reportFont, Brushes.Black, 100, 100)
				e.Graphics.DrawString(Me.Label3A.Text, reportFont, Brushes.Black, 250, 100)
				e.Graphics.DrawString(Me.Label3B.Text, reportFont, Brushes.Black, 320, 100)
				e.HasMorePages = False
		End Select

	   PageNumber += 1

End Sub



bigbug

This post has been edited by bigbug: 23 November 2009 - 12:33 PM

Was This Post Helpful? 0
  • +
  • -

#3 matyus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 22-February 09

Re: preview multiple pages in PrintPreview

Posted 23 November 2009 - 01:32 PM

oo.. i tried used e.HasMorePages like you said but when i printpreview, it show infinite pages but all those pages are same. so i add e.HasMorePages = false at the end of the coding to stop the infinite looping. but it is not what i want.
this is my example :

Public Class Form1

	Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click
		PrintPreviewDialog1.Document = PrintDocument1
		PrintPreviewDialog1.ShowDialog()
	End Sub

	Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
		Dim PrintFont As New Font("Arial", 10)
		Dim LineHeightSingle As Single = PrintFont.GetHeight + 2
		Dim VerticalPrintLocationSingle As Single = e.MarginBounds.Top
		Dim HorizontalPrintLocationSingle As Single = e.MarginBounds.Left
		Dim TextString As String
		Dim NumberInteger As Integer = 0

		Do
			NumberInteger += 1
			TextString = NumberInteger.ToString
			e.Graphics.DrawString(TextString, PrintFont, Brushes.Black, HorizontalPrintLocationSingle, VerticalPrintLocationSingle)
			VerticalPrintLocationSingle += LineHeightSingle
		Loop Until NumberInteger = 50

		e.HasMorePages = True
		NumberInteger = 5
		Do
			NumberInteger += 1
			TextString = NumberInteger.ToString
			e.Graphics.DrawString(TextString, PrintFont, Brushes.Black, HorizontalPrintLocationSingle, VerticalPrintLocationSingle)
			VerticalPrintLocationSingle += LineHeightSingle
		Loop Until NumberInteger = 60
		e.HasMorePages = False
	End Sub
End Class


i want print number 1 to 50 in first page and 5 to 60 in second page but it not happen. it print vertically 1 to 50 and continue print 5 to 17 and the next number to 60 is invisible because should print in next page.
so,what wrong with my coding?i do not know how to use e.HasMorePages, i tried search in another websites about it but i really not understand. this year is my first semester learn vb.net and i did not know too much about it. please help me.

This post has been edited by matyus: 23 November 2009 - 01:35 PM

Was This Post Helpful? 0
  • +
  • -

#4 bigbug   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 97
  • Joined: 27-October 09

Re: preview multiple pages in PrintPreview

Posted 23 November 2009 - 03:45 PM

LOL I know what you want to do and I know you did not READ my post...

So, here we go again.

You need a condition to be met... look at my select case statement, it is based on the value of PageNumber. If the page number is 0 ie.. case 0 them I print the contents of case 0. I know there are more pages so I set e.hasmorepages to True. The case statement finishes, breaks of of the select case and INCREMENTS PageNumber ie.. PageNumber += 1.

The next time through it hits Case 1 and prints the stuff insside of case 1 and so on and so fourth.

You code is simply telling the print sub to fire again and print its contents. You have nothing to divide the contents into pages.

Dim PageNumber as Integer = 0

Private Sub PrintDoc2_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc2.PrintPage
		Dim reportFont As New Font("Arial", 12, FontStyle.Regular)
		Dim titleFont As New Font("Arial", 22, FontStyle.Regular)
		
		Select Case PageNumber
		 Case 0
				PUT YOUR DO STUFF HERE FOR THE FIRST PAGE
				e.HasMorePages = True
		Case 1
				PUT YOUR DO STUFF HERE FOR THE SECOND PAGE
				e.HasMorePages = False
		
		End Select

	   PageNumber += 1

End Sub


Was This Post Helpful? 1
  • +
  • -

#5 bigbug   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 97
  • Joined: 27-October 09

Re: preview multiple pages in PrintPreview

Posted 23 November 2009 - 03:50 PM

Nor do you have anything to tell it that there is nothing else to print so yes, it will go on until you stop it or your system overloads...

Every time the program sees e.hmp=true it fires a new print event and in your case that is before it gets to e.hmp=false...
Was This Post Helpful? 0
  • +
  • -

#6 matyus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 22-February 09

Re: preview multiple pages in PrintPreview

Posted 24 November 2009 - 01:38 AM

:D thanks a lot bigbug..!
i get it.. i use your coding and put my coding in there and it work like i want.. but how the looping happen? after the case command, pagenumber increase to 1 and why PrintDoc2_PrintPage is looping?i did not see any looping command like do while, do untill or etc to do looping.. i'm understand how to use it now but i do not understand how it work.
Was This Post Helpful? 0
  • +
  • -

#7 matyus   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 22-February 09

Re: preview multiple pages in PrintPreview

Posted 10 December 2009 - 07:11 PM

View Postbigbug, on 23 Nov, 2009 - 02:45 PM, said:

LOL I know what you want to do and I know you did not READ my post...

So, here we go again.

You need a condition to be met... look at my select case statement, it is based on the value of PageNumber. If the page number is 0 ie.. case 0 them I print the contents of case 0. I know there are more pages so I set e.hasmorepages to True. The case statement finishes, breaks of of the select case and INCREMENTS PageNumber ie.. PageNumber += 1.

The next time through it hits Case 1 and prints the stuff insside of case 1 and so on and so fourth.

You code is simply telling the print sub to fire again and print its contents. You have nothing to divide the contents into pages.

Dim PageNumber as Integer = 0

Private Sub PrintDoc2_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc2.PrintPage
		Dim reportFont As New Font("Arial", 12, FontStyle.Regular)
		Dim titleFont As New Font("Arial", 22, FontStyle.Regular)
		
		Select Case PageNumber
		 Case 0
				PUT YOUR DO STUFF HERE FOR THE FIRST PAGE
				e.HasMorePages = True
		Case 1
				PUT YOUR DO STUFF HERE FOR THE SECOND PAGE
				e.HasMorePages = False
		
		End Select

	   PageNumber += 1

End Sub



hi bigbug, may i ask you one more thing? i can printpreview multiple pages using same code like you did above, but when i click print icon,nothing are printed on the paper, the printer just roll the paper.. why?
i can print if i remove PageNumber += 1 but only page 1 and no page 2 in the printpreview, so i think the problem is at PageNumber += 1 , right? what should i do? :(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1