Using the Creator in a Win32 Dialog Application

Below is a sample that uses the PDFCreator control in an MFC dialog application.  Visually, the control was added to the dialog in the same way specified in the MFC Console Application example.

 

Example

// MFCDialogAppDlg.cpp : implementation file

#include "stdafx.h"

#include "MFCDialogApp.h"

#include "MFCDialogAppDlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#endif

// 1- Register the ActiveX COM control or class using ' regsvr32.exe "C:\Program Files\Amyuni PDF Suite\PDFCreactiveX.dll"' 

// 2- Add a #import directive to .cpp or .h file where you want to use the control or to

// the stdafx.h file where it will be accessible to all files in the project that use the precompiled header

#import "C:\Program Files\Amyuni PDF Suite\PDFCreactiveX.dll" no_namespace /*raw_interfaces_only*/

BOOL CMFCDialogAppDlg::OnInitDialog()

{

CDialog::OnInitDialog();

    InitControl();

    return TRUE;  // return TRUE  unless you set the focus to a control

}

void CMFCDialogAppDlg::InitControl()

{

    // The PDFCreactiveX control was inserted a on to the form.

    // We could have added a variable m_pdf1 that will be used to

    // access the control but the VS2005 wizard â€™sometimes’ messes

    // up with this part, but it is a valid alternative.

    CWnd *pdfControl = GetDlgItem( IDC_PDFCREACTIVEX1 );

    ASSERT( pdfControl );

    LPUNKNOWN lpUnknown = pdfControl->GetControlUnknown();

    ASSERT( lpUnknown );

    IPDFCreactiveXPtr pdf = lpUnknown;

    {

        // this calling context here is added so that all the code below

        // runs after the COM subsystem is intialized and before it

        // is released. This included the call to the destructors of

        // the IPDFCreactiveXPtr instances.

        HRESULT hr;

        CString s;

        try

        {

            if ( FAILED( hr = pdf.CreateInstance( __uuidof(PDFCreactiveX) ) ) )

            {

                // code that will be entered if hr is a failure code and if

                // raw_interfaces_only is used in the #import directive above

                s.Format( _T("\nHRESULT hr = 0x%x; Failed to create an instance of the PDFCreactiveX COM class\n"), hr );

                AfxMessageBox( s );

                return;

            }

            // at this point pdf contains and AddRef' d pointer to the

            // IPDFCreactiveX interface of the PDFCreactiveX COM object

            // we can start using the methods and properties of this interface

            if (FAILED( hr = pdf->SetLicenseKey( _bstr_t(""), _bstr_t("") ) ))

            {

                // code that will be entered if hr is a failure code and if

                // raw_interfaces_only is used in the #import directive above

                s.Format( _T("\nHRESULT hr = 0x%x; Failed to set the license key\n"), hr );

                AfxMessageBox( s );

                return;

            }

            if ( FAILED( hr = pdf->Open( _bstr_t(""), _bstr_t("") ) ) )

            {

                // code that will be entered if hr is a failure code and if

                // raw_interfaces_only is used in the #import directive above

                s.Format( _T("\nHRESULT hr = 0x%x; Failed to open the file\n"), hr );

                AfxMessageBox( s );

                return;

            }

            long currentPage;

            // try obtaining a property value in vaious ways:

            currentPage = pdf->CurrentPage;

            currentPage = pdf->GetCurrentPage();

            hr = pdf->get_CurrentPage( &currentPage );

            // try setting a property value in vaious ways:

            currentPage = 10;

            pdf->CurrentPage = currentPage;

            pdf->PutCurrentPage( currentPage );

            if ( FAILED( hr = pdf->put_CurrentPage( currentPage ) ) )

            {

                // code that will be entered if hr is a failure code and if

                // raw_interfaces_only is used in the #import directive above

                s.Format( _T("\nHRESULT hr = 0x%x; Failed to set the new current page\n"), hr );

                AfxMessageBox( s );

                return;

            }

        }

        catch ( _com_error er )

        {

            // code that will be entered if hr is a failure code and if

            // raw_interfaces_only is NOT used in the #import directive above

            s.Format( _T("\nHRESULT hr = 0x%x\n"), er.Error() );

            AfxMessageBox( s );

            return;

        }

        catch ( ... )

        {

            s.Format( _T("\nAn unknown exception was thrown\n") );

            AfxMessageBox( s );

            return;

        }

    }

}

 

If you add the control to your project using the "Add Variable" feature of the Visual Studio form designer, you will lose the ability of using the PDF control properties in a direct way, and will have to use the getter and setter methods all the time.