How to create with 1D and 2D Barcodes in Business Central

Hello, today I want to show in a simple way how to create a report in Business Central that allows us to include barcodes in 1 and 2 dimensions.

In this Microsoft article you can find more detailed information.

In this github link is the repository with all the code of the extension in al and the report format used for this example.

Before starting with the explanation of the code I want to show first what the final result is like.

This post will be divided into 2 parts: report code and word layout.

Report Code

Bardcode 1D

To create 1-dimensional barcodes we must use the following interface:

BarcodeFontProvider: Interface "Barcode Font Provider"

The following enum allows us to initialize the previous interface. For now, it only has IDAutomation1D as the only option.

BarcodeFontProvider := Enum::"Barcode Font Provider"::IDAutomation1D;

Once we have configured the font provider in the previous step, we must choose the symbology to use. This comes to represents the font to use.

BarcodeSymbology: Enum "Barcode Symbology";

The following types of bar code letters are the ones we can choose for a dimension.

The next step will be to encode our information, for the example that we are doing, we will print the barcode of the Items, then we will use Item No as the string to convert, the result will be stored in the text variable Code39_1D.

BarcodeString := Item."No.";
BarcodeFontProvider.ValidateInput(BarcodeString, BarcodeSymbology);
Code39_1D := BarcodeFontProvider.EncodeFont(BarcodeString, BarcodeSymbology);

Procedure Generate_Bardcode_Code39_1D:

Bardcode 2D

Once we create our procedure in charge of embedding the type of font that will render us the bar code for 1D, doing it for 2D is almost the same, what changes are small details.

We must select the provider as follows:

BarcodeFontProvider2D: Interface "Barcode Font Provider 2D";

Once we have configured the font provider in the previous step in this case for 2D, we must choose the symbology to use.

BarcodeSymbology2D: Enum "Barcode Symbology 2D";

The following types of barcode letters are the ones that we can choose for two dimensions.

Similar to the case of 1D, we must convert our Item No to our barcode that we will store in our text variable QRCode_2D.

BarcodeString := Item."No.";
QRCode_2D := BarcodeFontProvider2D.EncodeFont(BarcodeString, BarcodeSymbology2D);

Procedure Generate_Bardcode_QRCode_2D:

Unlike the procedure when it is a dimension, in the case of 2D there is no ValidateInput(BarcodeString, BarcodeSymbology) method;

Report

The code of our report in AL would be as follows.

Word Layout

Once we compile our code, the layout that we have configured will be generated automatically.

The next step will be to open the Word document and enable if we have not done so, the developer mode.

For this last case, we go to word options and mark the developer checkbox.

We go to the Developer tab and click on XML Mapping

On the right side of the window we search for Custom XML Part and select the one with the name of our report as shown in the following image.

The next step will be to create a table and select the entire row, then on the right side of the window, we click on Item (Name of our dataset) and then check the repeating option.

Then we stop at the place in the table where we want our fields to appear, and on the right side of the window we click on “Insert content control” and then “Plain Text“, and repeat with the other fields.

We should be left with something similar:

In the case of the 1D label, press Ctrl+D and select the font that you see in the following image:

In the case of the 2D label, press Ctrl+D and select the font that you see in the following image:

If we have the fonts installed we will see something similar:

We save our changes and publish them.

Special Notes:
1) For on-premises environments:

  • For a Windows environment, install the fonts.
  • For a sandbox Docker environment, download and then install the demo fonts locally. Then use the Add-FontsToNavContainer PowerShell cmdlet to add fonts to the container.

2) For cloud environments, it is not necessary to have the font installed on the local PC.

In the following note from Microsoft is how to install the fonts.

Finally, in this article you can find additional information

I hope this will help.

Leave a Reply

Your email address will not be published. Required fields are marked *