Community

A beginner's guide to ABBYY external Assemblies (C# .Net v4.5): Part 2 - Sample Code

Recently I started work on an external assembly to add to our FlexiCapture project, but had no idea where to start. After finally figuring it out, I decided to publish my results for other beginners to use.

I'll be releasing my notes in three parts, because there's a lot of notes haha

If you learned something useful from this, or have something you'd like to add please feel free to like, comment and favorite!



PART 2: Sample Code

In my last post, I outlined the requirements for creating an external assembly to house your FlexiCapture scripts. Here is a link to that discussion

Here I will provide some sample code for you to use as a starting point for your own assemblies. The following class is used for storing basic field manipulation rules, specifically for manipulating the captured text from an open-response field.

Below that class sample code is a sample of code that calls the methods defined above within a field's ABBYY Script Editor Window

 

1

Comments

3 comments

  • Avatar
    Matthew Harper

    Here's a sample class for basic text-field manipulation rules. Below, I will provide the code that calls the methods from this class

    extern alias aControllerInterlop; //be sure to use aliases! this line needs to be first
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using aControllerInterlop::ABBYY.FlexiCapture;
    namespace FCToolkit
    {
        public class TextRule
        {
            protected IRuleContext _fieldRuleContext;

            // object constructor
            public TextRule(IRuleContext context)
            {
                _fieldRuleContext = context;
            }       

            // Static method: Pads the field's text with leading zeroes
            public static void PadWithLeadingZeroes(IRuleContext ruleContext, int FinalCharacterCount)
            {
                string modifiedText = ruleContext.Fields[0].Text;
                int initialCharacterCount = modifiedText.Length;
                for (int cidx = initialCharacterCount; cidx < FinalCharacterCount; cidx++)
                {
                    modifiedText = "0" + modifiedText;
                }
                ruleContext.Fields[0].Text = modifiedText;
            }     

            // static method: passes values from a text block to other text fields
            public static void GetRegistrationFieldsFromBarcode(IRuleContext ruleContext)
            {
                // Field("Barcode") R
                // Field("DocumentId") RW
                // Field("SourceId") RW
                if (ruleContext.HasField("Barcode") && ruleContext.HasField("DocumentId") && ruleContext.HasField("SourceId"))
                {
                    string barcodeValue = ruleContext.Field("Barcode").Text;
                    ruleContext.Field("DocumentId").Text = barcodeValue.Substring(0, 9);
                    ruleContext.Field("SourceId").Text = barcodeValue.Substring(9, 1);
                }
                else if (ruleContext.HasField("DocumentId") && ruleContext.HasField("SourceId"))
                {
                    string barcodeValue = ruleContext.Field("DocumentId").Text;
                    ruleContext.Field("SourceId").Text = barcodeValue.Substring(9, 1);
                }
                else
                {
                    ruleContext.ErrorMessage = "A field or field alias is incorrectly named";
                }
            }

            public static void GetBatchIdFromCoverSheet(IRuleContext Context)
            {
                // Field("CoversheetImageSource") is a service field set to image source
                // Field("CoversheetImageSource") R
                // Field("BatchId") RW
                if (Context.Field("BCSImageSource").Text != "")
                {
                    string bcsImageSource = Context.Field("CoverSheetImageSource").Text;
                    Context.Field("BatchId").Text = bcsImageSource.Substring(bcsImageSource.Length - 31, 31 - 6);
                }
            }
        }
    }
    1
  • Avatar
    Matthew Harper

    To call one of these static methods in FlexiCapture, you first need to build the project and add it to a document definition's .Net References as an 'attached file'.

    Create a new rule within a text field and navigate to the script editor. Use the following script to invoke the class method:

    // this sample code requires the field script window to be set to c# .net
    FCToolkit.PadWithLeadingZeroes(Context, 3);

    // This method pads a text field with leading zeroes until it is 3 characters long
    // EX: 5 -> 005
    // EX: 23 -> 023
    // EX: 653 -> 653
    1
  • Avatar
    Matthew Harper

    For some reason, this code doesn't want to allow my html summaries for the methods in the comment window. I'll add a post detailing intellensense summaries in my third post on this topic.

    Also, if anybody knows how to prevent the text wrap here, please feel free to let me know

    0

Please sign in to leave a comment.