Merge tiff files into multipage tiff file


  • Anonymous

    I'm trying to merge multiple tiff files under a folder into a single, multi-
    page tiff file. Is this something that can be done with freeImage (with VBA
    preferably) ?
    If so, I'll appreciate any tips.

    If not, maybe someone can direct me in the right direction to do this.


  • Carsten Klein
    Carsten Klein

    Hi there,

    I strongly recommend using the VB6 / VBA wrapper that comes with the official
    FreeImage distribution. Copy 'n' paste this VB6 BAS module into a module
    inside your VBA application.

    However, there is a bug in the latest release 2.5 of the VB6 / VBA wrapper,
    which prevents it from running in VBA environments. Two constants, that are
    defined in the VBRUN library (Visual Basic runtime objects and procedures) are
    used internally. I know now way of referencing this library in VBA. So, there
    is a new version of the MFreeImage.bas module available in the CVS. You could
    either download this new version or just replace both constants with their
    numeric value. To do so, compile the VBA code to automatically find the
    occurrences of these constants. Then replace vbPicTypeBitmap with 1 and
    vbPicTypeIcon with 3. That's all.

    So, lets come to the code to merge the images. This small code snipped should
    do the job:

    Dim hDIB As Long
    Dim hMultiBitmap As Long
    Dim strPath As String
    Dim strFile As String

    ' Setup path for gathering TIFF files
    strPath = "C:\MyImages\tiff\"

    ' Open new multipage bitmap
    hMultiBitmap = FreeImage_OpenMultiBitmap(FIF_TIFF, C:\MyImages\multi.tif", 1,

    ' Iterate on all files in directory
    strFile = Dir$(strPath + "MyImg*.tif")
    Do While (LenB(strFile) > 0)

    ' Got a new file to append so, open it as a single page image
    hDIB = FreeImage_LoadEx(strPath + strFile)

    ' Append the single page to the miltipage bitmap
    Call FreeImage_AppendPage(hMultiBitmap, hDIB)

    ' Never forget to unload the single page image to prevent memory leaks
    Call FreeImage_Unload(hDIB)

    ' Find next file to append
    strFile = Dir$()

    ' Close the multipage bitmap
    Call FreeImage_CloseMultiBitmap(hMultiBitmap, FISO_TIFF_LZW)


  • dackmo

    I just did it with C#. Great!
    But on question. We work with annotations on tiff files.
    when I append a page to a multitiff-file the annotations are gone. Is there a
    way to save them in the tiff file?

  • zop zoparat
    zop zoparat


    Fantastic! (I'm the thread starter)

    It seems like I need to have the freeImage.dll file in the same folder as the
    application. Is there a way to reference the .dll or embed it in a VBA
    application so that I dont' have to copy over the .dll everytime?

  • Carsten Klein
    Carsten Klein

    Hi there,

    in reply to 3.

    As far as I know, there is no means of preserving metadata in multipage
    images. At least not with FreeImage.

    Hervé: Is there any possibility to add metadata to multipage bitmaps?

    in reply to 4.

    No, the DLL is heavily needed by the VB6 (and also the .NET) wrapper. The
    native C/C++ DLL must be in a directory, so that it is found by the Win32
    LoadLibrary function. (read more on MSDN). So, for a VBA application, I
    recommend to put the DLL into the system32 directory.

    Although you could (in theory) ship the DLL within a BLOB field (if your host
    application is Access, for example) and copy it to a certain location on the
    target machine. In any case, the file needs to be found by LoadLibrary. So,
    best practice is to put it into a directory, that is part of the PATH
    environment variable.

    However, since most users cannot copy files to system32 or any other directory
    under 'Program Files', I don't see any benefit from doing so. This is clearly
    a setup task, which should likely be processed by a setup application. On most
    modern Windows versions, this process will require administrator privileges as

    Have a look at Inno Setup, for example:


  • dackmo


    do you know another library to preserve annotations?

    Single Bitmaps are no problem? How am I doing this with a single bitmap?
    Isn't it possible then, to clone every bitmap of the multipage bitmap to a
    single bitmap and append it at the end?

  • recthor

    I do this almost daily. I use a tiff merger driver found on the internet . Install it and it becomes a selectable merger option.Then you can merge PDFs into one in any program at all, including Adobe Acrobat . Just open the tiffs, select merge, and choice the form you want, the task will be finished in several seconds. if you haven't found a good choice , you can have a try. best wishes.