How to get total page of PrintPreviewDialog

jerry_hokh

Active member
Joined
Mar 14, 2006
Messages
25
Programming Experience
1-3
Hi all,
it would be appreciated if any one can help me out on this:
Pls show me how to get total page of PrintPreviewDialog

thanks alot in advance.
Jerry
 
VB.NET:
Imports System.Drawing.Printing
Public Class Form1 Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
RichTextBox1.Text = "Programmers have undergone a major change in many years of _
programming various machines. For example, what could take days to create an _
application in other programming languages like C, C++ could be done in hours with _
Visual Basic. Visual Basic provides many interesting sets of tools to aid us in _
building exciting applications. Visual Basic provides these tools to make our _
life far more easier because all the real hard code is already written for us."
'filling the richtextbox with some text that can be used readily
End Sub

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MenuItem2.Click
If PrintDialog1.ShowDialog = DialogResult.OK Then
'showDialog method makes the dialog box visible at run time
PrintDocument1.Print()
End If
End Sub

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MenuItem3.Click
Try
PrintPreviewDialog1.ShowDialog()
Catch es As Exception
MessageBox.Show(es.Message)
End Try
End Sub

Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MenuItem4.Click
With PageSetupDialog1
.PageSettings = PrintDocument1.DefaultPageSettings
End With
Try
If PageSetupDialog1.ShowDialog = DialogResult.OK Then
PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
End If
Catch es As Exception
MessageBox.Show(es.Message)
End Try
End Sub

Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MenuItem5.Click
Try
PrintPreviewControl1.Document = PrintDocument1
Catch es As Exception
MessageBox.Show(es.Message)
End Try
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As_
System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'PrintPage is the foundational printing event. This event gets fired for every 
' page that will be printed
Static intCurrentChar As Int32
' declaring a static variable to hold the position of the last printed char
Dim font As New Font("Verdana", 14)
' initializing the font to be used for printing
Dim PrintAreaHeight, PrintAreaWidth, marginLeft, marginTop As Int32
With PrintDocument1.DefaultPageSettings
' initializing local variables that contain the bounds of the printing area rectangle
PrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
PrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
' initializing local variables to hold margin values that will serve
' as the X and Y coordinates for the upper left corner of the printing 
' area rectangle.
marginLeft = .Margins.Left
marginTop = .Margins.Top
' X and Y coordinate
End With

If PrintDocument1.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = PrintAreaHeight
PrintAreaHeight = PrintAreaWidth
PrintAreaWidth = intTemp
' if the user selects landscape mode, swap the printing area height and width
End If

Dim intLineCount As Int32 = CInt(PrintAreaHeight / font.Height)
' calculating the total number of lines in the document based on the height of
' the printing area and the height of the font
Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, PrintAreaWidth, PrintAreaHeight)
' initializing the rectangle structure that defines the printing area
Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
'instantiating the StringFormat class, which encapsulates text layout information
Dim intLinesFilled, intCharsFitted As Int32
e.Graphics.MeasureString(Mid(RichTextBox1.Text, intCurrentChar + 1), font,_
New SizeF(PrintAreaWidth, PrintAreaHeight), fmt, intCharsFitted, intLinesFilled)
' calling MeasureString to determine the number of characters that will fit in
' the printing area rectangle
e.Graphics.DrawString(Mid(RichTextBox1.Text, intCurrentChar + 1), font,_
Brushes.Black, rectPrintingArea, fmt)
' print the text to the page
intCurrentChar += intCharsFitted
'advancing the current char to the last char printed on this page
< RichTextBox1.Text.Length Then
If intCurrentChar e.HasMorePages = True
'HasMorePages tells the printing module whether another PrintPage event should be fired
Else
e.HasMorePages = False
intCurrentChar = 0
End If
End Sub

End Class
 
Thank for your response.
it seems that your code get the total page after all pages have printed.
Can you show me the way of getting this result:
" page 1 0f 10 , page 2 of 10 , ....." I would like to print this in every page when it first generated.Not waiting until every pages are generated then get the total page number which is here 10

Many thanks.
Jerry.
 
Insert special field

You just right click on the rpt file(e.g. Report1.rpt) go to insert . There you will find the option special field where you select PageNofM and put it on the page Header. The report will print the page No as you wanted. I feel this should solve your problem.
 
Back
Top