Web Services Examples Part 3 – Generate Barcode

9 Jan

What a busy december month it has been! That last month of 2015 ended before I realized it. I wish everybody a very happy and prosperous 2016!

The last post in the web services series was about one month ago, so it is about time to post the next one. This time we will look at generating barcodes.

The challenge

One might argue “why do we need a web service to create a barcode?”. And this is a good point, as we can print barcodes on reports fairly easy. Here and here are some examples. However, these solutions require you to download and install a font or to download an external library. Not a big deal of course and servers might already have the correct font installed.

Things become a little more complex if you look for a solution that does not require any additional installation. Or you maybe want to display a barcode an a screen or store it in a database as an image so it can be retrieved with a NAV web service. Well, here a web service for barcodes does a very good job.

The web service

At http://www.barcodes4.me/ you can create a barcode very easy. This web site provides a bar code as a downloadable image. This image can be found by composing a hyperlink: http://www.barcodes4.me/barcode/c39/HAPPYNEWYEAR.png?width=250&height=100&istextdrawn=1


Supported barcodes are:

  • Code 39 (c39)
  • Code 128a (c128a)
  • Code 128b (c128b)
  • Code 128c (c128c)
  • 4 of 5 Interleaved (i2of5)
  • QR

The URL format for a barcode is (QR has a slightly different URL):


To build your URL:

  1. Replace [type] with the necessary type (c39, c128a, c128b, c128c, i2of5).
  2. Replace [value] with the value of your requested barcode.
  3. Replace [imagetype] with the type of image you would like created (png, gif, jpg). Use htm for more information about the barcode.

For more information look at this very clear explanation: http://www.barcodes4.me/apidocumentation

This response from this web service is an image. Instead of a JSON or XML response, it provides you with the image data. In other words, the content-type in the response headers is image/png.

The code

For this example I have redesigned the original demo code from NAV Techdays so I could demonstrate all available options.

First I have create a table that can hold all types of barcodes.


This is combined with a list page and a card page to display and enter the data.





I assume that you are not waiting for an explanation how you can bind a record in this table with a item record or any other record, am I right?

What’s probably more interesting is the code behind. Well, here it is:

IF Type = Type::" " THEN BEGIN

Window.OPEN('Generating Bar Code');

IF Type = Type::qr THEN
  Method := STRSUBSTNO('barcode/qr/qr.%1?value=%2&size=%3&ecclevel=%4',
                        GetOptionStringValue("Image Type",FIELDNO("Image Type")),
                        GetOptionStringValue("ECC Level",FIELDNO("ECC Level")))
  Method := STRSUBSTNO('barcode/%1/%2.%3?istextdrawn=%4&isborderdrawn=%5&isreversecolor=%6',
                       GetOptionStringValue("Image Type",FIELDNO("Image Type")),
                       FORMAT("Include Text",0,2),
                       FORMAT("Reverse Colors",0,2));


ImageStream := HttpResponseMessage.Content.ReadAsStreamAsync.Result;



LOCAL GetOptionStringValue(option : Integer;fieldno : Integer) : Text
FldRef := RecRef.FIELD(fieldno);
OptionString := FldRef.OPTIONSTRING;

separator := ',';
options := OptionString.Split(separator.ToCharArray());

The response

What is different from the previous examples is that the response contains an image. That is binary data, so it doesn’t make sense to get the data as text. (Only when you deliberately want to save a base64 representation of the image.)

So instead of using ReadAsStringAsync on the response content, we use ReadAsStreamAsync and stream the result into a .Net MemoryStream. This stream is then saved into a BLOB file in the record. The BLOB field has as subtype Bitmap, so Dynamics NAV knows how to display it.

Download the complete code of this web service example here

That’s it! Next example will be about sending SMS messages from Dynamics NAV.

3 thoughts on “Web Services Examples Part 3 – Generate Barcode

  1. Pingback: How Do I: Print Barcodes in RDLC? – think about IT

  2. hi, you solution is so interresting for me, thx for this posting ,
    just when i need to print the barcode , whats the best solution to do it from the page 60004

    • It’s an image, so you can put it on a report.
      In case you use a label printer, you could consider to use specific barcode instruction. Most label printer support that.

Leave a Reply

Your email address will not be published.