Scripting - Merge Two Tables on One Page
Issue
Is it possible to merge two tables? Like if they are two tables on one page?
Cause
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
Solution
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.
Steps
- Create a new project.
- Define a class:
- Set "Default classification result" to the defined class in project settings
- Define a table model with four fields: Course Number, Course Description, Credits and Grade.
- Create a table field in the defined class (Table).
- Select the table model defined earlier.
- Create a Table Locator and name it "TL_Left," to extract the left table:
- Select the table model created above.
- Select the Manual detection method.
- Select Use current sample image (the document can be found in the attached project).
- Define the Master Item.
- Assign the Cells Course Number, Course Description, Credits and Grade.
- Define a region for the left side of the page.
- Make use of two anchors to make the table extraction work.
- Create a Table Locator and name it "TL_Right" to extract the right table:
- Use the settings of the TL_Left.
- Define a region for the right side of the page.
- Create a Script Locator and name it "SL_Merging_TL" to combine the contents of both Table Locator into one Table Locator:
- Open the properties of the Script Locator and select Show Script.
- Open the "SL_Merging_TL" object and select the "LocateAlternatives" proc.
- Copy and paste the script provided below.
- 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.
- Assign the "TL_Left" table locator to the "Table" field.
- Classify and Extract.
- Validate:
- 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.
Script
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) oRowInserted.CopyProps(oRowSource) ' 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 = oRowSource.Cells(k).ExtractionConfident oRowInserted.Cells(k).Valid = oRowSource.Cells(k).Valid Next ' If the source line item is inserted in the target table. ' exit this loop to insert the next line item of the source table Next Exit For End If Next Next End Sub
Level of Complexity
Moderate
Applies to
Product | Version | Build | Environment | Hardware |
---|---|---|---|---|
Kofax Transformation Modules | All |
References
Add any references to other internal or external articles
Article # 3035885