logo

Script for generating a barcode image inserted in a document template

This article provides a script for generating a barcode by a calculated value. For the script to work, you must go to Web Application and enable barcode generation in Administration - Document Management Document Management Section Settings - Barcode Generation Settings.

Example of barcode image generation

1. In a business process (fig. 1), create a Barcode context variable of the File type (System group) for storing the barcode image and a String variable of the String type for storing the string values of the barcode. For the script to work, you must fill in the barcode string value in Web Application. The String value should be displayed in the Enter String Value task.

Fig. 1. Graphic model of the Barcode Generation process

2. To generate a barcode image, add the following script:

Namespaces:

using EleWise.ELMA.Barcode;
using EleWise.ELMA.ComponentModel;
using EleWise.ELMA.Barcode.Models;
using EleWise.ELMA.Services;
using EleWise.ELMA.Logging;
using EleWise.ELMA.Files;
using EleWise.ELMA.Runtime.Managers;
using EleWise.ELMA.Services;
using EleWise.ELMA.Common.Managers;
using EleWise.ELMA.Model.Services;

Script text:

var settingsModule = Locator.GetService<BarcodeGenerationSettingsModule>();
            if (settingsModule != null && settingsModule.Settings != null && settingsModule.Settings.GenerateBarCode && !string.IsNullOrEmpty(settingsModule.Settings.ProviderKey))
{
                var provider = ComponentManager.Current.GetExtensionPoints<IBarcodeProvider>().FirstOrDefault(p => p.Key == settingsModule.Settings.ProviderKey);
                if (provider != null)
                {
                    BinaryFile barcodeFile;
                    try
                    { 
                        barcodeFile = provider.CreateBarcode(
                            new BarcodeInfo {
                                Code = context.String,
                                Width = settingsModule.Settings.Width,
                                Height = settingsModule.Settings.Height
                            });
                    } catch (Exception ex)
                    { 
                        Logger.Log.Error(SR.T("Failed to generate a barcode with dimensions {0}х{1}. The barcode is generated with the default parameters.", settingsModule.Settings.Width, settingsModule.Settings.Height), ex);
                        barcodeFile = provider.CreateBarcode(
                            new BarcodeInfo {
                                Code = context.String,
                                Width = 0,
                                Height = 0
                            });
                    }
                     
                    context. Barcode =InterfaceActivator.Create<BinaryFile>();
                    context.Barcode.Name = barcodeFile.Name;
context.Barcode.CreateDate = DateTime.Now;
context.Barcode.InitializeContentFilePath();
System.IO.File.Copy(barcodeFile.ContentFilePath, context.Barcode.ContentFilePath);
var attachment = PublicAPI.Portal.Objects.Common.Attachment.Create(context. Barcode);
PublicAPI.Services.File.SaveFile(context.Barcode);
}
            }

3. To display the barcode image in the document template (fig. 2) add the following code:

{Image({$Barcode};’scale:50’)}

where scale:50 - is the scale of the displayed image.

Fig. 2.  Document template

Select the earlier created Barcode variable (fig. 3).

Fig. 3.  Generate Document Version activity. Settings tab

The document, generated in the Generate Document Version activity will contain the barcode image (fig. 4).

Fig. 4. Barcode image in the generated document

KB links

Current list of template generation functions

Email address generation function for inserting in document and notification templates