> ## 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 4. Step 5.4: Invoice Number field, InvoiceNumber element

> Capture the Invoice Number value with an InvoiceNumber Character String element, restricting the search area with FlexiLayout language pre-search relations.

On some documents, the **Invoice Number** field appears to the right of the field name; on others it appears below the name. In documents 1 and 2, the field data is placed directly beneath the name.

## Create the InvoiceNumber element

<Steps>
  <Step title="Create the Character String element">
    In the **InvoiceHeader** element, create a [Character String](/flexi-capture/fls/template/character-chain) element and name it **InvoiceNumber**.

    <Tip>
      The element name need not match the name of the block that corresponds to the **InvoiceNumber** field. However, identical names are convenient when working with a FlexiLayout.
    </Tip>
  </Step>

  <Step title="Specify the alphabet">
    Click the **Character String** tab and specify the alphabet:

    `-./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`
  </Step>

  <Step title="Set the non-alphabet character percentage">
    Set **Percentage of non-alphabet characters** to 30%.
  </Step>

  <Step title="Set the character count">
    In the **Character count** field, specify the fuzzy interval `{-1, 3, 20, INF}` for the length of the character string, assuming that possible values fall in the range of 3 to 20 characters. Any hypothesis outside this interval is penalized.
  </Step>

  <Step title="Exclude the kwInvoiceDate region">
    On some images, the search area for the **Invoice Number** field contains the already detected **kwInvoiceDate** element. Exclude this element from the search area, so that FlexiLayout Studio does not include this text in its hypotheses for the **Invoice Number** field:

    * On the **Search Constraints** tab, click **Add...** next to the **Exclude regions of elements** field.
    * Select **kwInvoiceDate** from the list of elements.
    * Click **OK**. The string `SearchElements.InvoiceHeader.kwInvoiceDate` appears in the **Exclude regions of elements** field.

    <Frame>
      <img src="https://mintcdn.com/abbyy/8aWHPAJyzWmTqglX/images/flexi-capture/fls/tutorial4_5_4.png?fit=max&auto=format&n=8aWHPAJyzWmTqglX&q=85&s=f75396d33e7d2b3b095a09fc446d3d3b" alt="Screenshot of the Search Constraints tab of the InvoiceNumber Character String element properties in ABBYY FlexiLayout Studio, showing the kwInvoiceDate element added to the Exclude regions of elements field as SearchElements.InvoiceHeader.kwInvoiceDate." width="427" height="454" data-path="images/flexi-capture/fls/tutorial4_5_4.png" />
    </Frame>
  </Step>

  <Step title="Add pre-search relations">
    Click the **Advanced** tab. In **Advanced pre-search relations**, specify additional conditions: if the name **kwInvoiceNumber** is not found, do not search for the **InvoiceNumber** field; otherwise, specify the search area as an array of rectangles, one to the right of the name and one below the name with a small offset. In the [FlexiLayout language](/flexi-capture/fls/code/general-code):

    ```text theme={null}
    if kwInvoiceNumber.IsNull then Dontfind();
    else
    { WholeWordMode: true;
    MaxGapInLine: 30dt;
    RectArray DataRegion;
    Let r1= Rect (kwInvoiceNumber.Rect.Right, kwInvoiceNumber.Rect.Top -20dt, kwInvoiceNumber.Rect.Right + 650dt, kwInvoiceNumber.Rect.Bottom + 50dt);
    // Limits the search area by a rectangle to the right of the name of the InvoiceNumber field.
    Let r2 = Rect (kwInvoiceNumber.Rect.Left - 100dt, kwInvoiceNumber.Rect.Bottom, kwInvoiceNumber.Rect.Right + 100dt, kwInvoiceNumber.Rect.Bottom + 100dt);
    // Limits the search area by a rectangle beneath the name of the InvoiceNumber field.
    DataRegion = RectArray (r1);
    DataRegion.Add (r2);
    RestrictSearchArea (DataRegion);
    }
    ```
  </Step>

  <Step title="Add post-search relations">
    The search area for the field is limited to two rectangles, one to the right of the field name and one beneath it. When the field data is to the right of the name, there is often other text beneath the field (on the test images, most commonly the name and data of the **Date** field). Because this extraneous text falls in the search area for the **Invoice Number** field, FlexiLayout Studio generates a hypothesis for it. To influence the quality of this hypothesis, specify additional checks in the **Advanced post-search relations** field: the lower the text beneath the field name, the bigger the penalty. In the [FlexiLayout language](/flexi-capture/fls/code/general-code):

    ```text theme={null}
    if not IsNull then
    { FuzzyQuality: Top - kwInvoiceNumber.Rect.Top, {-30000,0,0,10000}*dt;
    }
    // The greater the distance between the name and upper boundary of the field, the bigger the penalty for the corresponding hypothesis. In this particular case, the hypothesis for the element whose upper boundary coincides with the upper boundary of the name will have the highest quality.
    ```
  </Step>

  <Step title="Match the FlexiLayout">
    Temporarily exclude the **InvoiceFooter** element and match the FlexiLayout.
  </Step>

  <Step title="Set the block region">
    Specify the location of the **InvoiceNumber** block as the rectangular region of the **InvoiceNumber** element enlarged by 5 dots vertically and horizontally. Select **Expression** and type this expression:

    ```text theme={null}
    Rect outputRect;
    outputRect = InvoiceHeader.InvoiceNumber.Rect;
    IsNull = InvoiceHeader.InvoiceNumber.IsNull;
    // Treats the block region as detected if the InvoiceNumber element has been detected.
    OutputRegion = outputRect;
    OutputRegion.Inflate (5dt, 5dt);
    ```
  </Step>
</Steps>
