winformsvisual-studio-2015printingdot-matrixlpt

Printing from Windows application to LPT1 port


I'm building a windows application on VS 2015 and I need it to print simple text using LPT1 on Dot-Matrix printer. anyone can help? Thank you


Solution

  • I was able to solve it, and for anyone that face this issue, here is the code that worked:

    Public Class PrintHelper
        Friend TextToBePrinted As String
        Dim prn As New Printing.PrintDocument
    
        Public Sub prt(ByVal text As String, ByVal printer As String)
            TextToBePrinted = text
            Using (prn)
                prn.PrinterSettings.PrinterName = printer
                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 Printing.PrintPageEventArgs)
            Dim myFont As New Font("Courier New", 9)
            args.Graphics.DrawString(TextToBePrinted, _
               New Font(myFont, FontStyle.Regular), _
               Brushes.Black, 50, 50)
        End Sub
    End Class