Convert PDF to TIFF Convert PDF to TIFF: Privacy Policy Convert PDF to TIFF: Privacy Policy
Russian version


Adobe Reader DDE automation

This example was designed for using in Microsoft Visual C++ 6.0 or above. Adobe Reader 5.0 or above should be installed on your computer.

#include <DDEML.H>

inline void AppProcessMessages()
{
	MSG msg;

	while( PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}


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;
}

void ExecuteQuery( CString stQuery, BOOL bSync, DWORD dwIdInst, HCONV hConv )
{
	if( !dwIdInst || !hConv )
	{
		OutputDebugString( "\t ExecuteQuery Error\n" );
		return;
	}

	HDDEDATA hData = DdeCreateDataHandle(dwIdInst, (LPBYTE)( stQuery.GetBuffer( stQuery.GetLength() + 1 ) ), stQuery.GetLength(), 0, 0, CF_TEXT, 0);
	 if( !hData )
	{
		CString msg = "\t ExecuteQuery: Command failed: \"" + stQuery + "\"\n";
        OutputDebugString( (LPCTSTR)msg );
		return;
	}

	DWORD dwTimeout = TIMEOUT_ASYNC;
	if( bSync )
		dwTimeout = TIMEOUT_ASYNC - 1;

	DdeClientTransaction( (LPBYTE)hData, -1, hConv, 0L, 0, XTYP_EXECUTE, dwTimeout, 0 );
}


BOOL ReaderPrint( CString sExec, CString sPDFFile, CString sPrinter, CString sPrinterDrv, CString sPrinterPort, float fReaderVer )
{// Adobe Reader 5.0 or above

	HSZ			hszApp(0), hszTopic(0);
	CString	stDDECmdLine;

	DWORD		dwIdInst = 0;
	HCONV		hConv = 0;

        if( DMLERR_NO_ERROR != DdeInitialize( &dwIdInst, (PFNCALLBACK)DdeCallback, APPCLASS_STANDARD | APPCMD_CLIENTONLY, 0 ) )
		return 0;

	if( fReaderVer < 10.f )
		hszApp = DdeCreateStringHandle( dwIdInst, "acroview", 0 );
		
	{
		CString s;
		s.Format( "AcroViewR%d", (int)fReaderVer );
		hszApp = DdeCreateStringHandle( dwIdInst, s, 0 );
	}
	
	hszTopic = DdeCreateStringHandle( dwIdInst, "control", 0 );
	
	if( !( hConv = DdeConnect( dwIdInst, hszApp, hszTopic, 0 ) ) )
	{
	//	Start the DDE server and try connect again
		SHELLEXECUTEINFO si = { sizeof( SHELLEXECUTEINFO ) };
		si.lpVerb	= "open";
		si.lpFile	= sExec;
		si.nShow	= SW_HIDE | SW_MINIMIZE;

		if( ShellExecuteEx( &si ) )
		{
			do
			{
				AppProcessMessages();
				Sleep( 300 );
			}
			while( !( hConv = DdeConnect( dwIdInst, hszApp, hszTopic, 0 ) ) );
		}
	}

	DdeFreeStringHandle( dwIdInst, hszApp );
	DdeFreeStringHandle( dwIdInst, hszTopic );
////	
	if( !hConv )
    {
        OutputDebugString( "\t CAdobePrint: DDE Connection Failed in ReaderPrint(...)\n" );
		return 0;
    }

// Open PDF file
	ExecuteQuery( "[DocOpen(\"" + sPDFFile + "\")]", 0, dwIdInst, hConv );
	if( fReaderVer >= 8.f )
		ExecuteQuery( "[DocOpen(\"" + sPDFFile + "\")]", 0, dwIdInst, hConv );
	
// Run printing...
	ExecuteQuery( "[FilePrintTo(\"" + sPDFFile + "\",\"" + sPrinter + "\",\"" + sPrinterDrv + "\",\"" + sPrinterPort +"\")]", 0, dwIdInst, hConv );

// Close document
	ExecuteQuery( "[DocClose(\"" + sPDFFile + "\")]", 0, dwIdInst, hConv );

// Close Reader
	ExecuteQuery( "[AppExit()]", 0, dwIdInst, hConv );

//	Disconnect DDE
	if( hConv )
	{
		DdeDisconnect( hConv );
		hConv = 0;
	}
	
	if( dwIdInst )
	{
		DdeUninitialize( dwIdInst );
		dwIdInst = 0;
	}
//	~Disconnect DDE

	return 1;
}

// ---------------
// How to call:
// ReaderPrint( "C:\\Program Files\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe", "C:\\test.pdf", "Universal Document Converter", "Universal Document Converter", "UDC", 10.f );



Back

 
Download demo version of Universal Document Converter!
© fCoder SIA