Adding barcodes to a report

Completed

In Business Central 2021 Wave 1, fonts were added to generate one-dimensional barcodes in Business Central production and sandbox environments. This means that any custom layouts using the fonts will work. Microsoft has licensed the font packages provided by IDAutomation Inc.

A new AL module was also added to make it easy for developers to encode strings in the different barcode symbologies that the fonts support.

You can start testing the fonts now. See the following information for more information about font types and sizes:

For testing purposes, you can download demo versions of the fonts here:

Install the demo versions of the fonts in your developer environments so that you can design custom reports. 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.

The barcode functionality lets you convert an alphanumeric value in a report dataset into a barcode on a generated report.

The functionality is provided by the Barcode module of the System Application. The module includes the objects and tools that you need to add barcodes to reports. Using the API, you create barcode font providers for generating data strings as barcodes.

A barcode provider includes a library of different barcode fonts and symbologies. As part of the provider, you add barcode font encoders that convert data strings to the specific font specification and symbology.

For more information about this module, see the AL Extensions on GitHub.

Business Central online comes equipped with a barcode provider and fonts from IDAutomation. So, you can start adding barcodes to reports right away.

Setting up Business Central on-premises

With Business Central on-premises, there are couple tasks you'll need to do before you can start adding barcodes to your reports.

  1. Get the barcode fonts that you want to use on reports. You can get barcode fonts from any font provider. If you use the IDAutomation fonts, you can skip the next step and use the built-in IDAutomation barcode font provider.

  2. Create your own barcode font provider with encoders for the fonts. Adhere to the interface in the Barcode module. Consider contributing to the open-source project if you do.

  3. Install the fonts on the Business Central Server computer or machine. Install the fonts in the Business Central Server instance installation folder. By default, the folder is: C:\Program Files\Microsoft Dynamics 365 Business Central\XXX\Service

Encoding a string in AL

To represent a string as a barcode in a report, encode it according to the symbology you want. The Barcode module has encoder methods in codeunit 9215 IDAutomation 1D Provider. You can use these encoders with barcode fonts from IDAutomation.

Use the ValidateInput procedure to validate whether a string can be encoded in a given symbology, and then the EncodeFont procedure to do the actual encoding.

Use table 9203 Barcode Encode Settings to configure smaller variations in how different symbologies work. For example, you can enable an extended character set or checksums in Code39, or change the code set used in Code128.

Adding the encoded string to the report dataset

To show the barcode in a report, add the encoded string to the report dataset. The following code shows an example report that displays the GTIN field of the Item table as a barcode.

The barcode uses the Codabar font from the built-in IDautomation barcode font provider.

report 50100 ItemBarcodeReport
{
 UsageCategory = Administration;
 ApplicationArea = All;
 DefaultLayout = Word;
 Caption = 'Item Barcodes';
 WordLayout = '.\src\ItemBarcodes.docx';

 dataset
 {
  dataitem(Items; Item)
  {
   DataItemTableView = SORTING("No.");
   RequestFilterFields = "No.";
   RequestFilterHeading = 'Items';

   column(No_; "No.")
   {
 IncludeCaption = true;
   }
   column(Description; Description)
   {
 IncludeCaption = true;
   }

   column(Unit_Price; "Unit Price")
   {
 IncludeCaption = true;
   }

   column(Barcode; EncodedText)
   { }
   column(BarcodeLbl; BarcodeLbl)
   { }

   trigger OnAfterGetRecord()
   var

 BarcodeSymbology: Enum "Barcode Symbology";
 BarcodeFontProvider: Interface "Barcode Font Provider";

   begin
 BarcodeFontProvider := Enum::"Barcode Font Provider"::IDAutomation1D;
 BarcodeSymbology := Enum::"Barcode Symbology"::Codabar;
 EncodedText := BarcodeFontProvider.EncodeFont(GTIN, BarcodeSymbology);
   end;
  }
 }

 var
  EncodedText: Text;
  BarcodeLbl: Label 'Barcode';
}

Using a barcode font in the report layout

Each supported symbology has a corresponding font installed in Business Central online. To use a barcode font in the report layout, mark the encoded string from the dataset with the font.

Here's an example of how you can set the barcode in a Word layout, at design time:

Screenshot of the barcode in Word layout.

Here's an example of the barcode, in a Word layout, at runtime:

Screenshot of the barcode in Word layout result.

It's possible as a developer to use a font in a layout without having it installed on the machine that you use to develop the layout. Without the font, the report won't show the barcode. But, if you then test the report in an online sandbox, it will be shown on the report.