Search This Blog

Wednesday, July 8, 2009

Page Setup, Print Preview & Print code in vb.net

Page Setup, Print Preview & Print code in vb.net

' PGAE SETUP CODE
'ADD Control PrintDocument, Pagesetup, PrintDialog

Private Sub ToolStripMenuItem7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem7.Click
'PAGE SETUP
With PageSetupDialog1
.PageSettings = PrintDocument1.DefaultPageSettings
End With

With PageSetupDialog1
.PageSettings = PrintDocument1.DefaultPageSettings
End With
Try
If PageSetupDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
End If
Catch es As Exception
MessageBox.Show(es.Message)
End Try
End Sub


'PRINT PREVIEW CODE
Private Sub ToolStripMenuItem13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem13.Click

PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.WindowState = FormWindowState.Maximized
PrintPreviewDialog1.ShowDialog()

End Sub

'PRINT CODE

Private Sub ToolStripMenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem5.Click
'PRINT
Try
PrintDocument1.Print()
Catch es As Exception
MessageBox.Show(es.Message)
End Try
'pd = New Printing.PrintDocument
' GetFormImage()

End Sub

'NOW PRINTPAGE EVENT HANDLER CODE means what to preview

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
' MessageBox.Show("vikas")
' 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 print_Graphs_cpy2.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 print_Graphs_cpy2.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

'LET THIS FUNCTION IS CREATE A IMAGE OF A FROM, A POST IS ALSO THERE FOR THIS, AND CREATE A IMAGE NAMED formImage
GetFormImage()
e.Graphics.DrawString("PRINTING", font, Brushes.Black, 4, 15)
e.Graphics.DrawImage( formImage, 4, 100)

'YOU CAN ADD ANY OTHER THINGS LIKE THAT

End Sub

No comments:

Post a Comment