Day by day I am moving closer to DotNet programming in Dynamics NAV. More and more of the things I like to do are more easily solved with DotNet than with native C/AL code.
In most cases I can use the standard DotNet types but in some cases I need to build a small DotNet Class to solve the problem.
An example of this landed on my desk yesterday. A colleague of mine needed to be able to print text to the local label printer. Perhaps this can be solved with a simple report but in this case something more was needed. I asked the Internet – how do I print a text file with c# code. Got some answers and selected the one I liked the most.
Normally I rewrite the c# code with DotNet objects in C/AL but in this case I could not. The reason was that I needed the DotNet objects to be executed on the client side, and using DotNet to print requires an event handler. DotNet events are not supported on the client side so I needed to create a class.
I create a c# Class Project in Visual Studio and can use the code I found with only a few modifications.
[code lang=”csharp”] public class NAVTextFilePrinter
public void PrintText(string printerName, string fontName, float fontSize, string linesToPrint)
Font printFont = new Font(fontName, fontSize);
PrintDocument docToPrint = new PrintDocument();
docToPrint.DocumentName = "NAV Text File";
docToPrint.PrinterSettings.PrinterName = printerName;
docToPrint.PrintPage += (s, ev) =>
int count = 0;
float yPos = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
foreach (string line in linesToPrint)
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
ev.HasMorePages = false;
The class is compiled with DotNet 4.5 and added to the Client Add-ins folder. If you are using NAV version 2015 or newer just add it to the Server Add-ins folder.
Using this in NAV is easy
I like to use DotNet when I am handling text – in general. The DotNet object System.String has a lot of functions that can be useful. Just use
String := String.Copy(NAVString);
and your NAV String can now be handled with all the functionality available with DotNet. Good example is the Renumbering Tool I created a few months ago.
Another example here – where I need to create all the directory tree leading to the file I need to copy.
One of my favorite is the Global Variable Store Codeunit, where I use the DotNet Dictionary to store variables globally. This can be used to minimize the footprint of your code changes. For example, if you need to pass a new variable to a function and you don’t want to change the function – just store the variable before you call the function and retrieve it inside the function. It is even possible to pass a whole record this way.