Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Programmatic Printing

4.00/5 (1 vote)
10 Aug 2012CPOL 11.4K  
Easy to ready printing options for vb.net

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. 

VB
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
        'mm2hin - This  function translates millimetres to half inches
        Dim pkCustomSize1 As New PaperSize("Snap Paper", 80 / mm2hin, 0)
        'Paper size is set for any measurement my paper was 80mm wide with no end...
        Dim margins As New Margins(5 / mm2hin, 5 / mm2hin, 0, 10 / mm2hin)
        'Sets the margins to 5 mill left and right and 10 mill at the top
        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)

    ' -- Set the Picture at the top line
    Dim Image As Drawing.Image = Drawing.Image.FromFile("C:\Image.jpg")
    args.Graphics.DrawImage(Image, 0, 0)

    ' -- Declare the X and Y coords for the text to start printing out - xPos 
    '    Center is for text needing to be centered 70 is the offset for the picture
    Dim xPos As Single = 0
    Dim xPosCenter As Single = 0
    Dim yPos As Single = 70
    Dim size As System.Drawing.SizeF

    ' -- Sets the heading text defined in the "Heading String"
    ' -- This gets the size of the font and calculates how big the text will be
    size = args.Graphics.MeasureString(TicketHeadingText, HeadingFont)
    ' -- Adding this line centers the text
    xPosCenter = ((args.PageBounds.Width * args.Graphics.PageScale) / 2) - (size.Width / 2)
    ' -- This line Draws the Text as String and sets a Font to use
    args.Graphics.DrawString(TicketHeadingText, HeadingFont, Drawing.Brushes.Black, xPosCenter, yPos)
    ' -- This is what to set the next line too... adding a *1.5 to
    '    the end will blow out the yPos and will give a larger blank space
    yPos = yPos + size.Height * 1.5


    ' -- Prints the Ticket Number passed via the TicketNo String 
    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


    ' -- Prints the Appointment Heading text - This will contain
    '    the Time and Place of Appointment - "Your appointments for today:"
    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


        ' -- Prints the Clinic Information text - This will contain the Time and Place of Appointment
        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

        ' -- Prints the Subwait Information text - This will contain the Subwait and Level
        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

        ' -- Prints the Clinic Directions text - This will contain how to get to the subwait
        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

        ' -- This is a line break for multiple appointments - Will print a ------- line across the ticket
        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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)