> ## Documentation Index
> Fetch the complete documentation index at: https://docs.abbyy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Recognizing Invoices with ABBYY FlexiCapture SDK

As flexible documents, invoices have fields whose design, number, and layout may vary significantly from document to document. You can use ABBYY FlexiCapture SDK to extract information from invoices.

## Using a preconfigured FlexiCapture invoice project

ABBYY FlexiCapture SDK provides several preconfigured FlexiCapture invoice projects containing Document Definitions optimized for invoices from different countries. You can find the sample projects in **SampleInvoiceProjects** in the **Samples** folder (Start > Programs > ABBYY FlexiCapture 12 SDK > Code Samples Folder). Alternatively, you can create an empty project with predefined countries and a Document Definition configured for one of the countries:

* Australia or New Zealand
* Canada
* European Union
* Japan
* Spain
* United States of America

You can also use a FlexiCapture invoice project created and set up in FlexiCapture Developer's Package. In this case, see [help.abbyy.com](http://help.abbyy.com/en-us/flexicapture/12/invoice_reader/ir_titletopic) for reference.

## Setting up a new project

Invoice projects are set up the same way as [ordinary FlexiCapture projects](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/tutorial-overview.md). However, you may want to do some additional setting for the best recognition quality.

1. Create an empty invoice project using the [**CreateInvoiceProject**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/engine-createinvoiceproject.md) method or open an existing one using the [**OpenProject**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/engine-openproject.md) method of the **Engine** object (see also [Opening the Project](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/engine-openingproject.md)).

2. Load the business units and vendors data sets.
   1. Get the [**DocumentDefinition**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/documentdefinition.md) from the [**DocumentDefinitions**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/documentdefinitions.md) collection of your [**Project**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/project.md). Switch the Document Definition to editing mode by calling the [**CheckOut**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/documentdefinitions-checkout.md) method of the **DocumentDefinitions** object.
   2. Use the [**Find**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/datasets-find.md) method of the [**DataSets**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/datasets.md) object to retrieve the "Vendors" and "BusinessUnits" data sets.
   3. Populate the data sets with the information about your vendors and business units. Use either the [**AddNew**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/datasets-addnew.md) method and the [**Field**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/datasettablerecord-field.md) property of the [**DataSetTableRecords**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/datasettablerecords.md) object to add records one by one, or fill the data set in a single operation with the [**UpdateFromDB**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/datasettablerecords-updatefromdb.md) method.\
      Note: Using **UpdateFromDB** requires a configured connection to your external database. You can set up the connection in FlexiCapture Developer's Package.
   4. Save the changes you made to the data sets by calling the [**Commit**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/datasettablerecords-commit.md) method of the **DataSetTableRecords** object. Call the [**CheckIn**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/documentdefinitions-checkin.md) method of the **DocumentDefinitions** object.

      > [!NOTE]
      >
      > Since Release 1 Update 4, some information about vendors and business units will be detected even if no database is connected. We use machine learning to detect company names and addresses and recognize tax IDs and IBANs because of their distinctive formats.

3. You may also wish to specify [additional options](#additional-options) for best results.

## Extracting data from invoices

1. Create a batch and [add images](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/engine-addingimages.md) to it.
2. [Recognize the images](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/engine-recognizing.md). During recognition, the program will apply the Document Definition and extract the field values.
3. Work with the extracted data. You can go through all the fields that were found and retrieve their values (see [Working with Recognized Data](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/workingwithrecognizeddata.md)). If you find that a field was detected incorrectly, you can set the correct region and train the recognizer to extract this field more accurately in future (see [Fields Extraction Training](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/fieldsextractiontraining.md)).
4. [Export the results](https://docs.abbyy.com/flexi-capture/sdk/guided-tour/engine-exporting.md).

> [accordion] C# code
>
> ```csharp theme={null}
> IProject project = engine.CreateInvoiceProject(projectPath,
>     InvoiceProjectTypeEnum.IPT_EuropeanUnion);
>
> IDocumentDefinitions documentDefinitions = project.DocumentDefinitions;
> IDocumentDefinition documentDefinition = documentDefinitions[0];
> documentDefinitions.CheckOut(documentDefinition);
>
> // Get the records of Vendors data set and fill it
> IDataSet vendorsDataSet = documentDefinition.DataSets.Find("", "Vendors");
> IDataSetTableRecords records = vendorsDataSet.TableRecords;
> IDataSetTableRecord record = records.AddNew();
> record.Field["Name"] = "The Hotel";
> record.Field["CountryCode"] = "FR";
>
> // Commit the changes
> records.Commit(null);
> documentDefinitions.CheckIn(documentDefinition);
>
> // Create a batch with images for recognition
> IBatch workingBatch = project.Batches.AddNew();
> workingBatch.Open();
> workingBatch.AddImage("ImagePath");
> workingBatch.Recognize(null, RecognitionModeEnum.RM_ReApplyDocumentDefinitions, null);
> workingBatch.Export(null, null);
> workingBatch.Close();
> project.Close();
> ```

## Processing invoices without data sets

The process of capturing invoices can be carried out without "Vendors" and "BusinessUnits" data sets. In this case, you need to train the fields using the clustering feature.

1. Create an empty invoice project using the [**CreateInvoiceProject**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/engine-createinvoiceproject.md) method or open a project without data sets using the [**OpenProject**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/engine-openproject.md) method of the **Engine** object.
2. Enable clustering feature using the [**InvoiceFeatures**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/invoicesettings.md#invoicefeatures) property of the [**InvoiceSettings**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/invoicesettings.md) object.
3. Add a new training batch using the [**AddNew**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainingbatches-addnew.md) method of the [**FieldsExtractionTrainingBatches**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainingbatches.md) object. Pass NULL as a value of the *Record* parameter instead of the vendor record.
4. Add some documents for training. On each page, find the fields that you are going to train, for example, the *Total* field. Note that you cannot train the vendor and business unit fields.
5. Get the [**FieldsExtractionTrainer**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainer.md) object using the [**FieldsExtractionTrainer**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainingbatch.md#fieldsextractiontrainer) property of the [**FieldsExtractionTrainingBatch**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainingbatch.md) object.
6. Train fields extraction by calling the [**Train**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/fieldsextractiontrainer-train.md) method of the trainer object.
7. Now you can recognize the documents from the working batch similar to those used in training calling the [**Recognize**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/batch-recognize.md) method of the [**Batch**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/batch.md) object. The fields you have trained should be detected and extracted more precisely.

## Additional options

#### Countries and languages

A large list of predefined countries and languages is available for invoices recognition, but if you do not find the country you need in the list, you can add it. Use the [**ICountries::AddNew**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/countries-addnew.md) method, then specify the country name and ISO alpha-2 code in the [**Name**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/country.md#name) and [**Alpha2Code**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/country.md#alpha2code) properties of the new [**Country**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/country.md) object. Add the languages which are used in the invoices originating in this country with the help of the [**AddRecognitionLanguage**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/country-addrecognitionlanguage.md) method. If the language has not been already present in the [**InvoiceLanguages**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/invoicelanguages.md) collection, it will be added there as well, and you will be able to edit its invoice-related properties, such as keyword lists. You can also set up a default country from a list of predefined countries in case the country is not automatically detected. To do this, use the [**DefaultCountry**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/invoicesettings.md#defaultcountry) property of the [**InvoiceSettings**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/invoicesettings.md) object.

> [accordion] C# code
>
> ```csharp theme={null}
> documentDefinitions.CheckOut(documentDefinition);
> IInvoiceSettings invoiceSettings = documentDefinition.InvoiceSettings;
> ICountry country = invoicesSettings.Countries.AddNew();
> country.Alpha2Code = "MC";
> country.Name = "SomeCountry";
> country.AddRecognitionLanguage("German");
>
> documentDefinitions.CheckIn(documentDefinition);
> ```

#### Keywords

Every predefined country and language for invoice recognition contain keywords for specific fields. Once a keyword is recognized on the image, the processing engine searches for the corresponding field nearby. If the keywords found on your invoices are not listed, we recommend adding them for the better quality of field extraction. The country-specific keywords can be filled in various properties of the [**Country**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/country.md) object, while the language-specific keywords are accessed via the [**KeywordLists**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/invoicelanguage.md#keywordlists) property of the [**InvoiceLanguage**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/invoicelanguage.md) object. The set of keyword lists is predefined, as they are connected to available invoice fields.

> [accordion] C# code
>
> ```csharp theme={null}
> documentDefinitions.CheckOut(documentDefinition);
>
> country.BankAccountKeywords = "Keyword_1|Keyword_2|Keyword_3";
> IInvoiceLanguage someLanguage = invoicesSettings.InvoiceLanguages[0];
> IKeywordLists keywordLists = someLanguage.KeywordLists;
> foreach (IKeywordList someKeywordList in keywordLists)
> {
>     if (someKeywordList.Name == "InvoiceNumberHighConfidenceLabels")
>     {
>         someKeywordList.Keywords = someKeywordList.Keywords + "|Invoice#";
>     }
> }
>
> documentDefinitions.CheckIn(documentDefinition);
> ```

#### Additional fields

Some of the additional fields that can be recognized on an invoice are not turned on by default: for example, delivery date. You can manage these with the help of the [**InvoiceFeatures**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/invoicefeatures.md) object.

> [accordion] C# code
>
> ```csharp theme={null}
> documentDefinitions.CheckOut(documentDefinition);
>
> IInvoiceFeatures invoiceFeatures = invoicesSettings.InvoiceFeatures;
> IInvoiceFeature someFeature = invoiceFeatures[0];
> bool isEnabled = someFeature.IsEnabled;
> // For example: Line items extraction
> string featureName = someFeature.Name;
>
> documentDefinitions.CheckIn(documentDefinition);
> ```

#### Line items

In addition to capturing data from the main fields of an invoice (number, date, vendor, business unit, etc.) you may need to capture data from repeatable fields too, i.e. fields with several instances. These fields occur several times in the invoice. In ABBYY FlexiCapture SDK a group of repeatable fields of the invoice are represented by *line items*. To access the recognized data of the line items, you should:

1. Access the instances of the field using the [**Instances**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/field.md#instances) property of the corresponding [**Field**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/field.md) object.
2. Get child fields of the instance using the [**Children**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/field.md#children) property of the **Field** object.
3. Now we can get the value of the specified field using the [**Value**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/field.md#value) property of the **Field** object.

   > [!NOTE]
   >
   > To work with the line items, you must turn on the Line item extraction feature with the help of the [**InvoiceFeatures**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/invoicefeatures.md) object.

During line items verification, use the [**ContinueLineItems**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/document-continuelineitems.md) method of the [**Document**](https://docs.abbyy.com/flexi-capture/sdk/api-reference/document.md) object when many line items together were extracted incorrectly. Have the operator to mark up the first line item manually. Then call this method to detect the other line items using the same pattern. Note that this is only possible if the country of vendor or purchaser was detected.

## See also

[Field Extraction Training Objects](https://docs.abbyy.com/flexi-capture/sdk/api-reference/training-overview.md)

[Invoice Processing Objects](https://docs.abbyy.com/flexi-capture/sdk/api-reference/invoiceprocessing-overview.md)
