Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have created a script first to create string text which i want to print in the pos printer and then creating a notepad file and firing the print command using shell in vb.net but i want to know and find out any alternative method available instead of notepad i can send the print directly to printer which is a usb printer to print the text to the printer in desired format, kindly help.

VB
printtext = Chr(9) & "mytext1" & vbCrLf
           printtext = printtext & Chr(9) & "mytext2" & vbCrLf
           printtext = printtext & Chr(9) & "mytext3" & vbCrLf
           printtext = printtext & Chr(9) & "mytext4" & vbCrLf
           printtext = printtext & Chr(9) & "mytext5" & vbCrLf
           printtext = printtext & Chr(9) & "mytext6" & vbCrLf

           Dim printpath As String = \tprint.txt"

           File.WriteAllText(printpath, printtext)

           Dim sCommand As String
           sCommand = "start /min notepad /P " + "\tprint.txt"
           Shell("cmd.exe /c " & sCommand, AppWinStyle.Hide)
Posted

Try this method "prt" by passing the "Text" and "PrinterName", if the printer is a network printer then specify the full network path. eg. "\\sharedlocation\printername"

C#
Public Class PrintHelper
    Friend TextToBePrinted As String

    Public Sub prt(ByVal text As String, ByVal printer As String)
        TextToBePrinted = text
        Dim prn As New Printing.PrintDocument
        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


hope this will help...
 
Share this answer
 
Comments
digitaldxb 17-Jul-13 8:05am    
i am getting an error that "Me" is valid only with the instance method on the line " AddHandler prn.PrintPage, AddressOf Me.PrintPageHandler"
Member 13736580 20-Mar-18 8:17am    
works fine...
Member 11343625 15-Jan-19 16:30pm    
Need to print to network printer address. i.e. \\par-ps-01\ParZebra

Replacing VB6 code

Open PrinterName For Output As #1
Print #1

Need to convert to VB.NET code. Any help would be appreciated. Thx!!!
netsistemas 8-May-19 15:37pm    
only print 1 page!!
This is the direct way for sending raw text to any printer from VB.NET http://support.microsoft.com/kb/322090[^]
 
Share this answer
 
Comments
netsistemas 8-May-19 15:37pm    
the url is offline
nkm00e 4-May-21 3:13am    
You can use this url: https://www.betaarchive.com/wiki/index.php/Microsoft_KB_Archive/322090
in microsoft windows, add printer like 'generic printer' ports usb, don't use epson tmu-220 drivers, send print to generic printer and voila!!!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900