ImageControls

 

ImageControls 3 Application Note

IC Toolkit 3.1 is the most recent release of the ImageControls Toolkit.


If you are running a previous version of Kofax ImageControls, refer to the ImageControls How to Buy Web page for details on how to obtain the appropriate ImageControls 3 release for your configuration.


Inserting Pages To and Deleting Pages From a Multipage TIFF

ImageControls 2 & ImageControls 3 allow a developer to insert and delete images from multipage TIFF files. The ability to insert an image exists in the KScan control by setting the KScan.IOWriteMode property to KGIOWRITEMODEINSERT. Though a specific property, method or event to delete an individual image from a multipage TIFF file is not provided, this ability is also available with the KScan control.

Source code for this project can be found on our FTP site at ftp://ftp.kofax.com/pub/imagecontrols/unsupported/MPUtil.zip

This project was started from the Deleting Pages From a Multipage TIFF article. This project is based on code from that article with modifications to simplify it and add the ability to insert images. The project consists of one form, two KView controls, one KScan control and the CommonDialog control. Command buttons are used to load the main file, the file from which images will deleted and inserted, and the second file which will contain the image to insert into the main file. Command buttons are used to initiate the deletion process, the insertion process and to terminate the program. Horizontal scroll bars are utilized to flip through the pages of the selected multipage files, and labels grouped in frames to keep track of the total number of pages and the current page selected in each file. Figure 1 displays the placement of these objects on the form.

Figure 1

The following global variables are setup in the General Declarations section of the project:

Dim g_FileName As String 'Global Filename

Dim g_FileName2 As String 'Global Filename

Dim g_PSCounter As String 'Page Start event counter

Dim g_PECounter As String 'Page End event counter

Dim g_Insert As Boolean 'Global flag to denote insert page

The reserving and resetting of the engine occurs during the Form_Load event. This project uses file importation, and will select the first file import source setup in the Kofax Source Manager (KSM). This will normally be the default "Software File Import" source. A software file import source must be defined in the KSM.

Private Sub Form_Load()

'**** Size and position dialog form on screen

Left = (Screen.Width - Width) / 2

Top = ((Screen.Height - Height) / 2)

'**** Select first software import source as engine.

'**** System must have a software import source in KSM.

KScan1.DeviceAlias = KScan1.DeviceDiskCap(1)

KScan1.ActiveDevice = KSACTIVEDEVICEDISK

If KScan1.DeviceReserved Then

KScan1.Action = KSACTIONUNRESERVE

End If

'**** Set engine and reserve import source

KScan1.Action = KSACTIONRESET

KScan1.Action = KSACTIONRESERVE

End Sub

Files are opened using the CommonDialog control. The returned filename is assigned to the global string g_FileName for the main file, while the returned filename for the second file is assigned to the global string g_FileName2. With the controls KView1 and KView2, the FitHeight and FitWidth properties are set to "True" during design so that the entire image will be visible. Once the files are loaded, the total number of pages in the document is displayed along with the current page number of the image being displayed.

Private Sub cmdLoad_Click()

'***** Use CommonDialog control for dialog box to select TIFF file

CommonDialog1.DialogTitle = "File Open"

CommonDialog1.Flags = cdlOFNFileMustExist

CommonDialog1.Filter = "TIFF (*.tif)|*.tif"

CommonDialog1.CancelError = True

On Error Resume Next

CommonDialog1.ShowOpen

If Not Err = cdlCancel Then

g_FileName = CommonDialog1.filename

'***** Do not allow same file to be selected for both KViews

If g_FileName = KView2.filename Then

MsgBox ("This file has already been selected")

Exit Sub

End If

'***** Open selected file for viewing

KView1.filename = g_FileName

KView1.Refresh

'***** Update page counters on form

lblTotal.Caption = "Total: " & Str(KView1.MultiPageCount)

lblCurrent.Caption = "Current: " & Str(KView1.Page)

End If

 

'***** Settings for Scroll bar to flip through images (pages)

HScroll1.Min = 1

HScroll1.Max = KView1.MultiPageCount

HScroll1.LargeChange = 1

HScroll1.SmallChange = 1

HScroll1.Value = 1

End Sub

Private Sub cmdLoad2_Click()

'***** Use CommonDialog control for dialog box to select TIFF file

CommonDialog1.DialogTitle = "File Open"

CommonDialog1.Flags = cdlOFNFileMustExist

CommonDialog1.Filter = "TIFF (*.tif)|*.tif"

CommonDialog1.CancelError = True

On Error Resume Next

CommonDialog1.ShowOpen

If Not Err = cdlCancel Then

g_FileName2 = CommonDialog1.filename

'***** Do not allow same file to be selected for both KView controls

If g_FileName2 = KView1.filename Then

MsgBox ("This file has already been selected")

Exit Sub

End If

'***** Open selected file for viewing

KView2.filename = g_FileName2

KView2.Refresh

'***** Update page counters on form

lblTotal2.Caption = "Total: " & Str(KView2.MultiPageCount)

lblCurrent2.Caption = "Current: " & Str(KView2.Page)

End If

 

'***** Settings for Scroll bar to flip through images (pages)

HScroll2.Min = 1

HScroll2.Max = KView2.MultiPageCount

HScroll2.LargeChange = 1

HScroll2.SmallChange = 1

HScroll2.Value = 1

End Sub

Once the files are loaded, the user can examine the individual pages using the horizontal scroll bars. The horizontal scroll bars initial values are set when the files are first loaded in the application, and the appropriate horizontal scroll bar Change event is set to update the image to view within the file. Also, it updates the user on the page number being viewed.

Private Sub HScroll1_Change()

'***** Scrolling through multipage TIFF file

KView1.Page = HScroll1.Value

KView1.Refresh

'***** Update page counter on form

lblCurrent.Caption = "Current: " & Str$(KView1.Page)

End Sub

Private Sub HScroll2_Change()

'***** Scrolling through multipage TIFF file

KView2.Page = HScroll2.Value

KView2.Refresh

'***** Update page counter on form

lblCurrent2.Caption = "Current: " & Str$(KView2.Page)

End Sub

When the Delete button is pressed, the program verifies that a file has been loaded. If not, the user is presented with a message box stating that no file has been loaded and the operation discontinued. If the selected file has only one page then the user will be given a choice whether to delete the file. Otherwise, the KScan scan method is set to batch mode and the write mode to Append. Finally, the scan process is started.

Private Sub cmdDelete_Click()

Dim status As Integer

'***** Verify that file has been selected for processing.

If KView1.filename = "" Then

MsgBox "A file has not yet been loaded."

Exit Sub

End If

'***** If image has only one page, verify user wants to

'***** delete file. If multiple page, delete selected image.

If KView1.MultiPageCount = 1 Then

status = MsgBox("This file has only one page.

Do you want to Delete it?", vbYesNo)

If status = vbYes Then

Kill g_FileName

KView1.filename = ""

KView1.Clear

End If

Else

'***** Import settings to delete image from file

KScan1.IOWriteMode = KGIOWRITEMODEAPPEND

KScan1.DeviceMethod = KSDEVICEMETHODBATCH

KScan1.Action = KSACTIONSTART

End If

End Sub

When the Insert button is pressed, the program verifies that the main file and the second file have been loaded . If not, the user is presented with a message box stating that at least one file has not been loaded and the operation discontinued. Otherwise, a global variable, g_Insert, is set to True so that the KScan control will know to perform the insert operation and not the delete operation. The KScan scan method is set to single mode and the write mode to Insert. Finally, the scan process is started.

Private Sub cmdInsert_Click()

Dim status As Integer

 

'***** Verify that files have been selected for processing.

If (KView1.filename = "") Or (KView2.filename = "") Then

MsgBox "A file has not yet been loaded."

Exit Sub

End If

 

'***** Set flag to show processing page insert

g_Insert = True

'***** Import settings to insert image file

KScan1.IOWriteMode = KGIOWRITEMODEINSERT

KScan1.DeviceMethod = KSDEVICEMETHODSINGLE

KScan1.Action = KSACTIONSTART

'***** Set flag to original value

g_Insert = False

End Sub

Two counters used in the batch scan process, g_PECounter and g_PSCounter, are initialized in the KScan_BatchStart event. This is necessary to compensate for pre-scan cache (DeviceCache property) settings of greater than 0 when deleting images. These counters are only used when deleting a image.

 Private Sub KScan1_BatchStart()

'***** Counters for the KScan_PageStart and KScan_PageEnd events.

g_PSCounter = 1

g_PECounter = 1

End Sub

The KScan_PageStart event checks if it is performing a insert or delete operation. If inserting, KScan.PSFileName is set to the second file and the KScan.PSPage property to the image presently being viewed. Otherwise, KScan.PSFileName is set to the main file, and the g_PSCounter is used to set the PSPage property of the input file being scanned during the KScan_PageEnd event.

 Private Sub KScan1_PageStart()

'***** If inserting a image into file, select image to import.

If g_Insert Then

KScan1.PSFileName = g_FileName2

KScan1.PSPage = KView2.Page

Else

'***** Else (deletion), import all images in original file.

KScan1.PSFileName = g_FileName

If g_PSCounter > KScan1.PSPageCount Then

KScan1.PSFileName = ""

Else

KScan1.PSPage = g_PSCounter

g_PSCounter = g_PSCounter + 1

End If

End If

 

End Sub

The KScan_PageEnd event checks if it is performing a insert or delete operation. If inserting, KScan.PEFileName is set to the main file and the KScan.PEPage property to the image presently being viewed in the main file. If deleting, the KScan.PEFileName is set to either a temporary file or to "". The g_PECounter is used to keep track of the current page being written out to disk. When the g_PECounter equals the current displayed page in the main file (Kview1.Page), the PEFileName property is set to "". This effectively deletes the page.

 Private Sub KScan1_PageEnd()

'***** If inserting a image, then insert individual image into

'***** selected file and position.

If g_Insert Then

KScan1.PEFileName = g_FileName

KScan1.PEPage = KView1.Page

Else

'***** Else, save imported images except image selected for

'***** deletion. Images are imported into temporary file.

If g_PECounter = KView1.Page Then

KScan1.PEFileName = ""

Else

KScan1.PEFileName = "temp.tif"

End If

g_PECounter = g_PECounter + 1

End If

End Sub

When deleting a image, the temporary file created in the KScan_PageEnd event is used to replace the original file. This is not necessary when inserting images, as the new file is created during the KScan_PageEnd event. The KScan_BatchEnd event then proceeds to update the KView1 control with the new image file, including updating the image presently being displayed. This event will also update the appropriate horizontal scroll bar values and the page numbers displayed to the user.

 Private Sub KScan1_BatchEnd()

Dim ctr As Integer

Dim BackFile As String

 

'***** If operation is to delete image, then copy temporary file

'***** (which contains the original file images minus the selected

'***** image)file over the original.

If Not g_Insert Then

FileCopy "temp.tif", g_FileName

Kill "temp.tif"

End If

 

'***** Set page to view to original page in KView control

ctr = KView1.Page

'***** Reopen the updated image file

KView1.filename = g_FileName

KView1.Action = KVACTIONOPENIMAGE

'***** If selected page does no longer exists, set to valid value

If ctr = 0 Then ctr = 1

If ctr > KView1.MultiPageCount Then ctr = KView1.MultiPageCount

'***** Refresh KView control

KView1.Page = ctr

KView1.Refresh

 

'***** Update the Scroll bar values

HScroll1.Max = KView1.MultiPageCount

HScroll1.Value = KView1.Page

'***** Update the page values displayed

lblTotal.Caption = "Total: " & Str$(KView1.MultiPageCount)

lblCurrent.Caption = "Current: " & Str$(KView1.Page)

End Sub

Lastly, when the Exit button is pressed, the import source is unreserved and the program execution is terminated.

Private Sub cmdExit_Click()

'***** Unreserve the source and end program

KScan1.Action = KSACTIONUNRESERVE

End

End Sub

Back To Top

Kofax ImageControls Support

The Kofax ImageControls support group is skilled in Visual Basic and Visual C++ development and can assist developers in building their custom imaging applications. Our engineers can also point developers to source code samples on the Kofax FTP site.

The ImageControls 3 Technical Support Web pages have been kept up to date. We encourage ImageControls developers to investigate the information in these Web pages before looking for other sources of support. Use the menu options in the upper left side of this page to access additional product support Web pages for this product.


For details regarding technical support available for all Kofax products, please review the current Support Overview & Options and Product Support Eligibility Matrix Web pages.