ABBYY FlexiCapture Engine is a Software Development Kit (SDK) for extracting the data from different types of documents, such as PDF, images, or scanned documents, etc. It is one of the best solutions for developers to extract the text content from letters, invoices, forms, identity cards etc. Apart from this text capturing task, we can also perform the image processing ( Best Quality) task.
Note: ABBYY FlexiCapture Engine is not an open source application. You should have the license for using this application. You can get a trial license by sending the request via your business e-mail id.
FlexiLayout Studio
ABBYY FlexiLayout Studio is an application software through which you get text from structured, semi-structured and unstructured documents. The application contains the different tools for extracting the data from your document, such as - Block, Element, Group, Checkmark etc. Using this application, we can mark the area from where we have to get the data. You can export the AFL File (.afl extension), once you mark all the required text and values.
Getting text from any complex document, using FlexiLayout Studio.
- Install FlexiLayout Studio which you get from the ABBYY SDK technology.
- Open the application and create a project, as shown below.
- Add your file (Image, PDF, Scan etc.) using the Add Images button.
- Double-click on your file to display the file in the documents area.
- Create a document as a Training set.
- Now, let's suppose, we have to get Policy Number, Claim No, Insured Name etc. from the document. A FlexiLayout section contains Blocks and Elements as a mark which indicates the Marked area on your document. So, create the block and element whenever required.
- For creating an element, use the element tool to select the area that you want to capture by dragging the tool. For data separation, you can create a group and add element in created group, as the following image.
- Now, create a value of policy number. And, do the same for all the required data filed.
- Create a relation for an element for accessing the element value by its name in block section.
You have to create an element relation with all its value elements. And, the value element will be referenced by the block. This will be accessible by different programming languages, as C#, VB, C++ etc.
- Now, create a block for accessing the element.
You have to perform the above task for referencing all the blocks from an element. Following are the final element and block sets through which we will access the field value.
- Now, export the project and save AFL (Abby FlexiLayout) file with a name you want.
The above was the AFL project creation task to access the data from any type of the document. Now, we will use this AFL file in my C# programming to access the data.
Using FlexiLayout file in Windows or Web application to access the data.
- Create a Windows or Web application in which you want to access the data. Here, we are creating a Windows application.
- Open Visual Studio.
- Go to file menu and choose New Project.
- Select Windows form application, Type the name of project and click OK button.
- Design the form as the following. We are using two text boxes and one button to locate the the FlexiLayout file(.afl extension), and the document whose data we have to extract.
- Go to Code behind and add FCEngine.dll in your reference folder. (From ABBYY SDK which you get from ABBYY technology.)
- Write the following code in your code behind and run the application.
- using FCEngine;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Windows.Forms;
- namespace AflToDocDiff {
- public partial class Form1: Form {
- IEngine engine = null;
- IFlexiCaptureProcessor processor;
- IDocumentDefinition definition;
- IDocumentDefinition definition1;
- StringBuilder sb;
- private Dictionary < string, Dictionary < string, FieldParams >> fieldToType = new Dictionary < string, Dictionary < string, FieldParams >> ();
-
-
-
- public Form1() {
- InitializeComponent();
- }
- private IEngine loadEngine() {
- IEngine engine;
- int hresult = InitializeEngine(FceConfig.GetDeveloperSN(), out engine);
- Marshal.ThrowExceptionForHR(hresult);
- return engine;
- }
- private void unloadEngine(ref IEngine engine) {
- engine = null;
- int hResult = DeinitializeEngine();
- Marshal.ThrowExceptionForHR(hResult);
- }
- [DllImport(FceConfig.DllPath, CharSet = CharSet.Unicode), PreserveSig]
- internal static extern int InitializeEngine(String devSN, out IEngine engine);
- [DllImport(FceConfig.DllPath, CharSet = CharSet.Unicode), PreserveSig]
- internal static extern int DeinitializeEngine();
- class Gdi32 {
- [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
- internal static extern IntPtr DeleteObject(IntPtr hDc);
- }
- private void CreateDocumentDef() {}
-
-
- private void button1_Click(object sender, EventArgs e) {
- try {
- engine = loadEngine();
-
-
-
- string extension = Path.GetExtension(txtAflFilePath.Text);
- if (processor == null) {
- ICustomStorage storage = null;
- switch (extension) {
- case ".xfd":
- definition = engine.CreateDocumentDefinitionFromXFD(txtAflFilePath.Text, "English");
- storage = (ICustomStorage) definition;
- break;
- case ".afl":
- definition = engine.CreateDocumentDefinitionFromAFL(txtAflFilePath.Text, "English");
- storage = (ICustomStorage) definition;
- break;
- case ".fcdot":
- definition = engine.CreateDocumentDefinition();
- storage = (ICustomStorage) definition;
- storage.LoadFromFile(txtAflFilePath.Text);
- break;
- default:
- throw new NotImplementedException();
- }
- processor = engine.CreateFlexiCaptureProcessor();
- processor.AddDocumentDefinition(definition);
- } else {
- processor.ResetProcessing();
- processor = null;
- }
- var imageTools = engine.CreateImageProcessingTools();
- var file = imageTools.OpenImageFile(txtFormPath.Text);
- int pageCount = file.PagesCount;
- for (int i = 0; i < pageCount; i++) {
- processor.AddImage(file.OpenImagePage(i));
- }
- IDocument document = processor.RecognizeNextDocument();
- if (document != null && document.DocumentDefinition != null) {
- sb = new StringBuilder();
- buildDocumentView(document);
-
- }
- string desc = definition.Description;
- } catch (Exception ex) {
- string msg = ex.Message;
- } finally {
- unloadEngine(ref engine);
- processor = null;
- }
- }
- private void tuneRecognitionParams(string name, IDocumentDefinition definition) {
- if (fieldToType.ContainsKey(name)) {
- bool modified = false;
- var fields = definition.Sections[0].Fields;
- for (int i = 0; i < fields.Count; i++) {
- if (fieldToType[name].ContainsKey(fields[i].Name)) {
- var fieldParams = fieldToType[name][fields[i].Name];
- var textParams = fields[i].RecognitionParams.AsTextParams();
- if (fieldParams.Type == FieldValueTypeEnum.FVT_Text) {
- if (fieldParams.RegExp != null) {
- int pos = fieldParams.Params.IndexOf(' ');
- string letters = fieldParams.Params.Substring(0, pos);
- string regExp = fieldParams.Params.Substring(pos + 1);
- var newLanguage = textParams.CreateEmbeddedLanguage(LanguageTypeEnum.LT_Simple, null);
- newLanguage.AsSimpleLanguage().set_LetterSet(LanguageLetterSetEnum.LLS_Alphabet, letters);
- newLanguage.AsSimpleLanguage().RegularExpression = regExp;
- textParams.Language = newLanguage;
- }
- } else {
- var newLanguage = textParams.CreateEmbeddedLanguageByDataType(fieldParams.Type);
- textParams.Language = newLanguage;
- }
- textParams.TextType = fieldParams.TextType;
- textParams.CaseRecognitionMode = fieldParams.CaseType;
- modified = true;
- }
- }
- if (modified) {
- definition.Check();
- }
- }
- }
- private void button2_Click(object sender, EventArgs e) {
- DialogResult result = openFileDialog1.ShowDialog();
- if (result == DialogResult.OK)
- {
- txtAflFilePath.Text = openFileDialog1.FileName;
- }
- }
- private void button3_Click(object sender, EventArgs e) {
- DialogResult result = openFileDialog2.ShowDialog();
- if (result == DialogResult.OK)
- {
- txtFormPath.Text = openFileDialog2.FileName;
- }
- }
- private void buildDocumentView(IDocument document) {
- IField firstSection = document.Sections[0];
- addDocumentNodeChildren(firstSection.Children);
- }
- private void addDocumentNodeChildren(IFields children) {
- for (int i = 0; i < children.Count; i++) {
- addDocumentNode(children[i]);
- }
- }
- private void addDocumentNode(IField documentNode) {
- IFieldValue value = documentNode.Value;
- if (value == null) {
-
- } else {
- string keyval = documentNode.Name + ": " + documentNode.Value;
- sb.AppendLine(string.Format("{0}:{1}", documentNode.Name, value.AsString));
- string FormName = Convert.ToString(value.AsString);
- if (FormName.Contains("125")) {
- lblFormName.Text = "Accord Form 125";
- string str = FormName.Substring(0, 8);
- str = str.Split('(', ')')[1];
- } else if (FormName.Contains("126")) {
- lblFormName.Text = "Accord Form 126";
- } else if (FormName.Contains("140")) {
- lblFormName.Text = "Accord Form 140";
- } else {
- lblFormName.Text = "Please upload valid form.";
- }
- }
- lblFormName.Text = sb.ToString();
- if (documentNode.Instances != null) {
- addDocumentNodeInstances(documentNode.Instances);
- } else if (documentNode.Children != null) {
- addDocumentNodeChildren(documentNode.Children);
- }
- }
- private void addDocumentNodeInstances(IFieldInstances instances) {
- for (int i = 0; i < instances.Count; i++) {
- if (instances[i].Children != null) {
- addDocumentNodeChildren(instances[i].Children);
- }
- }
- }
- }
- }
Output