RNJarvis
7/30/2019 - 1:31 PM

Delete separator pages from all documents

Small routine to loop all documents of the batch, deleting specific pages based on certain criteria.

Public Sub SeparatorPageDelete(ByVal pXRootFolder As CASCADELib.CscXFolder)
   On Error GoTo ErrorHandler
   OutputDebugString "Scansation.SeparatorPageDelete"

   Dim i As Long
   Dim oXdoc As CscXDocument
   Dim lPageIndex As Long

   If Project.ScriptVariables.ItemByName("SeparatorPageDelete").Value = "True" Then
      OutputDebugString "Scansation.Separator page removal enabled, deleting seperator pages..."

      ' Here, we're assuming that the separator page is the last page of the document having been moved to the end during KTM Server Batch_Open

      For i = 0 To pXRootFolder.DocInfos.Count - 1
         OutputDebugString "Scansation.Document: " & CStr(i=1)
         Set oXdoc = pXRootFolder.DocInfos.ItemByIndex(i).XDocument

         ' Double check the page contains the separation barcode before deleting it
         If oXdoc.Fields.ItemByName("SepPageBarcode1").PageIndex = oXdoc.CDoc.Pages.Count - 1 Then
            lPageIndex = oXdoc.CDoc.Pages.Count - 1
            oXdoc.DeletePages(lPageIndex, 1)
         End If

      Next

   End If

Exit Sub
ErrorHandler:
   LogError(Err.Number, "SeparatorPageDelete", Err.Description)
End Sub
Public Sub SeparatorPageDetection(ByVal pXRootFolder As CASCADELib.CscXFolder)
   On Error GoTo ErrorHandler
   OutputDebugString "Scansation.SeparatorPageDetection"

   Dim i As Long
   Dim j As Long
   Dim oXdoc As CscXDocument
   Dim lBarcodeCount As Long
   Dim sBarcodeValue As String
   Dim sRegex As String
   Dim lNewPageIndex As Long


   If Project.ScriptVariables.ItemByName("SeparatorPageDetection").Value <> "True" Then
      Exit Sub
   End If


   ' Loop through all documents, checking for the existence of a separator page (1st page only) by accessing Kofax Capture CSS
   ' Inspect available barcodes to verify the separation barcode is present
   ' When detected, move the page to the end of the document so that is can still be used for extraction purposes but does not cause problems for classification

   sRegex = Project.ScriptVariables.ItemByName("SeparatorPageDetectionRegex").Value
   OutputDebugString "Scansation.SeparatorPageDetectionRegex: " & CStr(sRegex)

   Select Case Project.ScriptExecutionMode
   Case CscScriptModeServer

      If Project.ScriptExecutionInstance = 1 Then


         Select Case pXRootFolder.Fields.ItemByName("BatchType").Text
         Case "MAIL", "EMAIL", "GONEAWAY", "RESCAN"


            ' The first document is skipped as this is assumed to be the batch header (starting index is 1 instead of 0)
            For i = 1 To pXRootFolder.DocInfos.Count - 1
               OutputDebugString "Scansation.Document: " & CStr(i=1)
               Set oXdoc = pXRootFolder.DocInfos.ItemByIndex(i).XDocument
               If oXdoc.XValues.ItemExists("AC_CSS_PAGE1_Kofax.Capture.Barcodes.Count") = True Then
                  lBarcodeCount = CLng(oXdoc.XValues.ItemByName("AC_CSS_PAGE1_Kofax.Capture.Barcodes.Count").Value)
                  OutputDebugString "Scansation.Barcode Count: " & CStr(lBarcodeCount)
                  If lBarcodeCount > 0 Then
                     For j = 1 To lBarcodeCount
                        OutputDebugString "Scansation.Checking Barcode: " & CStr(j)
                        sBarcodeValue = CStr(oXdoc.XValues.ItemByName("AC_CSS_PAGE1_Kofax.Capture.Barcodes." & CStr(j) & ".Value").Value)
                        If IsRegexMatch(sRegex, sBarcodeValue, True, False, False) = True Then
                           OutputDebugString "Scansation.Separator Page Detected, moving separator to end of document"
                           lNewPageIndex = oXdoc.CDoc.Pages.Count - 1
                           oXdoc.MovePage(0, lNewPageIndex)
                           Exit For
                        End If
                     Next
                  End If
               End If
            Next


         Case "SFTP"



         End Select





      End If

   Case CscScriptModeValidation

      ' Do nothing

   Case Else

      ' Project Builder (DocReview)
      For i = 0 To pXRootFolder.DocInfos.Count - 1
         OutputDebugString "Scansation.Document: " & CStr(i=1)
         Set oXdoc = pXRootFolder.DocInfos.ItemByIndex(i).XDocument
         OutputDebugString "Scansation.Separator Page Detected, moving separator to end of document"
         lNewPageIndex = oXdoc.CDoc.Pages.Count - 1
         oXdoc.MovePage(0, lNewPageIndex)
      Next

   End Select

Exit Sub
ErrorHandler:
   LogError(Err.Number, "SeparatorPageDetection", Err.Description)
End Sub