> ## 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.

# How results from different FlexiLayouts are merged

> Understand how FlexiCapture for Invoices merges field regions from the main FlexiLayout, additional FlexiLayouts, and trained FlexiLayout variants for each vendor.

This article describes the order in which FlexiLayouts are matched with documents and how field regions are selected.

FlexiCapture for Invoices can use several types of FlexiLayout when matching the Document Definition:

* A **generic FlexiLayout**, which is used to process all invoices and which may itself include two subtypes:
  * A **main FlexiLayout**, used to detect the standard predefined invoice fields. This FlexiLayout is provided with FlexiCapture for Invoices and cannot be modified.
  * An **additional FlexiLayout**, used to detect user-defined fields and standard invoice fields whose detection logic has been changed. This FlexiLayout is [created by the user](/flexi-capture/invoice-reader/ir-fls-special-facilities).
* **FlexiLayout variants**, which are used to process invoices from specific vendors. FlexiLayout variants can detect any fields defined in their respective Document Definition and can be created by the user or by FlexiCapture for Invoices when it is trained on invoices from specific vendors.

After matching the Document Definition with an invoice, the program obtains a set of field regions that come from the different FlexiLayouts included in the Document Definition.

## Stage 1: Match the generic FlexiLayout

### Apply the main FlexiLayout

First, the program applies the main FlexiLayout. The invoice image is pre-recognized using the pre-recognition parameters specified in the Document Definition:

* The languages are specified under **Countries and Languages** on the **Document Definition Settings** tab of the **Document Definition Properties** dialog box.
* The pre-recognition mode (**Fast**, **Balanced**, **Normal**, or **Accurate**) is specified on the **FlexiLayout** tab of the **Document Definition Properties** dialog box.

As a result of applying the main FlexiLayout, the program obtains:

* The ID of the vendor and the ID of the business unit, if detected.
* The regions of the standard fields. For more information, see [Captured fields](/flexi-capture/invoice-reader/ir-extractable-fields).

### Apply the additional FlexiLayout

Next, the program applies the **additional FlexiLayout**, if it is available in the generic FlexiLayout.

The invoice image is pre-recognized using the pre-recognition parameters (the languages and the pre-recognition mode) specified in the FlexiLayout. We recommend specifying the same pre-recognition parameters in the additional FlexiLayout as in the Document Definition. In this case, the program uses the pre-recognition result obtained when applying the main FlexiLayout instead of pre-recognizing the invoice twice.

If your invoices contain some unusual fields, you can specify different pre-recognition parameters in the additional FlexiLayout, but this slows down processing.

As a result of applying the additional FlexiLayout, the program obtains the regions of all the fields defined in the additional FlexiLayout. These may be either standard invoice fields whose detection logic had to be changed, or additional fields not defined in the main FlexiLayout.

### Generate the aggregate result

Next, the program generates the aggregate result of applying the generic FlexiLayout. If the Document Definition includes an additional FlexiLayout, the program merges the results from the main FlexiLayout with those from the additional FlexiLayout at this stage.

The fields are identified by their names. The result is a collection of unique fields derived from the main and additional FlexiLayouts. If there are fields with identical names in the main and additional FlexiLayout, the program uses the region from the additional FlexiLayout.

This approach allows you to define new fields or change the logic of capturing any of the standard invoice fields.

## Stage 2: Apply FlexiLayout variants

If the program detected the vendor by applying the generic FlexiLayout in stage 1 and there is a **FlexiLayout variant** for this vendor, this FlexiLayout variant is applied at this stage.

The pre-recognition parameters from the Document Definition are used. These are the same pre-recognition parameters that were used when matching the main FlexiLayout included in the generic FlexiLayout. Therefore, no additional pre-recognition is required.

After matching the FlexiLayout variant with the invoice, the program obtains the regions of all the fields defined in the FlexiLayout variant for this specific vendor. If a FlexiLayout variant is obtained by training the program on a variety of invoices, it includes all the fields defined in the Document Definition. If a FlexiLayout variant is created manually, only those fields whose detection requires actions specific to the given vendor should be left in the variant.

## Stage 3: Merge the results

At this step, the results obtained by matching the **generic FlexiLayout** are merged with the results obtained by matching the **FlexiLayout variant**.

If the FlexiLayout variant was obtained by training the program on a variety of invoices, it includes all the fields defined in the Document Definition. The Document Definition stores information about the fields whose regions had to be changed by the user during training, that is, the fields that were detected poorly by the generic FlexiLayout. The program uses this information to decide which field regions to take from the generic FlexiLayout and which to take from the FlexiLayout variant trained on this vendor's invoices.

<Note>
  The regions of the **Amount** fields are selected differently. When applying the generic FlexiLayout, the program specifies the level of confidence with which the regions of the **Amount** fields have been detected. If the regions are detected unreliably, the **Total** field requires verification. If there is a FlexiLayout variant trained on invoices from the given vendor, and the generic FlexiLayout fails to detect the **Amount** fields with a sufficient level of confidence or does not detect them at all, the regions of the **Amount** fields detected by the FlexiLayout variant are used. You can also configure the program to always use the regions of the **Amount** fields detected by the FlexiLayout variant. To do this, set the value of the `HKEY_CURRENT_USER\Software\ABBYY\FlexiCapture\12.0\DAForms\UseTrainedInvoiceAmounts` registry key to `true` (the key is set to `false` by default).
</Note>

If the FlexiLayout variant has been created manually, the program uses the regions of all the fields included in the FlexiLayout variant. The regions of the other fields come from matching the generic FlexiLayout.

Information about which FlexiLayout was used to detect the region of a field is recorded in the recognition log.

## Example

Let us consider some of the fields defined in the Document Definition.

Consider two standard invoice fields, **InvoiceDate** and **InvoiceNumber**, and two user-defined fields, **CustomFieldA** and **CustomFieldB**.

* **InvoiceDate** and **InvoiceNumber** are standard fields, and the detection algorithm for these fields is specified in the main FlexiLayout included in the generic FlexiLayout.
* **CustomFieldA** and **CustomFieldB** are user-defined fields, and the detection algorithms for these fields are specified in the additional FlexiLayout included in the generic FlexiLayout.

Suppose the Operator is not satisfied with the quality of detection of the **InvoiceDate** and **CustomFieldB** fields on invoices from a specific vendor and trains the program on invoices from this vendor.

The field regions from invoices from this vendor are merged as follows:

* **InvoiceNumber** — This region comes from the results obtained with the main FlexiLayout.
* **CustomFieldA** — This region comes from the results obtained with the additional FlexiLayout.
* **InvoiceDate** and **CustomFieldB** — These regions come from the results obtained with the FlexiLayout variant generated by training the program on invoices from this vendor.

For invoices from other vendors, the field regions are merged as follows:

* **InvoiceDate** and **InvoiceNumber** — These regions come from the results obtained with the main FlexiLayout.
* **CustomFieldA** and **CustomFieldB** — These regions come from the results obtained with the additional FlexiLayout.
