Custom Function: Generate a barcode

This function generates a barcode image from an input value. This could be used for a variety of purposes such as product labeling, event tickets, or shipping labels, to name a few. A variety of different barcode types are supported.

Important: This function uses a third-party library called the Barcode Image Generation Library to generate the barcodes. Copy the .dll file as needed to the Application\Bin folder, Scheduler folders, and WebService\Bin folder.

Arguments (Min - Max): 2 - 4


  • BarcodeLib.dll;
  • System.Drawing.dll


  • WebReports.Api.Common;
  • WebReports.Api.Reports;
  • BarcodeLib;
  • System.Drawing;
  • System.Drawing.Imaging


Barcode(value, type)

Barcode(value, type, cell)

Barcode(value, type, width, height)

  • value is the input barcode
  • type is the type of barcode (see Barcode Image Generation Library for the valid types)
  • Optional: cell is the index of the current cell, in the format "A#", where A is the letter of the cell row, and # is the number of the cell column. This scales the barcode to the cell size.
  • Optional: width and height are integer values for the width and height to set the barcode.


// Parse first argument for barcode value
int value;
if (!int.TryParse(args[0].ToString(), out value))
  return string.Format("Error: Value '{0}' must be an integer", args[0]);

// Parse second argument for barcode type
TYPE type = TYPE.CODE128;
if (!Enum.TryParse<TYPE>(args[1].ToString(), true, out type))
  return string.Format("Error: Invalid barcode type '{0}'", args[1]);

int width  = 200; // default
int height = 100; // default

// Third argument can be either a cell reference or a width value
// If fourth argument is set, parse third argument as width value
if (args.Length == 4)
  if (!int.TryParse(args[2].ToString(), out width))
    return string.Format("Error: Width '{0}' must be an integer", args[2]);
  if (!int.TryParse(args[3].ToString(), out height))
    return string.Format("Error: Height '{0}' must be an integer", args[3]);

// If fourth argument is not set, parse third argument as cell reference
if (args.Length == 3)
  string cell = args[2].ToString().ToLower();
  if (cell.Length < 2)
    return string.Format("Error: '{0}' must be in the form 'A1'", args[2]);

  // Parse as cell reference
  int colIdx = cell[0] - 97;     // Convert col char to index (A = 0, B = 1, ...)
  if (colIdx < 0 || colIdx > 25) // Make sure that it is a letter
    return string.Format("Error: '{0}' must be in the form 'A1'", args[2]);

  int rowIdx;
  if (!int.TryParse(cell.Substring(1), out rowIdx))
    return string.Format("Error: '{0}' must be in the form 'A1'", args[2]);
  rowIdx--; // Rows are 0 indexed

  // Check that the cell exists
  if (sessionInfo.Report.Cells.GetCell(rowIdx, colIdx) == null)
    return string.Format("Error: Cell '{0}' does not exist", args[2]);

  // Set barcode width and height to that of the cell 
  width  = sessionInfo.Report.Columns[colIdx].Width;
  height = sessionInfo.Report.Rows   [rowIdx].Height;

// Create the barcode object
Barcode barcode = new Barcode()
  Width        = width,
  Height       = height,
  ForeColor    = Color.Black,
  BackColor    = Color.White,
  Alignment    = AlignmentPositions.CENTER,
  IncludeLabel = true,
  ImageFormat  = ImageFormat.Png

// Generate the barcode
barcode.Encode(type, value.ToString());

// Return the barcode as an image
return new UdfLoadImageData()
  ImageTag  = Base64Encoding.Encode(barcode.GetImageData(SaveTypes.PNG)),
  ImageType = UdfLoadImageType.Db

Hidden Article Information

Article Author
Exago Development
created 2016-08-17 17:28:27 UTC
updated 2017-08-08 20:59:15 UTC

functions, example, custom function, bar, code,
Have more questions? Submit a request