Splitting a Multi-page TIFF File (Visual C++ 5.0)

The following steps show how to loop through all pages of a multi-page TIFF file and extract each page, process it individually, and save it into a separate file.

1.

Add the following #import statements to your .CPP or .H file:

#import "ltrvr14n.dll" no_namespace, named_guids
#import "ltr14n.dll" no_namespace, named_guids
#import "ltrio14n.dll" no_namespace, named_guids
#import "ltrpr14n.dll" no_namespace, named_guids

2.

Declare the following variables:

CoInitialize(NULL);
ILEADRasterIOPtr pRasterIO(__uuidof(LEADRasterIO));
ILEADRasterPtr pRasterImage(__uuidof(LEADRaster));
ILEADRasterProcessPtr pRasterProcess(__uuidof(LEADRasterProcess));
int nPageNumber, nTotalPages;
char szOutFileName[MAX_PATH];

 

3.

Find out the number of pages in the file:

pRasterIO->GetFileInfo(pRasterImage, "MultiPage.tif", 1, FILEINFO_TOTALPAGES);
nTotalPages = pRasterIO->InfoTotalPages;

 

4.

Loop through the pages of the file; perform a specific processing on the image, then save each page into a separate file:

for(nPageNumber = 1; nPageNumber <= nTotalPages; nPageNumber++)
{
   // Load the next image in the loop
   pRasterIO->Load(pRasterImage, "MultiPage.tif", 0, nPageNumber, 1);

   // Perform desired processing on the page, such as flipping it
   pRasterProcess->Flip(pRasterImage);

   // Save the page to a separate file
   wsprintf(szOutFileName, "PageNumber%4.4d.tif", nPageNumber);
   pRasterIO->Save(pRasterImage, szOutFileName, FILE_TIFLZW,
   0, (QFactorConstants)0, SAVE_OVERWRITE);
}

 

5.

Clean up:

pRasterIO = NULL;
pRasterImage = NULL;
pRasterProcess = NULL;
CoUninitialize ();