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

# Sample 3. Step 25: Detecting the TotalQuantity and TotalAmount fields with a TotalQuantity and TotalAmount elements

> Recover the Total Quantity and Total Amount values with TotalQuantity and TotalAmount Character String elements inside an optional grTotal group.

Analysis of the test images reveals that the Total Quantity and Total Amount fields only occur together with the name `Footer.kwTotal` and are located on the same level.

Use elements of type [Character String](/flexi-capture/fls/template/character-chain) to detect the Total Quantity and the Total Amount fields. To specify the search constraints common to these elements, create a [Group](/flexi-capture/fls/template/elements-compound) element — grTotal.

## Create the grTotal Group element

To create the [Group](/flexi-capture/fls/template/elements-compound) element grTotal:

1. In the Footer element, create a [Group](/flexi-capture/fls/template/elements-compound) element and name it **grTotal**.

2. Select **Optional element** so that the search for the subelements stops if the name is absent.

   <Note>
     For more information, see [The Optional property of a Group element](/flexi-capture/fls/tips-tricks/date11).
   </Note>

3. Click the Advanced tab.

4. Since the **Total Quantity** and the **Total Amount** fields only occur on the image together with the name `Footer.kwTotal`, set the following [additional search constraint](/flexi-capture/fls/template/advanced-constraints) in the Advanced pre-search relations field: If the `Footer.kwTotal` element has not been detected, do not look for the object on the image. In the [FlexiLayout language](/flexi-capture/fls/code/general-code):\
   `If Footer.kwTotal.IsNull Then DontFind;`

## Create the TotalQuantity element

The Total Quantity field is located at the intersection of the Total row and the Quantity column. The first constraint was already specified at the [Group](/flexi-capture/fls/template/elements-compound) element level. To specify the second constraint, use the previously detected column name as a reference element.

To create the TotalQuantity element:

1. In the **grTotal** element, create an element of type [Character String](/flexi-capture/fls/template/character-chain) and name it **TotalQuantity**.

2. Click the Character String tab.

3. In the Character count field, specify this fuzzy interval: `{-1, 1, 10, INF}`. This is an estimate of the length of the character string. Assume that the possible values may range from 1 to 10. Any hypotheses outside this range will be penalized.

4. Leave the default values for the other parameters of the element.

5. Click the Relations tab.

6. Since the **Total Quantity** field is always located on the same level as the keywords of the name `Footer.kwTotal` or slightly lower, set the following search constraints:

   * Above the element `Footer.kwTotal`, Offset = -200, Reference boundary = Bottom
   * Below the element `Footer.kwTotal`, Offset = -20, Reference boundary = Top

   <Tip>
     Prior to setting the values for the offsets, you may want to analyze the geometrical properties (that is, size and boundaries) of the neighboring objects detected during pre-recognition. Offset values are selected by trial and error.
   </Tip>

7. Click the Advanced tab.

8. Since the **Total Quantity** field is optional on the images but whenever it occurs it occurs together with the **Quantity** column and below its name, set the following [additional search constraint](/flexi-capture/fls/template/advanced-constraints) in the Advanced pre-search relations field:

   Look for the object on the image only if the `TableHeader.kwQuantity` element has been detected; Look no farther than 50 dots to the left or to the right of the respective boundaries of the column name. In the [FlexiLayout language](/flexi-capture/fls/code/general-code):\
   `If Not (TableHeader.kwQuantity.IsNull) Then`\
   `{`\
   `LeftOf: TableHeader.kwQuantity.Right, -50 * dot;`\
   `RightOf: TableHeader.kwQuantity.Left, -50 * dot;`\
   `}`\
   `Else DontFind;`

9. Try matching the FlexiLayout with the test images and make sure that the program successfully detects the element on all the images where applicable.

10. To describe the location of the **TotalQuantity** block, select the Source element option, and then click "**…**" and select the **TotalQuantity** element as the source element.

## Create the TotalAmount element

The Total Amount field is located at the intersection of the Total row (the footer of the table) and the Total column. The first constraint was already specified at the [Group](/flexi-capture/fls/template/elements-compound) element level. To specify the second constraint, use the previously detected column names as reference elements.

To create the TotalAmount element:

1. In the **grTotal** element, create an element of type [Character String](/flexi-capture/fls/template/character-chain) and name it **TotalAmount**.

2. Click the Character String tab.

3. In the Character count field, specify this fuzzy interval: `[-1, 1, 20, 2147483647]`. This is an estimate of the length of the character string. Assume that the possible values may range from 1 to 20. Any hypotheses outside this range will be penalized.

4. Leave the default values for the other parameters of the element.

5. Click the Relations tab.

6. Since the **Total Amount** field is always located on the same level as the keywords of the name `Footer.kwTotal`, set the following search constraints:
   * Above the element `Footer.kwTotal`, Offset = -20, Reference boundary = Bottom.
   * Below the element `Footer.kwTotal`, Offset = -20, Reference boundary = Top.

7. Click the **Advanced** tab.

8. Since the **Total Amount** field can only be located immediately below (possibly with a small shift) the name of the **Total** column and only between the **Unit Price** and **Sales** columns, set the following [additional search constraint](/flexi-capture/fls/template/advanced-constraints) in the Advanced pre-search relations field:

   If the `TableHeader.kwTotal` element has been detected, look for the object no farther than 70 dots to the right of the right boundary of the `TableHeader.kwTotal` element and no farther than 50 dots to the left from the left boundary of the `TableHeader.kwTotal` element.

   Or, if the `TableHeader.kwUnitPrice` and `TableHeader.kwSales` elements are detected, look for the object no farther than 40 dots to the right of the left boundary of the `TableHeader.kwSales` element and to the right of the right boundary of the `TableHeader.kwUnitPrice` element; otherwise do not look for the object. In the [FlexiLayout language](/flexi-capture/fls/code/general-code):\
   `If Not (TableHeader.kwTotal.IsNull) Then`\
   `{`\
   `LeftOf: TableHeader.kwTotal.Right, -70 * dot;`\
   `RightOf: TableHeader.kwTotal.Left, -50 * dot;`\
   `}`\
   `Else`\
   `If Not (TableHeader.kwUnitPrice.IsNull) and Not (TableHeader.kwSales.IsNull) Then`\
   `{`\
   `LeftOf: TableHeader.kwSales.Left, -40 * dot;`\
   `RightOf: TableHeader.kwUnitPrice, 0 * dot;`\
   `}`\
   `Else DontFind;`

   <Tip>
     Prior to setting the values for the offsets, you may want to analyze the geometrical properties (that is, size and boundaries) of the neighboring objects detected during pre-recognition. Offset values are selected by trial and error.
   </Tip>

9. Try matching the FlexiLayout with the test images and make sure that the program successfully detects the element on all the images where applicable.

10. To describe the location of the **TotalAmount** block, select the Source element option, and then click <img src="https://mintcdn.com/abbyy/fmgRWFNHKYN2MLSg/images/flexi-capture/fls/Browse_Button.gif?s=3673db30dda4a3293aec716853588b55" alt="Browse button" style={{display:"inline-block",verticalAlign:"middle",margin:0}} width="21" height="20" data-path="images/flexi-capture/fls/Browse_Button.gif" /> and select the **TotalAmount** element as the source element.
