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();

14 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

    1. Then its another story.
      You would have to use the Win32 API directly, but I am unsure if that is possible from NAV.

      If the printer emulates a serial port, then you can stream to that instead.

      Like

      1. I hvae now succeded in getting a label printer on the net with an IP address. It is a Cognitive DLXi printer. I can print testlabels from my client (outside NAV) – but the code above does not seem to find the printer – any ideas?

        Like

    1. OK, Thanks Brian

      BUt one question though.
      I’m quite new into using dotnet functionality in NAV coding – but it seems very interesting.

      If I find out what printercodes the printer has, wil it be possible to create labels with:
      – different textsize in different lines
      – bold in some lines
      – barcode in one line

      I guess from your example that ‘190000203800001’ + Item.”No.”); points to a specific place on the label (x,y coordination of some kind) – is that correct – or where do you specifi the placement of the text?

      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 )

Connecting to %s