Dynamics 365 Business Central: Generate barcode through external services/APIs (Telepen, Swiss QR Code, QR codes with colors, images or text……)

Dynamics 365 Business Central

Hi, Readers.
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. More details:

This is a standard function provided by Microsoft. We can simply add it in the extension using AL. However, if the customer requires a QR code in a special format, such as a special size, background color, etc., the standard cannot do it. For example,

At this time we can use some external services/APIs. Here is the API I used for this test:
QR Code API: QuickChart supports QR code generation. Generate a QR code like this: https://quickchart.io/qr?text=Here’s my text

QR code parameters: Build your customized QR code using the following query parameters. You may also use the web-based QR code builder.

Parameter NameDescriptionRequired?Default value
textContent of the QR code (can be a URL or any other string)Yes
formatFormat of QR code, either png, svg, or base64png
marginWhitespace around QR image4
sizeWidth and height dimension of the image150
darkHex color code of “dark” QR grid cells000000 (black)
lightHex color code of “light” QR grid cellsffffff (white)
ecLevelError correction level (L, M, Q, H)M (H if center image is present)
centerImageUrlURL of image to show in the center. Must be URL-encoded.
centerImageSizeRatioHow much space to take up, between 0.0 and 1.00.3
centerImageWidthWidth of center image in pixels
centerImageHeightHeight of center image in pixels
captionCaption text to display below the QR code.
captionFontFamilyFont family of the caption text‘sans-serif’
captionFontSizeFont size of the caption text in pixels.10
captionFontColorColor caption text, color names or hex code.black

Here’s the same code as above but URL encoded with slimmer margins, more error protection, colors, and in SVG format: https://quickchart.io/qr?text=Here’s%20my%20text&dark=f00&light=0ff&ecLevel=Q&format=svg

Let’s look at a simple example of utilizing this API from AL: Generate a QR Code from the Item Number and Item Description, and then save the QR Code in the Item Picture. (Of course, you can also create a new media field or blob field to save the QR Code)

In addition, we can also add an image to the QR Code, which is also impossible in the standard. (Only the URL is different)

BarcodeAPIURL := 'https://quickchart.io/qr?text=' + Rec."No." + '_' + Rec.Description + '&centerImageUrl=https://yzhums.com/wp-content/uploads/2025/05/Snipaste_2025-05-19_14-42-11.png';

And we can also add some instructions below the QR Code.

BarcodeAPIURL := 'https://quickchart.io/qr?text=' + Rec."No." + '_' + Rec.Description + '&caption=TextBelowQr&captionFontFamily=mono&captionFontSize=20'; //Text below QR code

Source code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

pageextension 50112 ItemCardExt extends "Item Card"
{
    actions
    {
        addfirst(processing)
        {
            action(GenerateQRCode)
            {
                ApplicationArea = All;
                Caption = 'Generate QR Code';
                Image = CreateDocument;
                Promoted = true;
                PromotedCategory = Process;
                trigger OnAction()
                var
                    BarcodeAPIURL: Text;
                    Client: HttpClient;
                    HttpResponseMsg: HttpResponseMessage;
                    HttpRequestMsg: HttpRequestMessage;
                    InS: InStream;
                    OverrideImageQst: Label 'The existing picture will be replaced. Do you want to continue?';
                    MustSpecifyDescriptionErr: Label 'You must add a description to the item before you can import a picture.';
                    FileName: Text;
                begin
                    if Rec.Description = '' then
                        Error(MustSpecifyDescriptionErr);
                    if Rec.Picture.Count > 0 then
                        if not Confirm(OverrideImageQst) then
                            Error('');
                    FileName := Rec.Description + '.png';
                    //BarcodeAPIURL := 'https://quickchart.io/qr?text=' + Rec."No." + '_' + Rec.Description + '&dark=f00&light=0ff&ecLevel=Q&format=png';// Here's the same code as above but URL encoded with slimmer margins, more error protection, colors, and in png format
                    BarcodeAPIURL := 'https://quickchart.io/qr?text=' + Rec."No." + '_' + Rec.Description + '&centerImageUrl=https://yzhums.com/wp-content/uploads/2025/05/Snipaste_2025-05-19_14-42-11.png';// Add an images in QR codes
                    //BarcodeAPIURL := 'https://quickchart.io/qr?text=' + Rec."No." + '_' + Rec.Description + '&caption=TextBelowQr&captionFontFamily=mono&captionFontSize=20'; //Text below QR code
                    HttpRequestMsg.SetRequestUri(BarcodeAPIURL);
                    if Client.Send(HttpRequestMsg, HttpResponseMsg) then begin
                        if HttpResponseMsg.IsSuccessStatusCode() then begin
                            HttpResponseMsg.Content.ReadAs(InS);
                            Clear(Rec.Picture);
                            Rec.Picture.ImportStream(InS, FileName);
                            Rec.Modify(true);
                        end;
                    end else
                        Error('Failed to send request to the API.');
                end;
            }
        }
    }
}

Of course, we can also generate other types of barcodes through external services/APIs.

Barcode API: https://quickchart.io/barcode?type=datamatrix&text=foo
Supported Parameters:

ParameterRequiredDescriptionOptions
typeYesSpecifies the type of barcode to generate (see list below).
textYesThe data or text you want to encode in the barcode.
widthNoDesired width of the barcode in pixels.
heightNoDesired height of the barcode in pixels.
scaleNoA scaling factor for the barcode, affecting both width and height.
includeTextNoIf set, includes human-readable text (if supported by the barcode type).truefalse
rotateNoControls the rotation of the barcode.N (Normal), R (Right), L (Left), I (Inverted)

Supported Barcode Types: Please note that some barcodes have limitations such as length or only support numbers. You can test it by url first, for example, https://quickchart.io/barcode?type=datamatrix&text=Custom%20Size&width=300&height=300

Barcode TypeDescription
auspostAusPost 4 State Customer Code
azteccodeAztec Code
azteccodecompactCompact Aztec Code
aztecruneAztec Runes
bc412BC412
channelcodeChannel Code
codablockfCodablock F
code11Code 11
code128Code 128
code16kCode 16K
code2of5Code 25
code32Italian Pharmacode
code39Code 39
code39extCode 39 Extended
code49Code 49
code93Code 93
code93extCode 93 Extended
codeoneCode One
coop2of5COOP 2 of 5
daftCustom 4 state symbology
databarexpandedGS1 DataBar Expanded
databarexpandedcompositeGS1 DataBar Expanded Composite
databarexpandedstackedGS1 DataBar Expanded Stacked
databarexpandedstackedcompositeGS1 DataBar Expanded Stacked Composite
databarlimitedGS1 DataBar Limited
databarlimitedcompositeGS1 DataBar Limited Composite
databaromniGS1 DataBar Omnidirectional
databaromnicompositeGS1 DataBar Omnidirectional Composite
databarstackedGS1 DataBar Stacked
databarstackedcompositeGS1 DataBar Stacked Composite
databarstackedomniGS1 DataBar Stacked Omnidirectional
databarstackedomnicompositeGS1 DataBar Stacked Omnidirectional Composite
databartruncatedGS1 DataBar Truncated
databartruncatedcompositeGS1 DataBar Truncated Composite
datalogic2of5Datalogic 2 of 5
datamatrixData Matrix
datamatrixrectangularData Matrix Rectangular
datamatrixrectangularextensionData Matrix Rectangular Extension
dotcodeDotCode
ean13EAN-13
ean13compositeEAN-13 Composite
ean14GS1-14
ean2EAN-2 (2 digit addon)
ean5EAN-5 (5 digit addon)
ean8EAN-8
ean8compositeEAN-8 Composite
flattermarkenFlattermarken
gs1-128GS1-128
gs1-128compositeGS1-128 Composite
gs1-ccGS1 Composite 2D Component
gs1datamatrixGS1 Data Matrix
gs1datamatrixrectangularGS1 Data Matrix Rectangular
gs1dldatamatrixGS1 Digital Link Data Matrix
gs1dlqrcodeGS1 Digital Link QR Code
gs1dotcodeGS1 DotCode
gs1northamericancouponGS1 North American Coupon
gs1qrcodeGS1 QR Code
hanxinHan Xin Code
hibcazteccodeHIBC Aztec Code
hibccodablockfHIBC Codablock F
hibccode128HIBC Code 128
hibccode39HIBC Code 39
hibcdatamatrixHIBC Data Matrix
hibcdatamatrixrectangularHIBC Data Matrix Rectangular
hibcmicropdf417HIBC MicroPDF417
hibcpdf417HIBC PDF417
hibcqrcodeHIBC QR Code
iata2of5IATA 2 of 5
identcodeDeutsche Post Identcode
industrial2of5Industrial 2 of 5
interleaved2of5Interleaved 2 of 5 (ITF)
isbnISBN
ismnISMN
issnISSN
itf14ITF-14
japanpostJapan Post 4 State Customer Code
kixRoyal Dutch TPG Post KIX
leitcodeDeutsche Post Leitcode
mailmarkRoyal Mail Mailmark
mandsMarks & Spencer
matrix2of5Matrix 2 of 5
maxicodeMaxiCode
micropdf417MicroPDF417
microqrcodeMicro QR Code
msiMSI Modified Plessey
onecodeUSPS Intelligent Mail
pdf417PDF417
pdf417compactCompact PDF417
pharmacodePharmaceutical Binary Code
pharmacode2Two-track Pharmacode
planetUSPS PLANET
plesseyPlessey UK
posicodePosiCode
postnetUSPS POSTNET
pznPharmazentralnummer (PZN)
qrcodeQR Code
rationalizedCodabarCodabar
rawCustom 1D symbology
rectangularmicroqrcodeRectangular Micro QR Code
royalmailRoyal Mail 4 State Customer Code
sscc18SSCC-18
swissqrcodeSwiss QR Code
symbolMiscellaneous symbols
telepenTelepen
telepennumericTelepen Numeric
ultracodeUltracode
upcaUPC-A
upcacompositeUPC-A Composite
upceUPC-E
upcecompositeUPC-E Composite

Let’s look at another simple example, users can select one of five barcode types (‘QR Code, Data Matrix, Aztec, Telepen, Swiss QR Code’) and save it in the item picture.

Test video:

Source code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

pageextension 50112 ItemCardExt extends "Item Card"
{
    actions
    {
        addfirst(processing)
        {
            action(GenerateQRCode)
            {
                ApplicationArea = All;
                Caption = 'Generate QR Code';
                Image = CreateDocument;
                Promoted = true;
                PromotedCategory = Process;
                trigger OnAction()
                var
                    BarcodeAPIURL: Text;
                    Client: HttpClient;
                    HttpResponseMsg: HttpResponseMessage;
                    HttpRequestMsg: HttpRequestMessage;
                    InS: InStream;
                    OverrideImageQst: Label 'The existing picture will be replaced. Do you want to continue?';
                    MustSpecifyDescriptionErr: Label 'You must add a description to the item before you can import a picture.';
                    FileName: Text;
                    BarcodeTypes: Label 'QR Code, Data Matrix, Aztec, Telepen, Swiss QR Code';
                    Selection: Integer;
                begin
                    if Rec.Description = '' then
                        Error(MustSpecifyDescriptionErr);
                    if Rec.Picture.Count > 0 then
                        if not Confirm(OverrideImageQst) then
                            Error('');
                    FileName := Rec.Description + '.png';
                    Selection := StrMenu(BarcodeTypes);
                    case
                        Selection of
                        1:
                            BarcodeAPIURL := 'https://quickchart.io/barcode?type=' + 'qrcode' + '&text=' + Rec."No." + '_' + Rec.Description + '&width=200&height=200&format=png';
                        2:
                            BarcodeAPIURL := 'https://quickchart.io/barcode?type=' + 'datamatrix' + '&text=' + Rec."No." + '_' + Rec.Description + '&width=200&height=200&format=png';
                        3:
                            BarcodeAPIURL := 'https://quickchart.io/barcode?type=' + 'azteccode' + '&text=' + Rec."No." + '_' + Rec.Description + '&width=200&height=200&format=png';
                        4:
                            BarcodeAPIURL := 'https://quickchart.io/barcode?type=' + 'telepen' + '&text=' + Rec."No." + '_' + Rec.Description + '&width=200&height=200&format=png';
                        5:
                            BarcodeAPIURL := 'https://quickchart.io/barcode?type=' + 'swissqrcode' + '&text=' + Rec."No." + '_' + Rec.Description + '&format=png';
                    end;
                    HttpRequestMsg.SetRequestUri(BarcodeAPIURL);
                    if Client.Send(HttpRequestMsg, HttpResponseMsg) then begin
                        if HttpResponseMsg.IsSuccessStatusCode() then begin
                            HttpResponseMsg.Content.ReadAs(InS);
                            Clear(Rec.Picture);
                            Rec.Picture.ImportStream(InS, FileName);
                            Rec.Modify(true);
                        end;
                    end else
                        Error('Failed to send request to the API.');
                end;
            }
        }
    }
}

Very simple. You can also add a QR code to a report using the following methods.

Also, please note that some of these external services are not completely free, and you need to pay attention to the instructions on these websites. For example, QuickChart Pricing

You can also try other external services, such as the two below. If your country has better services, you can use them, not limited to those mentioned in this post. But please note that the structure and keywords of the URL may be different, the method is the same. Give it a try!!!😁

  1. https://orcascan.com/guides/free-barcode-image-api-0e4a4fa6
  2. https://barcode.tec-it.com/en/Code11?data=0123-4567

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL