Skip to main content

Scripting - Merge Two Tables on One Page

Article # 3035885 - Page views: 225


Is it possible to merge two tables? Like if they are two tables on one page?



Figure 1 shows a Document with two tables containing line items one on the left side and one on the right side of the page. The validation operator would like to validate the line items in the correct order.

Figure 1: Document with two tables

Screen Shot 2018-09-04 at 4.19.27 PM.png



With two table locators, one for the left table and one for the right table, a script is needed to put the data in a proper order. Validation is based on the content of only one table, which means that the contents of both Table Locators should be returned from only one Table Locator. This example project describes how to combine the contents of two Table Locators, which then returns the results to a Field of type table.

  1. Create a new project.
  2. Define a class:
    1. Set "Default classification result" to the defined class in project settings
  3. Define a table model with four fields: Course Number, Course Description, Credits and Grade.
  4. Create a table field in the defined class (Table).
    1. Select the table model defined earlier.
  5. Create a Table Locator and name it "TL_Left," to extract the left table:
    1. Select the table model created above.
    2. Select the Manual detection method.
    3. Select Use current sample image (the document can be found in the attached project).
    4. Define the Master Item.
    5. Assign the Cells Course Number, Course Description, Credits and Grade.
    6. Define a region for the left side of the page.
    7. Make use of two anchors to make the table extraction work.
  6. Create a Table Locator and name it "TL_Right" to extract the right table:
    1. Use the settings of the TL_Left.
    2. Define a region for the right side of the page.
  7. Create a Script Locator and name it "SL_Merging_TL" to combine the contents of both Table Locator into one Table Locator:
    1. Open the properties of the Script Locator and select Show Script.
    2. Open the "SL_Merging_TL" object and select the "LocateAlternatives" proc.
    3. Copy and paste the script provided below.
    4. Close the Script Locator properties.
      Logic of the script
      Insert the source line items "TL_Right" into the target table locator "TL_Left" after the end of TL_Left on each page.
  8. Assign the "TL_Left" table locator to the "Table" field.
  9. Classify and Extract.
  10. Validate:
    1. Check the results. This is a two page Document and the sequence should be the following: Fisrt page left table --> first page right table --> second page left table.
Private Sub SL_Merging_TL_LocateAlternatives(ByVal pXDoc As CASCADELib.CscXDocument, _
                                             ByVal pLocator As CASCADELib.CscXDocField)
    '# Monday 4th July 2011.
    '# This script will merge all entries from TL_Right to TL_Left
    '# It is added in such a way that the validation person can validate on each page,
    '# first TL_Left and then TL_Right
    '# The logic of inserting the rows of TL_Right into TL_Left is tested on a 2-page
    '# document and should be tested on a document with more pages
    Dim oTableTargetLeft As CscXDocTable
    Dim oTableSourceRight As CscXDocTable
    Dim oRowSource As CscXDocTableRow
    Dim oRowInserted As CscXDocTableRow
    Set oTableTargetLeft = pXDoc.Locators.ItemByName("TL_Left").Alternatives(0).Table
    Set oTableSourceRight = pXDoc.Locators.ItemByName("TL_Right").Alternatives(0).Table
    Dim i, k, l, m, n, p As Integer
    p = 0
    'count pages
    For m = 0 To pXDoc.Pages.Count - 1
        'Count rows of TL_Left
        For l = 0 To oTableTargetLeft.Rows.Count - 1
        'Compare startpage value of row to pages number
        'And the row number "p" should be equal to the page number
        If Not oTableTargetLeft.Rows.ItemByIndex(l).StartPage = m
        And oTableTargetLeft.Rows.ItemByIndex(p).StartPage =
            pXDoc.Pages.ItemByIndex(m).IndexOnDocument Then
                ' remember the current row number
                p = m
                ' set the row number of the target table where the sourceTable
                ' rows will be inserted
                n = oTableTargetLeft.Rows.ItemByIndex(l).IndexInTable
                ' Loop over the source table to insert all line items to the target table.
                For i = 0 To oTableSourceRight.Rows.Count - 1
                    Set oRowSource = oTableSourceRight.Rows(i)
                    oTableTargetLeft.Rows.Insert(n + i)
                    ' Copy properties of the source table line item to the
                    ' newly inserted target table line item.
                    Set oRowInserted = oTableTargetLeft.Rows(n + i)
                    ' Copy the value of each cell of the source line item to the
                    ' newly inserted target line item.
                    For k = 0 To oRowInserted.Cells.Count - 1
                    oRowInserted.Cells(k).Text = oRowSource.Cells(k).Text
                    oRowInserted.Cells(k).Top = oRowSource.Cells(k).Top
                    oRowInserted.Cells(k).Left = oRowSource.Cells(k).Left
                    oRowInserted.Cells(k).Width = oRowSource.Cells(k).Width
                    oRowInserted.Cells(k).Height = oRowSource.Cells(k).Height
                    oRowInserted.Cells(k).PageIndex = oRowSource.Cells(k).PageIndex
                        oRowInserted.Cells(k).ExtractionConfident =
                        oRowInserted.Cells(k).Valid = oRowSource.Cells(k).Valid
                    ' If the source line item is inserted in the target table.
                    ' exit this loop to insert the next line item of the source table
                Exit For
            End If
End Sub

Level of Complexity 



Applies to  

Product Version Build Environment Hardware
Kofax Transformation Modules All      



Add any references to other internal or external articles



Article # 3035885
  • Was this article helpful?