DotNetWebTutor.com                                                    Link to Us

Printing
visual basic printing
To print use the PrintDocument.Print method
You have to plan and layout your printing

PrintDocument-
Can be added from the toolbox
Or added in code

This represents your printer
Whatever you draw on your printDocument can be sent to your printer
A graphics object represents your print surface

After you add the control you can call the Print method
This raises the control’s PrintPage event

Add your code here to create output for the printer
print method
Print Method
Used to start the printing process

Printing Events:

  • BeginPrint event
    • raised first
  • PrintPage event
    • raised when you need to print more than one page for each extra page
  • EndPrint
    • Event raised when the printing is done

vb print dialog
PrintDialog
Lets the user choose printer, print range and settings before printing
You have to set the control’s PrintSettings
Ex:
'Set the print documents settings by using the PrintDialog
PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
End If

Selecting a printer from the dialog makes it the active printer
You can also set the printer in your code”
Printdocument1.PrinterSettings.PrinterName=you printer
Ex:
PrintDocument1.PrinterSettings.PrinterName = "Macromedia FlashPaper"

page setup dialog
PageSetupDialog
Used to adjust borders, margins and spacing of the print job
The printDocument object has a DefaultPageSettings property

You must drop the pageSetupDialog control to the form and call the ShowDialog method

PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
If PageSetupDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
End If

You can set a pagesetup dialog box before displaying it by using code
'create your own pagesettings
Dim myPageSetup As New PageSettings
myPageSetup.Margins.Left = 150
myPageSetup.Margins.Right = 200
myPageSetup.Margins.Top = 110
myPageSetup.Margins.Bottom = 300
'set the pagesetupDialog's pagesettings with your settings
PageSetupDialog1.PageSettings = myPageSetup
print preview dialog
PrintPreviewDialog
Lets the user view the document on the screen before printing

2 steps:

  • Set the document of the printpreviewdialog to the printdocument
  • Show the printPreviewDialog with the ShowDialog method

PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()

You need to put this in a try/catch block because if the printer is on a network and cannot be accessed you will get an exception

Try
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
Catch ex As Exception
MessageBox.Show("Printing Error" & ex.Message)
End Try

print preview control
PrintPreviewControl
Used to preview printouts. This is the preview pane only. You have to provide your own interface

pagebounds
PageBounds.Width
Can calculate the width of our paper
print more than one page
To print more than one page
We can set the hasMorePages to true to print more than one page
Set it just before the end of the PrintPage event handler
Ex:
If myPages < 4 Then
e.HasMorePages = True
Else
e.HasMorePages = False
myPages = 0
End If

myPages variable must be static so it will retain its value from page to page
To print to must choose the font and location on the page to print

DrawString
The drawstring method lets you draw text in the location you choose
Ex:
e.Graphics.Drawstring(stringToPrint, fontvariable, Color, XPos,YPos)
DrawLine
Lets you draw a line on the print document

DrawRectangle
Lets you draw a rectangle on the print document
start new project

  • Start a new project
  • Add several labels and textboxes
  • Add a button and change the text to Print
  • Add another button and change the text to Printer Settings

print example

  • Add the following controls
    • PrintDocument
    • PrintDialog
    • PrintPreviewDialog

in tray

  • Go to the codeView and add the following import statement

Imports System.Drawing.Printing

  • Add this code the printer Settings button

'Set the print documents settings by using the PrintDialog
PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
End If

  • Add this code to the Print button’s click event handler (The comments can be omitted)

'allow the user to select a range for printing
PrintDialog1.AllowSomePages = True
'set the printPreviewDialog to your current dialog
PrintPreviewDialog1.Document = PrintDocument1
'Show the print preview dialog
PrintPreviewDialog1.ShowDialog()
'Set the print dialog to your current dialog
PrintDialog1.Document = PrintDocument1
'Display the PrintDialog and capture the result
Dim result As DialogResult = PrintDialog1.ShowDialog()
'if the result was OK then print the document
If (result = Windows.Forms.DialogResult.OK) Then
PrintDocument1.Print()
End If

  • Select the class name window and click the drop down
  • Select the PrintDocument1 class

printDocument1

  • Select the Method Name window and choose Printpage

printPage

  • Add the following code:

Dim HeaderFont = New Font("Arial", 14, FontStyle.Bold)
e.Graphics.DrawString("Name", HeaderFont, Brushes.Black, 100, 100)
e.Graphics.DrawString("CourseID", HeaderFont, Brushes.Black, 250, 100)
e.Graphics.DrawString("CourseName", HeaderFont, Brushes.Black, 400, 100)
Dim myFont = New Font("Arial", 12, FontStyle.Regular)
e.Graphics.DrawString(TextBox1.Text, myFont, Brushes.Black, 100, 150)
e.Graphics.DrawString(TextBox2.Text, myFont, Brushes.Black, 250, 150)
e.Graphics.DrawString(TextBox3.Text, myFont, Brushes.Black, 400, 150)

  • Test it

test it
print preview

adding page setup
Adding Page Setup

  • Add another button and change the text to Page Setup
  • Add this code to the button click event handler

PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
If PageSetupDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
End If

  • Test it

vb print page setup

vb printing