// #include "ReaderWrapper.h" // This will instanciate a new object CReaderWrapper ReaderDemo("d:\\test.pdf"); // Open your document inside acrobat ReaderDemo.DocOpen(); //Seek display to page 4 ReaderDemo.GoToPage(4); //Print the file without displaying any modal dialog box ReaderDemo.FilePrintSilent(); //Close the document ReaderDemo.DocClose(); //Exit reader ReaderDemo.ExitAcrobat(); //
// ReaderWrapper.cpp: implementation of the CReaderWrapper class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "sstpdf2.h" #include "ReaderWrapper.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif /************************************************************************* ------------------------------------- * Software: CReaderWrapper * * Version: 1.1 * * Date: 2004-11-09 * * * * You may use, modify and redistribute this software as you wish. * ************************************************************************** OVERVIEW This class uses DDE messages to communicate with acrobat reader (or acrobat) Most of usefull DDE messages have been wrapped into that class. Acrobat DDE messages are described in the adobe document called IACReference.pdf (this document can be downloaded inside the ADOBE SDK) ************************************************************************** *************************************************************************/ ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// HDDEDATA CALLBACK DdeCallback( UINT uType, // Transaction type. UINT uFmt, // Clipboard data format. HCONV hconv, // Handle to the conversation. HSZ hsz1, // Handle to a string. HSZ hsz2, // Handle to a string. HDDEDATA hdata, // Handle to a global memory object. DWORD dwData1, // Transaction-specific data DWORD dwData2) // Transaction-specific data. { return 0; } CReaderWrapper::CReaderWrapper(CString stPdfFile) { this->stPdfFileName=stPdfFile; this->dwIdInst=0; this->hConv=0; /*DDE initialisation process*/ UINT iReturn; iReturn = DdeInitialize(&this->dwIdInst, (PFNCALLBACK)DdeCallback, APPCLASS_STANDARD | APPCMD_CLIENTONLY, 0 ); ASSERT(iReturn==DMLERR_NO_ERROR); } CReaderWrapper::~CReaderWrapper() { /*Disconnect */ if(this->hConv) DdeDisconnect(this->hConv); if(this->dwIdInst) DdeUninitialize(this->dwIdInst); } /*Open the pdf file in acrobat reader*/ void CReaderWrapper::DocOpen() { HINSTANCE hRet; /*Start the DDE server*/ hRet = ShellExecute(0, "open", (LPCTSTR)this->stPdfFileName, 0, 0, SW_SHOWNORMAL); ASSERT((int)hRet >= 33); /*Connect to server*/ HSZ hszApp, hszTopic; char szApp[] = "acroview"; char szTopic[] = "control"; hszApp = DdeCreateStringHandle(this->dwIdInst, szApp, 0); hszTopic = DdeCreateStringHandle(this->dwIdInst, szTopic, 0); this->hConv = DdeConnect(this->dwIdInst, hszApp, hszTopic, NULL); DdeFreeStringHandle(this->dwIdInst, hszApp); DdeFreeStringHandle(this->dwIdInst, hszTopic); if (this->hConv == NULL) { printf("DDE Connection Failed.\n"); Sleep(1500); DdeUninitialize(this->dwIdInst); } /*DDE message DocOpen MUST be send first before using other messages*/ CString stCmdLine; stCmdLine=""; stCmdLine.Format("[DocOpen(\"%s\")]",this->stPdfFileName); this->_ExecuteQuery(stCmdLine); } /*Open the pdf file in acrobat reader*/ void CReaderWrapper::CloseAllDocs() { CString stCmdLine; stCmdLine="[CloseAllDocs]"; this->_ExecuteQuery(stCmdLine); } /*Close the current document*/ void CReaderWrapper::DocClose() { CString stCmdLine; stCmdLine=""; stCmdLine.Format("[DocClose(\"%s\")]",this->stPdfFileName); this->_ExecuteQuery(stCmdLine); } void CReaderWrapper::ExitAcrobat() { CString stCmdLine; stCmdLine="[AppExit()]"; this->_ExecuteQuery(stCmdLine); } void CReaderWrapper::GoToPage(int iPage) { CString stCmdLine; stCmdLine=""; stCmdLine.Format("[DocGoTo(\"%s\",%i)]",this->stPdfFileName,iPage-1); this->_ExecuteQuery(stCmdLine); } void CReaderWrapper::FilePrint() { CString stCmdLine; stCmdLine=""; stCmdLine.Format("[FilePrint(\"%s\")]",this->stPdfFileName); this->_ExecuteQuery(stCmdLine); } void CReaderWrapper::FilePrintSilent() { CString stCmdLine; stCmdLine=""; stCmdLine.Format("[FilePrintSilent(\"%s\")]",this->stPdfFileName); this->_ExecuteQuery(stCmdLine); } /*Private methods*/ void CReaderWrapper::_ExecuteQuery(CString stQuery) { HDDEDATA hData=NULL; hData = DdeCreateDataHandle(this->dwIdInst, (LPBYTE)(LPCTSTR)stQuery, stQuery.GetLength()+1, 0, NULL, CF_TEXT, 0); if (hData==NULL) { printf("Command failed: %s\n", (LPCTSTR)stQuery); } else { DdeClientTransaction((LPBYTE)hData, 0xFFFFFFFF, this->hConv, 0L, 0, XTYP_EXECUTE, TIMEOUT_ASYNC, NULL); } }