Splitting Multipage TIFF Files into Single Pages using Visual Basic & Freeimage
published : 12.Dec.2004
Introduction
The code from this tutorial has been turned into a finished product which you can download. The TIFF File Splitting Tool will be useful if you don't want to create your own solution using the example code below.
We recently had a conversion job where we had to split hundreds of multipage TIFF files into individual TIFF files for each page, so that they could be loaded into a document management system. This short tutorial demonstrates how we used the FreeImage image processing library and Visual Basic to automate the whole process.
By following this example code, or by downloading the sample source files, you will be able to create a program which allows the user to select a multipage TIFF, and create single page TIFF files named page1, page2 e.t.c. from the original.
The Form Components Needed
To write this software you will need a form with 4 components added to it:-
- A Kodak thumbnail control to display the individual pages
- A CommandButton to actually perform the splitting of the multipage TIFF file into its individual pages.
- A CommandButton to invoke the file Open dialog.
- A CommonDialog control to produce an Open dialog, so that the user can select a TIFF file for processing.
Selecting the file to process
In the cmdFile_Click() procedure the first thing we do is disable the button for the split command, we will only enable it at the end of this procedure if the selected file actually has multiple pages. The next segment of code initialises aspects of the open dialog, for example setting a filter to only display TIF and TIFF files.
Once a file has been selected then we assign the image filename to the kodak thumbnail (imgThumb.Image) control, so that if the file contains multiple pages they will be displayed as thumbnails.
Next we use two FreeImage functions to determine the page count, and if the TIFF file contains multiple pages, then we enabled the split command button.
Option Explicit
Dim ImageFile As Long
Dim BitMapImage As Long
Private Sub cmdFile_Click()
cmdSplit.enabled = False
' create a open dialog so that you can select
' the multipage tiff to split
CommonDialog.InitDir = App.path
CommonDialog.DialogTitle = "Select Multi Page TIFF"
CommonDialog.DefaultExt = "*.TIF"
CommonDialog.filter = "TIF Files|*.TIF|TIFF Files|*.TIFF"
CommonDialog.ShowOpen
If (Len(CommonDialog.filename) > 0) Then
' apply the selected image to the Kodak
' thumbnail control to display icons of the
' different pages
imgThumb.Image = CommonDialog.filename
imgThumb.Refresh
' use the FreeImage DLL to open the multipage
' TIFF file, and use the GetPageCount function
' to determine if there are multiple pages to
' process
ImageFile = FreeImage_OpenMultiBitmap(FIF_TIFF, _
CommonDialog.filename, False, True)
If (FreeImage_GetPageCount(ImageFile) > 1) Then
cmdSplit.enabled = True
End If
' update caption bar with filename + page count
Caption = CommonDialog.filename & " " & _
FreeImage_GetPageCount(ImageFile) & " page(s)"
' close the multi-page TIFF file
Call FreeImage_CloseMultiBitmap(ImageFile)
End If
End Sub
Splitting the TIFF image into individual pages
First of all we open the TIFF file using FreeImages FreeImage_OpenMultiBitmap function, and then we count the number of pages to process using the FreeImage_GetPageCount function from the FreeImage library.
Next we need to loop through each page in the TIFF file and extract each page, saving the contents away as a new file. The page indexing of FreeImage appears to be zero based, so for example if the FreeImage_GetPageCount function returns 7 pages, then you need to process pages 0 to 6. This is the reason why our Do While loop works from zero up to page count - 1.
The process of extracting the individual pages is quite easy using the functions provided by FreeImage. As you loop through the page numbers you basically lock the page using the FreeImage_LockPage function, assigning the result into a new Long variable called BitMapImage. This temporary variable now holds the contents of the locked page, so its a simple mater of calling the FreeImage_Save function, passing in the BitMapImage variable and giving it an appropriate page number file name.
You have to ensure that you unlock the page before moving onto the next one in the TIFF file.
Private Sub cmdSplit_Click()
Dim pages As Integer
Dim i As Integer
' use the FreeImage DLL to open the multipage
' TIFF file, and use the GetPageCount function
' to determine if there are multiple pages to process
ImageFile = FreeImage_OpenMultiBitmap(FIF_TIFF, _
CommonDialog.filename, False, True)
pages = FreeImage_GetPageCount(ImageFile)
i = 0
Do While i <= (pages - 1)
' use the lock page function to copy that
' page into a new variable BitMapImage
BitMapImage = FreeImage_LockPage(ImageFile, i)
' save that page out to a new filename
Call FreeImage_Save(FIF_TIFF, BitMapImage, _
App.path & "\page" & i + 1 & ".tif")
' unlock the page
Call FreeImage_UnlockPage(ImageFile, i, False)
i = i + 1
Loop
' close the multi-page TIFF file
Call FreeImage_CloseMultiBitmap(ImageFile)
' feedback to user that process is complete
MsgBox pages & " pages, split from " & _
CommonDialog.filename, vbInformation, "Complete"
End Sub
Thanks
Thanks to Hervé Drolon FreeImage Project Manager for pointing out some errors in our original article.
