Function for generating a barcode image, inserted in document templates

When creating documents from templates, it is possible to generate images (not only string and numerical values). This example illustrates barcode generation by a calculated value.

Note! You can use this function only when creating a document from a template. Do not use it for business process tasks and email notification templates.

Example of a document template


Example of a created document


Example of an extension class for the view of the template generator function

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using EleWise.ELMA.ComponentModel;
using EleWise.ELMA.Templates;
using EleWise.TemplateGenerator;
using EleWise.TemplateGenerator.Formats;
using EleWise.TemplateGenerator.Functions;
using ITino.ELMA.Documents.Barcode.Formats;

namespace ITino.ELMA.Documents.Barcode.Components
  /// <summary>
  /// Extension for the view of the template generator function
  /// </summary>
  public class TemplateGeneratorFunctionsContainer : ITemplateGeneratorFunctionsContainer
    /// <summary>
    /// Generation of a barcode image, using the ZXing library
    /// </summary>
    /// <param name="context">Context for calculating the function value</param>
    /// <returns>Barcode of the Image type</returns>
    public static FormatedValue Barcode(FunctionEvaluationContext context)
      if (context.Parameters.Count == 0)
        return (FormatedValue)null;

      BarcodeFormat barcodeFormat = new BarcodeFormat();
      if (context.Parameters.Count > 1)
        string s = context.Parameters[1] == null || context.Parameters[1].Value == null ? (string)null : context.Parameters[1].Value.ToString();
        if (!string.IsNullOrEmpty(s))

      // Barcode generation
      var bw = new ZXing.BarcodeWriter();
      bw.Format = barcodeFormat.Code;
      bw.Options.PureBarcode = !barcodeFormat.ShowText;
      if (barcodeFormat.Height != -1)
        bw.Options.Height = barcodeFormat.Height;
      if (barcodeFormat.Width != -1)
        bw.Options.Width = barcodeFormat.Width;
      if (barcodeFormat.Margin != -1)
        bw.Options.Margin = barcodeFormat.Margin;

      Bitmap bm = bw.Write(context.Parameters[0].Value.ToString());

      if (barcodeFormat.Aspect != 1)
        // Resize the barcode, if the scale is not 1
        bm = ResizeImage(bm, (int)(bm.Width * barcodeFormat.Aspect), (int)(bm.Height * barcodeFormat.Aspect));

      // Return the barcode as an image
      return new FormatedValue((Image)bm, (DrawingFormat)barcodeFormat);

    // Function for image resizing without quality loss
    private static Bitmap ResizeImage(Image image, int width, int height)
      var destRect = new Rectangle(0, 0, width, height);
      var destImage = new Bitmap(width, height);

      destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

      using (var graphics = Graphics.FromImage(destImage))
        graphics.CompositingMode = CompositingMode.SourceCopy;
        graphics.CompositingQuality = CompositingQuality.HighQuality;
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = SmoothingMode.HighQuality;
        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

        using (var wrapMode = new ImageAttributes())
          graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);

      return destImage;

The ITemplateGeneratorFunctionsContainer extension point (interface) defines the list of classes (components) which will be registered in the system when starting the server. When generating a document from a template, the static methods, implemented in this class, are systematically sorted out. If the system finds a match with the name of the required method, it is being invoked and the result is being calculated by the data of the input context variable and its parameters.

The public static FormatedValue Barcode(FunctionEvaluationContext context) method defines the processing of the input value of the context variable. If the context does not meet the criteria during processing (the value is absent, or empty string), then null is returned. As the result, the generated documents will not contain the barcode.

Otherwise, a barcode image is generated by the passed parameters using the ZXing library (included in ELMA installation software) and returned as the Image type to the generated document.

Links to API elements


Links to Knowledge Base

Current list of template generator functions