Using the Creator in an MFC Console Application

The example below shows how to use the PDFCreator control as a library in an MFC console application.  The file Win32ConsoleApp.cpp is the only file (in addition to stdafx.cpp) that is produced by the Visual Studio wizard.

 

Example

// Win32ConsoleApp.cpp : Defines the entry point for the console

// application.

// This sample demonstrates how we can use instantiate a

// PDFCreactiveX COM object and use its IPDFCreactiveX.

#include "stdafx.h"

// 1- Register the ActiveX COM control or class using ' regsvr32.exe "C:\My Installation Path\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

// raw_interfaces_only

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

// The line above will open the type library that is embedded as a resource inside the PDFCreactiveX.dll and after it reads this type library, it will generate two C++ files with the .tlh and .tli extenstions in the temp folder of the project. These two files will be included verbatim automatically by the preprocessor. The pdfcreactivex.tlh file will contain the GUIDs of the COM classes and COM interfaces that are found in the type library. It will also create smart pointer wrappers for the interfaces that we can use directly.

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

{

    CoInitialize(0); // Initialize the COM subsystem

    {

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

        // Below runs after the COM subsystem is intialized and beforeit

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

        // IPDFCreactiveXPtr instances.

        HRESULT hr;

        // Smart pointer the #import command above

        // typedef' d in pdfcreactivex.tlh

        IPDFCreactiveXPtr pdf;

        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

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

                return -1;

            }

            // 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

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

                return -1;

            }

            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

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

                return -1;

            }

            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

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

                return -1;

            }

        } // end try

        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

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

            return -1;

        }

        catch ( ... )

        {

            _tprintf( _T("\nAn unknown exception was thrown\n") );

            return -1;

        }

    }

    CoUninitialize();// Release the COM subsytem

    return 0;

}