Tuesday 17 July 2012

Print from mobile application directly into a network printer

In this article i will explain how we can print a report directly from a mobile application connected through wireless network to the printer, so try the code below.


Dim strPrintData As String = "" 'Contains the string to be printed
Dim DataBytes() As Byte 'Contains the String converted into bytes
Dim PrintedStream AsNetworkStream 'Stream to transmit the bytes to the Network printer
Dim HostIPAddress AsSystem.Net.IPAddress 'IP Address of the Printer
Dim PrinterPort As NewSystem.Net.Sockets.TcpClient 'TCPClient to comunicate
'HostIPAddress = System.Net.IPAddress.Parse("192.168.201.201")
HostIPAddress = System.Net.IPAddress.Parse(dvWarehouseKeepers(0)("PCADDRESS").ToString)

          
          

strPrintData += Chr(27) &"&l1O"
     
strPrintData += "Hello world" & vbCrLf

Dim Unicode As NewSystem.Text.UnicodeEncoding
DataBytes = System.Text.Encoding.ASCII.GetBytes(strPrintData)

'If Not connected then connect
If NotPrinterPort.Client.Connected Then
'Printer Port 9100 for HP JetDirects
PrinterPort.Connect(HostIPAddress, 9100)
End If

PrintedStream = PrinterPort.GetStream
'Send the stream to the printer
 If PrintedStream.CanWriteThen
     PrintedStream.Write(DataBytes, 0, DataBytes.Length)
 End If
'Close the connections
PrintedStream.Close()
PrinterPort.Close()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Happy coding

No comments:

Post a Comment