Knowledgebase

Back to Articles List

How do I handle print events PDF-XChange Drivers API SDK event handling and ATL?

Question:

How do I handle PDF-XChange Drivers API SDK print events?

Can you tell me how to handle print events, such as OnStartDoc, OnStartPage and so on.
I tried this way:
-Implement _IPXCPrinterEvents interface
-Ask IConnectionPointContainer at PXCComLib::IPXCControlEx object
-Make Advise
-Send document on print through application
-Wait for events
I use ATL.

Answer:

Here is a small example of a class declaration (or you can use an existing class) using ATL.

First add the following code to your stdafx.h:

#import "progid:PXCComLib.CPXCControlEx" rename_namespace("PXC"), named_guids  

Second declare a class like this:

public:
    MyPXCHandler()
    {
        m_pFactory = NULL;
        m_pPrinter = NULL;
    }
    ~MyPXCHandler()
    {
        Finish();
    }
public:
    HRESULT Prepare()
    {
        if (m_pPrinter != NULL)
            return S_OK;
        if (m_pFactory == NULL)
        {
            m_pFactory = PXC::IPXCControlExPtr(__uuidof(PXC::CPXCControlEx));
            if (m_pFactory == NULL)
            {
                // handle error here
                return E_FAIL;
            }
        }
        m_pPrinter = m_pFactory->Printer[L"", L"PDF-XChange 4.0 Sample", L"<YOUR REG KEY HERE>", L"<YOUR DEV CODE HERE>"];
        if (m_pPrinter == NULL)
            return E_FAILE;
        return DispEventAdvise(m_pPrinter);
    }
    HRESULT Finish()
    {
        if (m_pPrinter != NULL)
        {
                m_pPrinter->RestoreDefaultPrinter();
                DispEventUnadvise(m_pPrinter);
                m_pPrinter.Release();
        }
        if (m_pFactory != NULL)
        {
                m_pFactory.Release();
        }
    }
public:
        BEGIN_SINK_MAP(CMainDlg)
                SINK_ENTRY_EX(0, __uuidof(PXC::_IPXCPrinterEvents), 1, OnStartDoc)
                SINK_ENTRY_EX(0, __uuidof(PXC::_IPXCPrinterEvents), 2, OnStartPage)
                SINK_ENTRY_EX(0, __uuidof(PXC::_IPXCPrinterEvents), 3, OnEndPage)
                SINK_ENTRY_EX(0, __uuidof(PXC::_IPXCPrinterEvents), 4, OnEndDoc)
                SINK_ENTRY_EX(0, __uuidof(PXC::_IPXCPrinterEvents), 5, OnFileSaved)
                SINK_ENTRY_EX(0, __uuidof(PXC::_IPXCPrinterEvents), 6, OnFileSent)
                SINK_ENTRY_EX(0, __uuidof(PXC::_IPXCPrinterEvents), 7, OnError)
                SINK_ENTRY_EX(0, __uuidof(PXC::_IPXCPrinterEvents), 8, OnDocSpooled)
        END_SINK_MAP()
public:
   HRESULT __stdcall OnStartDoc(long JobID, BSTR lpszDocName, BSTR lpszAppName);
    HRESULT __stdcall OnStartPage(long JobID, long nPageNumber)
    {
        // add your handling here
        return S_OK;
    }
    HRESULT __stdcall OnEndPage(long JobID, long nPageNumber)
    {
        // add your handling here
        return S_OK;
    }
    HRESULT __stdcall OnEndDoc(long JobID, long bOK)
    {
        // add your handling here
        return S_OK;
    }
    HRESULT __stdcall OnFileSaved(long JobID, BSTR lpszFileName)
    {
        // add your handling here
        return S_OK;
    }
    HRESULT __stdcall OnFileSent(long JobID, BSTR lpszFileName)
    {
        // add your handling here
        return S_OK;
    }
    HRESULT __stdcall OnError(long JobID, long dwErrorCode)
    {
        // add your handling here
        return S_OK;
    }
    HRESULT __stdcall OnDocSpooled(long JobID, BSTR lpszDocName, BSTR lpszAppName)
    {
        // add your handling here
        return S_OK;
    }
public:
    PXC::IPXCPrinterPtr   m_pPrinter;
    PXC::IPXCControlExPtr   m_pFactory;
};
Was this article helpful?
Yes No Somewhat