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:

// 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('', 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('190000203800001' + Item."No.");
StreamWriter.WriteLine('190000203300001' + Item.Description);
StreamWriter.WriteLine('190000201800001' + Item."Unit Price");
StreamWriter.WriteLine('1F1207000200020' + EAN13-Barcode);  
StreamWriter.WriteLine('E' + FF);


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.


    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.


    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.


      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?


    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?


Leave a Reply

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

You are commenting using your 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