D365 Business Central : Printing 1D and 2D Barcode

Barcode Printing

Microsoft recently added support for printing 2D barcode. If you are on cloud, you don’t need to purchase the barcode font because Microsoft has already provided and licensed them for you. The cloud font packages are provided by IDAutomation Inc. We are going to see how to print both 1D and 2D barcode. Read Microsoft documentation here to see the list of supported barcodes.

Let’s say we have Pallet ID = 9876543210123, and you want to print it as 1D or 2D Barcode. In order to print the barcode, there are two steps that you need to do: encode the barcode, and then use the barcode font to print it.

To encode the barcode, you will need to use Barcode Symbology enum and interface. I have already created 2 procedures below that you can use to print 1D and 2D barcode that you can use.

procedure Generate1DBarcodeSymbology(BarcodeSymbology: Enum "Barcode Symbology"; BarcodeString: Text[50]): Text
    var
        BarcodeFontProviderEnum: Enum "Barcode Font Provider";
        BarcodeFontProvider: Interface "Barcode Font Provider";
    begin
        BarcodeFontProvider := BarcodeFontProviderEnum::IDAutomation1D;
        BarcodeFontProvider.ValidateInput(BarcodeString, BarcodeSymbology);
        Exit(BarcodeFontProvider.EncodeFont(BarcodeString, BarcodeSymbology));
    end;
procedure Generate2DBarcodeSymbology(BarcodeSymbology2D: Enum "Barcode Symbology 2D"; BarcodeString: Text[50]): Text
    var
        BarcodeFontProvider2DEnum: Enum "Barcode Font Provider 2D";
        BarcodeFontProvider2D: Interface "Barcode Font Provider 2D";
    begin
        BarcodeFontProvider2D := BarcodeFontProvider2DEnum::IDAutomation2D;
        Exit(BarcodeFontProvider2D.EncodeFont(BarcodeString, BarcodeSymbology2D));
    end;

Let’s try using those procedures.

trigger OnAfterGetRecord()
var
   BarcodeSymbology: Enum "Barcode Symbology";
   BarcodeSymbology2D: Enum "Barcode Symbology 2D";
   PalletID, PalletBarcode, PalletBarcode2D : Text;
begin
   PalletID := '9876543210123';
   PalletBarcode := Generate1DBarcodeSymbology(BarcodeSymbology::Code128, PalletID);
   PalletBarcode2D := Generate2DBarcodeSymbology(BarcodeSymbology2D::"QR-Code", PalletID);
end;

The PalletBarcode and PalletBarcode2D now contain the correct encoded barcode text. What you need to do is to display the encoded text on report using Textbox and change the font to the correct one.

If you don’t have the font in your PC, you can just type the font from inside VS Code.

And there you go, the barcode will show up on your report.

If you don’t use the correct font, the barcode will look like garbled random text.

Incorrect font

You can read ID Automation 2D Universal Barcode Font Specification here. For 1D Barcode Font Specification, you need to go to each 1D font page, such as Code 128. Make sure you specify the correct font, otherwise it will not print correctly.

thatnavguy

Experienced NZ-based NAV Developer and Consultant with 15+ years of experience leading multiple IT projects, performing business analyst, developing, implementing, and upgrading Dynamics NAV and Business Central. Passionate to deliver solution that focuses on user-friendly interface while keeping high standard of compliance with the needs.

You may also like...

1 Response

  1. 18 January 2022

    […] Printing 1D and 2D Barcode […]

Leave a Reply

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