Scripting - C Sharp Validation Scripting Tips
Issue
In addition to the Kofax Capture Developer's Guide, is there any more information that can be provided to assist in developing a Validation script using the C# language?
Solution
Unlike VB.NET validation Scripting, there are extra steps that need to be done in a C# implementation. This article will attempt to address this issue by providing some simple steps to get started developing a simple Validation Script in C#.
Creating the Script
Right-click the Document Class and select Document Validation Script.
Note: there are now three selections, C#, VB.NET and SBL. Although still available as a scripting language, SBL was officially deprecated in Kofax Capture 10.1.
Select C# and press Create to open Visual Studio. Open the “.cs” code file. The window should contain initial code looking similar as below:
using Kofax.AscentCapture.NetScripting; using Kofax.Capture.CaptureModule.InteropServices; using System; using System.Collections.Generic; using System.Text; namespace TestDoc { [SuppressFieldEventsOnDocClose(false)] public class TestDoc : DocumentValidationScript { [IndexFieldVariableAttribute("Name0")] FieldScript Name0; public TestDoc(bool bIsValidation, string strUserID, string strLocaleName) : base(bIsValidation, strUserID, strLocaleName) {} } }
Previously in VB.NET, you would simply pull down the menus at the top of the code window, select the Document or Index Field then click the appropriate event you wanted to implement and the code would be automatically created. In C#, this process is more granular where the developer must perform these steps in a more manual fashion.
Refer to the above code. You are provided with one method which is the constructor for the class, in this case the “TestDoc” class. In this constructor, the BatchLoading, BatchUnloading, DocumentPreProcessing and DocumentPostProcessing events will need to be subscribed to and the corresponding event method implementations created. This is easily done by following these steps:
1. In the constructor method, type “this.” and select BatchLoading.
2. Follow by typing “+=” and you will see a message like:
TestDoc_BatchLoading (Press TAB to insert)
3. pressing the TAB key will automatically complete the declaration and present another message:
Press TAB to generate handler 'TestDoc_BatchLoading' in this class
4. Pressing the TAB key again will automatically generate the BatchLoading event method in the class.
5. Perform the same steps to subscribe to and generate the BatchUnloading, DocumentPreProcessing and DocumentPostProcessing event methods.
6. The following exception code will be automatically added to all event methods created in this manner. Remove this line:
throw new NotImplementedException();
7. The next task is to subscribe and implement the Index FieldPreProcessing, FieldPostProcessing and FieldFormatting events, also in the same manner as above. However, in this case, Field events need to be subscribed to in the BatchLoading event. Also, the Index Field name will be used in the declaration.
8. Shown below is an example of the class constructor and BatchLoading event after the above steps have been performed.
public TestDoc(bool bIsValidation, string strUserID, string strLocaleName) : base(bIsValidation, strUserID, strLocaleName) { this.BatchLoading += TestDoc_BatchLoading; this.BatchUnloading += TestDoc_BatchUnloading; this.DocumentPreProcessing += TestDoc_DocumentPreProcessing; this.DocumentPostProcessing += TestDoc_DocumentPostProcessing; } void TestDoc_BatchLoading(object sender, BatchEventArgs e) { this.Name0.FieldPreProcessing += Name0_FieldPreProcessing; this.Name0.FieldPostProcessing += Name0_FieldPostProcessing; this.Name0.FieldFormatting += Name0_FieldFormatting; }
10. And the rest of the resultant generated event methods:
void TestDoc_BatchUnloading(object sender, BatchEventArgs e) { } void TestDoc_DocumentPreProcessing(object sender, PreDocumentEventArgs e) { } void TestDoc_DocumentPostProcessing(object sender, PostDocumentEventArgs e) { } void Name0_FieldPreProcessing(object sender, PreFieldEventArgs e) { } void Name0_FieldPostProcessing(object sender, PostFieldEventArgs e) { } void Name0_FieldFormatting(object sender, FormatFieldEventArgs e) { }
11. The last requirement is to unsubscribe from the events. This is done in the BatchUnloading event method and the events need to be unsubscribed from in reverse order they were initially subscribed:
void TestDoc_BatchUnloading(object sender, BatchEventArgs e) { Name0.FieldFormatting -= Name0_FieldFormatting; Name0.FieldPostProcessing -= Name0_FieldPostProcessing; Name0.FieldPreProcessing -= Name0_FieldPreProcessing; DocumentPostProcessing -= TestDoc_DocumentPostProcessing; DocumentPreProcessing -= TestDoc_DocumentPreProcessing; BatchLoading -= TestDoc_BatchLoading; BatchUnloading -= TestDoc_BatchUnloading; }
Level of Complexity
Moderate
Applies to
Product | Version | Build | Environment | Hardware |
---|---|---|---|---|
Kofax Capture | All |