Introduction
This bit of code was developed for printing tickets out of a web application, it is a great way for picking a printer and organizing text, images, and drawing elements onto the page.
Background
I was asked to introduce a Way Finding solution (pretty much, identify to a kiosk and be presented with a ticket number and directions) and one of the most important elements
of the way finding was printing all applicable details onto a ticket.
Using the code
Adding these two functions to any class file will allow the printer service to be called and text, image and drawing elements passed too it... The code is documented for easy of use.
Public TicketTable As DataTable
Public TicketHeadingText As String
Public TicketNumberText As String
Public AppointmentHeadingText As String
Public AppointmentClinicText As String
Public AppointmentDirectionsText As String
Public Breakline As String
Public EmptyString As String
Public AppointmentSubWaitText As String
Sub Print_Ticket(TicketID As Integer, PrinterName As String)
TicketNumberText = TicketID
Dim prn As New PrintDocument
Using (prn)
Dim mm2hin As Double = 25.4 * 100
Dim pkCustomSize1 As New PaperSize("Snap Paper", 80 / mm2hin, 0)
Dim margins As New Margins(5 / mm2hin, 5 / mm2hin, 0, 10 / mm2hin)
prn.PrinterSettings.DefaultPageSettings.Margins = margins
prn.PrinterSettings.PrinterName = PrinterName
prn.DocumentName = "Ticket"
prn.PrinterSettings.DefaultPageSettings.PaperSize = pkCustomSize1
AddHandler prn.PrintPage, AddressOf Me.PrintPageHandler
prn.Print()
RemoveHandler prn.PrintPage, AddressOf Me.PrintPageHandler
End Using
End Sub
Private Sub PrintPageHandler(ByVal sender As Object, ByVal args As PrintPageEventArgs)
TicketTable = Get_Ticket_Information(TicketNumberText)
TicketHeadingText = "Your Ticket Number is:"
AppointmentHeadingText = "Your appointment(s) for today:"
Dim HeadingFont As New Drawing.Font("Tahoma", 12, Drawing.FontStyle.Bold)
Dim TicketFont As New Drawing.Font("Tahoma", 30, Drawing.FontStyle.Bold)
Dim AppointmentHeadingFont As New Drawing.Font("Tahoma", 12, Drawing.FontStyle.Bold)
Dim AppointmentClinicFont As New Drawing.Font("Tahoma", 12, Drawing.FontStyle.Bold)
Dim AppointmentDirectionsFont As New Drawing.Font("Tahoma", 12, Drawing.FontStyle.Regular)
Dim BreakFont As New Drawing.Font("Tahoma", 12, Drawing.FontStyle.Regular)
Dim EmptyStringFont As New Drawing.Font("Tahoma", 12, Drawing.FontStyle.Regular)
Dim Image As Drawing.Image = Drawing.Image.FromFile("C:\Image.jpg")
args.Graphics.DrawImage(Image, 0, 0)
Dim xPos As Single = 0
Dim xPosCenter As Single = 0
Dim yPos As Single = 70
Dim size As System.Drawing.SizeF
size = args.Graphics.MeasureString(TicketHeadingText, HeadingFont)
xPosCenter = ((args.PageBounds.Width * args.Graphics.PageScale) / 2) - (size.Width / 2)
args.Graphics.DrawString(TicketHeadingText, HeadingFont, Drawing.Brushes.Black, xPosCenter, yPos)
yPos = yPos + size.Height * 1.5
size = args.Graphics.MeasureString(TicketTable.Rows("0").Item("TicketNo").ToString, TicketFont)
xPosCenter = ((args.PageBounds.Width * args.Graphics.PageScale) / 2) - (size.Width / 2)
args.Graphics.DrawString(TicketTable.Rows("0").Item("TicketNo").ToString, _
TicketFont, Drawing.Brushes.Black, xPosCenter, yPos)
yPos = yPos + size.Height * 1.5
size = args.Graphics.MeasureString(AppointmentHeadingText, AppointmentHeadingFont)
xPosCenter = ((args.PageBounds.Width * args.Graphics.PageScale) / 2) - (size.Width / 2)
args.Graphics.DrawString(AppointmentHeadingText, AppointmentHeadingFont, _
Drawing.Brushes.Black, xPosCenter, yPos)
yPos = yPos + size.Height * 2.0
For Each Row In TicketTable.Rows
Dim WayPointData As DataTable = Get_Ticket_Waypoints(Row("ApptId"))
AppointmentClinicText = Row("BookingStartTime") + " " + Row("Specialty")
AppointmentSubWaitText = "• " + WayPointData.Rows(0).Item("WaypointName").ToString + _
" (" + WayPointData.Rows(2).Item("WaypointName").ToString + ")"
AppointmentDirectionsText = "Proceed through " + _
WayPointData.Rows(1).Item("WaypointName").ToString
size = args.Graphics.MeasureString(AppointmentClinicText, AppointmentClinicFont)
xPosCenter = ((args.PageBounds.Width * args.Graphics.PageScale) / 2) - (size.Width / 2)
args.Graphics.DrawString(AppointmentClinicText, AppointmentClinicFont, Drawing.Brushes.Black, xPosCenter, yPos)
yPos = yPos + size.Height * 1.2
size = args.Graphics.MeasureString(AppointmentSubWaitText, AppointmentClinicFont)
xPosCenter = ((args.PageBounds.Width * args.Graphics.PageScale) / 2) - (size.Width / 2)
args.Graphics.DrawString(AppointmentSubWaitText, AppointmentClinicFont, Drawing.Brushes.Black, xPos, yPos)
yPos = yPos + size.Height * 1.2
size = args.Graphics.MeasureString(AppointmentDirectionsText, AppointmentDirectionsFont)
xPosCenter = ((args.PageBounds.Width * args.Graphics.PageScale) / 2) - (size.Width / 2)
args.Graphics.DrawString(AppointmentDirectionsText, _
AppointmentDirectionsFont, Drawing.Brushes.Black, xPosCenter, yPos)
yPos = yPos + size.Height * 2.0
size = args.Graphics.MeasureString(Breakline, BreakFont)
xPosCenter = ((args.PageBounds.Width * args.Graphics.PageScale) / 2) - (size.Width / 2)
args.Graphics.DrawLine(Drawing.Pens.Black, xPos, yPos, _
args.Graphics.PageScale * args.PageBounds.Width - xPos, yPos)
yPos = yPos + size.Height * 1.2
Next
End Sub
Points of Interest
Once you start playing with the Graphics.Draw function you will learn to can do a HEAP of crazy things... There does not seem to be an end for someone willing
to sit there and work out all of the measurements.