How to call pdf2imagecom.exe to convert from PDF files to image files from C++ source code?

pdf2imagecom.exe is a COM interface for the pdf2image.dll library, pdf2imagecom.exe can be called from both 32bit and 64bit EXE applications, the APIs are stay same between 32bit and 64bit source code.

The following is a VC++ source code to call the pdf2imagecom.exe, you can compile this source code in VC++ 6.0, VS 2012, etc. IDE properly,

#include <windows.h>

#include <stdio.h>

#include <tchar.h>

#import "..\\..\\pdf2imagecom.exe"

 

BOOL PDFToImageDrawToHDC(PDF2Image::_CPDF2Image *pdfptr, char *lpPDFFile, const char *szUserPassword, const char *szOwnPassword, HDC hDC, int nXDest, int nYDest, int nWidth, int nHeight, int xRes, int yRes, int nBitCount, BOOL bGrayscale, int nPageIndex)

{

   VARIANT nDIBData;

   VariantInit(&nDIBData);

   long nRet = pdfptr->asp_PDFToImageDrawToDIB2(lpPDFFile, &nDIBData,
       szUserPassword, szOwnPassword, FALSE,
xRes, yRes, nBitCount,
       bGrayscale, nPageIndex);

   if(nRet > 0)

   {

       char *lpDIBData = NULL;

       long plLbound, plUbound;

       SAFEARRAY *parrTemp = nDIBData.parray;

       HRESULT hr = SafeArrayAccessData (parrTemp, (void **)&lpDIBData);

       if (!FAILED(hr) && lpDIBData)

       {

          SafeArrayGetLBound (parrTemp, 1, &plLbound);

          SafeArrayGetUBound (parrTemp, 1, &plUbound);

          int nDIBDataLen = plUbound-plLbound+1;

          if (nDIBDataLen > 0)

          {

             LPBITMAPINFO lpBMPInfo = (LPBITMAPINFO)lpDIBData;

             LPBYTE lpBits = (LPBYTE)lpDIBData + sizeof
              (BITMAPINFOHEADER);

             nRet = ::StretchDIBits(hDC, nXDest, nYDest,
                nWidth, nHeight,
0, 0,
                lpBMPInfo->bmiHeader.biWidth,
                lpBMPInfo->bmiHeader.biHeight,
               
lpBits, lpBMPInfo, DIB_RGB_COLORS, SRCCOPY);

          }

          SafeArrayUnaccessData (parrTemp);

          SafeArrayDestroy(parrTemp);

       }

   }

   return TRUE;

}

 

/*

void VeryPDF_Test_PDFToImageDrawToDIB()

{

   VARIANT nDIBData;

   VariantInit(&nDIBData);

 

   _bstr_t input("C:\\test.pdf") ;

   _bstr_t pw1("") ;

   _bstr_t pw2("") ;

 

   long xRes = 100 ;

   long yRes = 100 ;

   long nBitCount = 24 ;

   long bGrayscale = 0 ;

   long nPageIndex = 1 ;

 

   long nRet = pdfptr->asp_PDFToImageDrawToDIB(input, &nDIBData, pw1, pw2, FALSE, xRes, yRes, nBitCount, bGrayscale, nPageIndex);

 

   printf ("nRet = %ld\n", nRet) ;

}

*/

 

int _tmain(int argc, _TCHAR* argv[])

{

   HRESULT hr = CoInitialize(NULL);

   if(FAILED(hr))

       return -1; 

 

   CLSID clsid;

   hr = CLSIDFromProgID(OLESTR("PDF2Image.CPDF2Image"),&clsid);

   if (FAILED(hr))

       return -1;

   PDF2Image::_CPDF2Image *pdfptr = NULL;

   hr = CoCreateInstance(clsid,NULL,CLSCTX_LOCAL_SERVER,
       
__uuidof(PDF2Image::_CPDF2Image),(LPVOID *)&pdfptr);

   if (FAILED(hr))

   {

       return -2;

   }

 

   pdfptr->asp_PDFToImageSetCode("XXXXXXXXXXXXXXX");

   //pdfptr->asp_PDFToImageConverter("C:\\test.pdf", "C:\\test.tif", "", "", 200, 200, 24, 32773, 100, FALSE, FALSE, 0, 0);

   PDFToImageDrawToHDC(pdfptr, "C:\\test.pdf", "", "", GetDC(NULL), 0, 0, 200, 200, 72, 72, 24, FALSE, 1);

 

   if(pdfptr)

       pdfptr->Release();//Release

   pdfptr = NULL;

   ::CoUninitialize();

   return 0;

}

VN:F [1.9.20_1166]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.20_1166]
Rating: 0 (from 0 votes)

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *


Verify Code   If you cannot see the CheckCode image,please refresh the page again!