ImageGear for C and C++ on Windows v19.0
PDF to Multi-Page Image

Before working with a PDF document, make sure to initialize the PDF component (see Getting Started with PDF).

This topic illustrates converting a PDF into a multi-page TIFF.

Refer to the PDF to Multi-Page Tiff Sample for complete sample code that illustrates how to use this capability.

On a page-by-page basis, we convert each page from the native PDF vector format to a raster format, and then export the page (in memory) as a single-page TIFF file. We save each of those pages to disk. Each page after the first is added to the end of the document, so we end with a multi-page TIFF file.

The following code snippet loads all pages from a PDF document from disk, to a multi-page vector document HMIGEAR:

C and C++
Copy Code
LPSTR inputPath = "MyFilePath.pdf";
HMIGEAR document = 0;
IG_mpi_create(&document, 0);
IG_mpi_file_open(inputPath, document, IG_FORMAT_PDF, IG_MP_OPENMODE_READONLY);

Then, we cycle through each page of the loaded PDF and call SavePageToFile(), which will be explained next. Setting the overwrite parameter to true will "start over" with a new file if a file with that name already exists. We want it to "overwrite" for the first page, but not for subsequent pages. Those will be appended to the end of the file being saved:

C and C++
Copy Code
UINT pageCount;
AT_BOOL overwrite = false;
unsigned int i = 0;
IG_mpi_page_count_get(document, &pageCount);
for (i = 0; i < pageCount; i++)
{
    if (i == 0)
    {
        overwrite = true;
    }
    else
    {
        overwrite = false;
    }
    SavePageToFile(document, i, overwrite);
}
IG_mpi_delete(document);

Lastly, the function SavePageToFile loads a PDF page, produces a converted raster page, and saves that page to disk:

C and C++
Copy Code
void SavePageToFile(HMIGEAR &document, UINT pageIndex, bool overwrite)
{
    HIGEAR page = 0;
    HIGEAR rasterPage = 0;
    UINT pageNumber = 0;
    // Gets vector PDF page from the document.
    IG_mpi_page_get(document, pageIndex, &page);
    // Gets rasterized version of PDF vector page.
    IG_vector_data_to_dib(page, &rasterPage);
    // Save using the specified compression (TIFF compression types include
    // LZW, JPEG, deflate, uncompressed, etc.)
    // A page number of 0 appends to the end rather than inserting at a specific index.
    IG_fltr_save_file(rasterPage, "MultiPagePdfToRaster.tiff", IG_SAVE_TIF_LZW, pageNumber,
        overwrite);
    IG_image_delete(rasterPage);
}

 

 


©2017. Accusoft Corporation. All Rights Reserved.

Send Feedback