0

I have PDF files containing a single image. I would like to extract the image to a buffer, uncompressed, inside a C++ application.

Can you advise a (preferably lightweight, preferably free) library to achieve that ?

1

One of the best-known open source libraries for parsing PDF is Ghostscript. Depending on what you’re going to do with your application, there might be a free edition, but I don’t know the details, so please check their website. Their latest API is documented here.

If you want something easier to use, consider the LEADTOOLS PDF Pro library. (Disclaimer: I am an employee of the vendor of this toolkit). The following C++ code gets the page as bitmap then gets pixel data into a buffer:

BITMAPHANDLE bmp = {0};
L_LoadBitmap(pszPdfFileName, &bmp, sizeof(bmp), 24, ORDER_BGR, nullptr, nullptr);
// Optional: save as BMP or any other file format
L_SaveBitmap(TEXT("d:\\tst1230.bmp"), &bmp, FILE_BMP, 24, 2, nullptr);
// calculate buffer size and allocate it
L_SIZE_T uBytes = BITMAPHEIGHT(&bmp) * bmp.BytesPerLine;
L_UCHAR* pBuffer = new L_UCHAR[uBytes];
// Copy all rows of bitmap into buffer
L_AccessBitmap(&bmp);
L_GetBitmapRow(&bmp, pBuffer, 0, uBytes);
L_ReleaseBitmap(&bmp);
// free the bitmap
L_FreeBitmap(&bmp);
// use pBuffer as needed then free it.
delete pBuffer;

There are also other advanced options if you want more control on how the bitmap is loaded.

If you would like to try it, there’s a free evaluation edition here. The evaluation is fully-functional (but time-limited) and comes with free technical support through email and chat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.