Direct Print using .NET

If you want to send print commands directly to a Label or Barcode printer on the network, you can do this with StreamWriter and TcpClient from .NET.

Normally/earlier on this was done by creating a text file with the commands, escape code and data. Sometime refered to as PDL, DPL or ZPL.
And use som CMD line og Shell to send it to a shared printer or mapped LPT1 port.

But in Dynamics NAV you can instead create the data directly to a Stream, and send it to the printers IP address on port 9100. And you won’t even have to install the printer.

Lets see how it is done.
Below is an example of a pricelabel send to a Citizen printer.

Create these variables:
2017-04-26_0730

// Define needed Control characters
SOH := Char.ConvertFromUtf32(1); // Start of heading
STX := Char.ConvertFromUtf32(2); // Start of text
FF := Char.ConvertFromUtf32(12); // Form feed

// Create instance of TcpClient class and connect
TcpClient := TcpClient.TcpClient();
TcpClient.Connect('192.168.123.200', 9100);

// Create instance of StreamWriter class and set Encoding
StreamWriter := StreamWriter.StreamWriter(TcpClient.GetStream(), Encoding.UTF8); 

// Create the commands and data to send to the printer
StreamWriter.WriteLine(STX + 'e');
StreamWriter.WriteLine(SOH + 'D' + STX + 'L');
StreamWriter.WriteLine(STX + 'L');
StreamWriter.WriteLine('D10');
StreamWriter.WriteLine('m');
StreamWriter.WriteLine('H17');
StreamWriter.WriteLine('190000203800001' + Item."No.");
StreamWriter.WriteLine('190000203300001' + Item.Description);
StreamWriter.WriteLine('190000201800001' + Item."Unit Price");
StreamWriter.WriteLine('1F1207000200020' + EAN13-Barcode);  
StreamWriter.WriteLine('E' + FF);

StreamWriter.Close();
TcpClient.Close();

7 thoughts on “Direct Print using .NET

  1. This looks great. Does this code get executed on the server or on the client? I ask because we will be running a terminal server session in which we will use the NAV Client.

    I am wondering whether the Terminal Server Session or the NAV Server will need to be able to ping the IP address.

    Like

    1. Yes it runs on server. But if it needs to run on client, you just set all the DOTNET variables to RunOnClient in proberties of the variable.
      So the solution supports both.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s