MFC Converter - SDK sample

This article applies only to novaPDF. If you don't have it yet, you must download it first.

Download now Buy licenses
Feb 25, 2019

We strive to keep our articles as accurate as possible. If you notice any inconsistencies or outdated info please let us know.

MFC Converter - SDK sample

The MFC Converter sample demonstrates how to convert an existing file by printing it to novaPDF using the ShellExecute function. It also demonstrates how to set different options and manage profiles.

The same approach should be taken if you print using a "Print()" method from another object (like an internet browser or a report control). Just replace the ShellExecute call with the call of your Print method.

The same approach should be taken if you print using a Print() method from another object (like an internet browser or a report control). Just replace the ShellExecute call with the call of your Print method.

Note: To be able to use the samples you must install novaPDF SDK as samples work only with it. Download it here: nova PDF SDK.

When the application starts, it creates a few profiles and makes different settings in the profiles. Then it shows a dialog from where the user can select the active profile and change its settings using the controls from the dialog.

After that a document can be selected from the harddisk and printed to novaPDF using the ShellExecute function call.

When using this technique to convert a file to PDF, you have to take care of the fact that ShellExecute prints to the default printer. This function returns immediately and does not wait until the print is finished (it may return before the printing is actually started).

Therefore you have to set the default printer to novaPDF before calling ShellExecute (using the SetDefaultPrinter method), register FileSaved message (or any other novaPDF message) to be sure that the print job was started. In this message handler restore the default printer (with the RestoreDefaultPrinter method). This way you made sure that the default printer was restored and your document is printed to novaPDF.

Source Code Snippets

//1. Declare INovaPdfOptions variable
//declare an INovaPdfOptions member variable
private :
    INovaPdfOptions *m_novaOptions;

//2. Register <%SDK_SAMPLE_PRINTER%> messages
const UINT  wm_Nova_FileSaved = RegisterWindowMessageW( MSG_NOVAPDF2_FILESAVED );
const UINT  wm_Nova_PrintError = RegisterWindowMessageW( MSG_NOVAPDF2_PRINTERROR );

BEGIN_MESSAGE_MAP(CnovaPrintDlg, CDialog)

    //...

    ON_REGISTERED_MESSAGE(wm_Nova_FileSaved, OnNovaPDFFileSaved)
    ON_REGISTERED_MESSAGE(wm_Nova_PrintError, OnNovaPDFPrintError)

    //...

END_MESSAGE_MAP()

//3. Initialize INovaPdfOptions
BOOL CnovaPrintDlg::OnInitDialog()
{

    //...

    HRESULT hr = S_OK;
    m_novaOptions = 0;

    //create an instance of INovaPdfOptions
    hr = CoCreateInstance(__uuidof(NovaPdfOptions80), NULL, CLSCTX_INPROC_SERVER, __uuidof(INovaPdfOptions80), (LPVOID*) &m_novaOptions);
    if (SUCCEEDED(hr)) {
        // initialize the NovaPdfOptions object to use with a printer licensed for SDK
        hr = m_novaOptions->Initialize(PRINTER_NAME, L"");   
    }
    else {
        ::MessageBoxW(NULL, L"Failed to create novaPDF COM object", L"novaPDF", MB_OK);
    }
    //...
}

//4. Release INovaPDFOptions
CnovaPrintDlg::~CnovaPrintDlg()
{
    //...

    //delete profiles
    if (m_wsProfileSmall)
    {
        hr = m_novaOptions->DeleteProfile(m_wsProfileSmall);
        CoTaskMemFree(m_wsProfileSmall);
    }
    if (m_wsProfileFull)
    {
        hr = m_novaOptions->DeleteProfile(m_wsProfileFull);
        CoTaskMemFree(m_wsProfileFull);
    }
    // destroy our nova options object
    if (m_novaOptions) {
        m_novaOptions->Release();
    }
    // uninitialize COM libraries
    CoUninitialize();

    //...

}

//5. Set <%SDK_SAMPLE_PRINTER%> Options
BOOL CnovaPrintDlg::OnInitDialog()
{

    // Add a profile called "Small size". If profile L"Small size" exists this will fail
    hr = m_novaOptions->AddProfile(SMALL_SIZE_PROFILE, PROFILE_IS_PUBLIC, &m_wsProfileSmall);

    //load the newly created profile
    if (SUCCEEDED(hr) && m_wsProfileSmall) 
    {
        //load profile
        m_novaOptions->LoadProfile(m_wsProfileSmall);

        // disable the "Save PDF file as" prompt 
        m_novaOptions->SetOptionLong(NOVAPDF_SAVE_PROMPT_TYPE, PROMPT_SAVE_NONE);
        // set generated Pdf files destination folder ("c:\")
        m_novaOptions->SetOptionLong(NOVAPDF_SAVE_LOCATION, LOCATION_TYPE_LOCAL);
        m_novaOptions->SetOptionLong(NOVAPDF_SAVE_FOLDER_TYPE, SAVEFOLDER_CUSTOM);
        m_novaOptions->SetOptionString(NOVAPDF_SAVE_FOLDER, szExeDirectory);
        // set output file name
        m_novaOptions->SetOptionString(NOVAPDF_SAVE_FILE_NAME, L"PDF Converter small size.pdf");
        // if file exists in the destination folder, append a counter to the end of the file name
        m_novaOptions->SetOptionLong(NOVAPDF_SAVE_FILEEXIST_ACTION, FILE_CONFLICT_STRATEGY_AUTONUMBER_NEW);    
        //Set other options
        //...

        //save profile changes
        m_novaOptions->SaveProfile();
    }
}

//6. Start a print job
void CnovaPrintDlg::OnBnClickedOk()
{

    //...

    HRESULT hr = S_OK;

    // set the active profile to be used for printing
    hr = m_novaOptions->SetActiveProfile(m_strProfileId);

    // register our window to receive messages from the printer
    hr = m_novaOptions->RegisterEventWindow((LONG) GetSafeHwnd());

    // set novaPDF as default printer, so it will be used by ShellExecute
    hr = m_novaOptions->SetDefaultPrinter();

    // license file for ShellExecute
    hr = m_novaOptions->LicenseShellExecuteFile(m_strFileToConvert.AllocSysString());

    // print the document
    m_bPrintJobPending = TRUE;
    HINSTANCE hExec = ShellExecute(GetSafeHwnd(), L"print", m_strFileToConvert, NULL, NULL, SW_HIDE);    

      //...

}

//7. Restore default printer when printing finished
LRESULT CnovaPrintDlg::OnNovaPDFFileSaved(WPARAM wParam, LPARAM lParam)
{
    // restore original default printer
    m_novaOptions->UnRegisterEventWindow();
    m_novaOptions->RestoreDefaultPrinter();
    m_bPrintJobPending = FALSE;
    return 0;
}

LRESULT CnovaPrintDlg::OnNovaPDFPrintError(WPARAM wParam, LPARAM lParam)
{
    switch(wParam){
        case ERROR_MSG_TEMP_FILE:
            MessageBox(L"Error saving temporary file on printer server", L"novaPDF", MB_OK);
            break;
        case ERROR_MSG_LIC_INFO:
            MessageBox(L"Error reading license information", L"novaPDF", MB_OK);
            break;
        case ERROR_MSG_SAVE_PDF:
            MessageBox(L"Error saving PDF file", L"novaPDF", MB_OK);
            break;
        case ERROR_MSG_JOB_CANCELED:
            MessageBox(L"Print job was canceled", L"novaPDF", MB_OK);
            break;
    }
    // restore original default printer
    m_novaOptions->UnRegisterEventWindow();
    m_novaOptions->RestoreDefaultPrinter();
    m_bPrintJobPending = FALSE;
    return 0;
}