Reusing Code in .NET Scripts

Each script is stored in a workflow as an independent code fragment. In earlier versions that did not support .NET languages, users who wanted to reuse their existing code had to copy function definitions into each new script.

With the introduction of .NET support, you can write and distribute script code as compiled .NET assemblies. To reuse existing code, you simply have to configure your workflow to call the required methods from a .NET assembly.

Here is an example of how you can create a .NET assembly in Visual Studio:

  1. Create a new C# project of type Class Library and name it FineReaderServer.UserScripts.
  2. Add a reference into this assembly: C:\Program Files\ABBYY FineReader Server 14.0\Bin\FRS.ScriptingObjects.Interop.dll
  3. Add a new file named UserScripts.cs into your project containing the following code:

using System.IO;
using System.Linq;
using FineReaderServer.ScriptingObjects.Interop;
namespace FineReaderServer.UserScripts
{
   public static class Utils
   {
       public static string Join(string separator, IStringsCollection collection)
       {
           return string.Join(separator, collection.Cast<string>());
       }
   }
   public static class Extensions
   {
       public static string GetStringValue(this IAttribute attr)
       {
           switch(attr.Type)
           {
               case DocumentAttributeType.DAT_MultipleLines:
                   return Utils.Join(", ", attr.Value);
              case DocumentAttributeType.DAT_DateTime:
                   return attr.Value.ToString("s");
               default:
                   return attr.Value.ToString();
           }
       }
   }
   public static class DocumentAttributes
   {
       public static void SaveToFile(string path, IAttributes attributes)
       {
           using(var writer = File.CreateText(path))
           {
               foreach(IAttribute attribute in attributes)
               {
                   writer.WriteLine(string.Format("{0}: {1}", attribute.Name, attribute.GetStringValue()));
               }
           }
       }
   }
}

  1. Compile the project and add a reference to the resulting assembly into the Configuration.xml file.
  2. Create a new workflow and configure a document type in that workflow.
  3. Specify an export script as follows:

var format = doc.OutputFormats[0];
var outputFile = format.OutputFiles[0];
var attributesFile = System.IO.Path.Combine(format.OutputLocation, outputFile +".attributes.txt");
FineReaderServer.UserScripts.DocumentAttributes.SaveToFile(attributesFile, doc.Attributes);

  1. Submit a document for processing.
  2. Wait until the document arrives at the Indexing Station, specify its type and values of index fields, and click Accept. As a result, the document will be published together with a file named document.pdf.attributes.txt, which will contain the values of the index fields:

Title: Test document
Authors: Alice, Bob
Date:2019-07-22T19:40:36

20.09.2022 9:27:51

Please leave your feedback about this article

Usage of Cookies. In order to optimize the website functionality and improve your online experience ABBYY uses cookies. You agree to the usage of cookies when you continue using this site. Further details can be found in our Privacy Notice.