Reading Barcodes in KTA

Even without Transformation Server, TotalAgility is absolutely capable of reading barcodes during scan time. However, how can you access said barcodes and store them in a variable? This article will cover the creation of a small helper method in Visual Studio that calls the TotalAgility API in order to read barcodes, as well as its integration in a process using a .NET activity. Special thanks here to Ondřej Musil and Joseph Joy who helped me with getting on the right track.

Where are my Barcodes?

Barcodes are being stored in the Pages table in the TotalAgility_Documents database. Let’s assume the following scenario: in a Scan/Create New Job form, we process three pages using barcode separation (Code 128):

Here’s the Page table with the last three records being highlighted. As you can see, barcodes are stored as XML in a column with the same name.

Now let’s have a closer look at the XML. Not only does TotalAgility expose the value, but certain meta data as well such as the type and the coordinates.


<?xml version="1.0" encoding="utf-16"?>
<ArrayOfBarcode xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <Barcode>
 <Text>123457</Text>
 <Left>1102</Left>
 <Top>532</Top>
 <Width>337</Width>
 <Height>397</Height>
 <Orientation>LeftToRight</Orientation>
 <BarcodeType>Code128</BarcodeType>
 </Barcode>
</ArrayOfBarcode>

Using the API

Now that we know where barcodes are stored, let’s look at the API. Pages are related to Documents, and each Document can have 1:n Pages. The following method takes a document identifier (as well as the session id), returning a list of string values for each barcode present on the first and all subsequent pages.


public List GetDocumentBarcodes(string sessionId, string docId)
{
    List barcodes = new List();
    var cds = new CaptureDocumentService();
    var apiDoc = cds.GetDocument(sessionId, null, docId);

    foreach (var page in apiDoc.Pages)
    {
        foreach (var barcode in page.Barcodes)
        {
            barcodes.Add(barcode.Value);
        }
    }

    return barcodes;
}

Integrating the code in any process is easy – all we need is a (dynamic) complex variable to store barcode values, plus a .NET activity to call our method. The following process uses a folder variable to store documents in, loops over each document, reads the barcodes, and then loops over each found barcode again (although we only use the first iteration).

Configuring the .NET activity is straight-forward as well: 

That’s all we needed! The following screenshot shows the job halted at the STOP HERE activity as depicted above. Note the complex Barcodes variable holding all barcodes present on any page of the document.

Conclusion and Outlook

You may want to access barcodes this way if Transformation Services isn’t an option because you lack the appropriate licenses, or you simply just don’t want that kind of overhead. Reading barcodes via the API isn’t the only way – in fact, a Business Rule is sufficient to read the first barcode on every document (i.e. on the first page). However, it shows the powers of the API and how nicely integration can be done in KTA: the List collection is mapped just nicely with a (dynamic) complex variable.

What else could we do? Well, here are some ideas:

  • Create a Business Rule to read barcodes using the API. Imagine the method name or signatur changes – you’d only need to change one single rule instead of multiple processes
  • Call the API during scan time. It would be great if the user could see the values right in a Scan/Create New Job form.