MFC Converter - SDK sample

Download Sample
Download size: 59.9KB
[Format: ZIP]
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.

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.

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 StartDoc (or any other Windows 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 Printer.


Source Code Snippets

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

2. Register novaPDF 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(NovaPdfOptions), NULL, CLSCTX_INPROC_SERVER, __uuidof(INovaPdfOptions), (LPVOID*) &m_novaOptions);
  if (SUCCEEDED(hr)) {
      //initialize NovaPdfOptions and pass printer name
      // if you have an application license for novaPDF SDK,
      // pass both the registration name and the license key to the Initialize() function
      // hr = m_novaOptions->Initialize(PRINTER_NAME, L"<registration name>", L"<license key>");
      hr = m_novaOptions->Initialize(PRINTER_NAME, L"", L"", L"");
  }
  else {
      ::MessageBoxW(NULL, L"Failed to create novaPDF COM object", L"novaPDF", MB_OK);
  }
  //... 

} 

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

  //delete profiles
  m_novaOptions->DeleteProfile(SMALL_SIZE_PROFILE, PROFILE_IS_PUBLIC); 

  // destroy our nova options object
  if (m_novaOptions) {
      m_novaOptions->Release();
  }
  // uninitialize COM libraries
  CoUninitialize(); 

  //... 

}
5. Set novaPDF Printer Options 

BOOL CnovaPrintDlg::OnInitDialog()
{ 

  //...
  //initialize m_novaOptions (see above)
  //... 

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

  // Set some options to this profile 

  // disable the "Save PDF file as" prompt
  hr = m_novaOptions->SetOptionLong(NOVAPDF_SAVE_PROMPT, FALSE, SMALL_SIZE_PROFILE, PROFILE_IS_PUBLIC);
  // set generated Pdf files destination folder ("c:\")
  hr = m_novaOptions->SetOptionString(NOVAPDF_SAVE_FOLDER, L"", SMALL_SIZE_PROFILE, PROFILE_IS_PUBLIC);
  // set output file name
  hr = m_novaOptions->SetOptionString(NOVAPDF_SAVE_FILE, L"PDF Converter small size.pdf", SMALL_SIZE_PROFILE, PROFILE_IS_PUBLIC); 

  //Set other options
  //...
}

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_strProfile, PROFILE_IS_PUBLIC); 

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

© 2002-2008 Softland. All Rights Reserved.
PDF Creator novaPDF
Create PDF for your paperless office
home Home   contact usContact us   partnershipsPartnerships   bookmarkBookmark   Site updated: 8-October-2008
Downloads Download Now

Download, install and use novaPDF. Latest version: 5.5 build 276

Download PDF creator
promotionsSpecial Offers

Current discounts:
- up to 25% for Desktop Editions
- up to 25% for Server Editions
- 20% Academic Discount
- up to 16% for Developer Tools

buyBuy Now
Professional edition $39.95
(Save $10)
Standard edition $29.95
(Save $10)
Lite edition $19.95
(Save $5)
Buy Now
newsLatest News

novaPDF 5.5 released
August 28, 2008

What’s new (read more):
 » Added Import/Export profiles option
 » Added external function exit code to cancel print job in novaPDF OEM
 » Added "/RegisterWin32COM" install parameter for silent installers
 » Added "/DoNotAskReason" command line parameter for silent uninstalling
 » Added new large printing forms (A0)
 » Added Norwegian and Turkish interface languages

sdkDeveloper Tools

novaPDF SDK is a software development toolkit that can be used by software developers and programmers to add the ability to create PDF files in their applications.
novaPDF OEM is a PDF solution that programmers can use to generate PDF files that will be post-processed by their applications. […]

awardsAwards

novaPDF received the Editor’s Pick from Soft32, a major download site:

PDF creator reviews

See the Awards page for more ratings.

Currently supported languagesLanguages

News:
- Norwegian language added.

Help us translate novaPDF and we will give you licenses for novaPDF Professional and mention you on the Languages page. […]

Reviews & User QuotesPDF Creator Reviews
"novaPDF is a really neat tool. Basically, it’s a PDF printer driver that prints to a PDF file instead of paper.
See more reviews about nova PDF
[…] novaPDF is a great product that works without forcing the user to configure anything. The convenience alone is worth the money." […]
Jason Pullara, Editor
NewsletterKnowledgebase articles

Latest articles on how to:
 » Create PDF files
 » Convert JPG to PDF
 » Convert Powerpoint to PDF
 » Convert Publisher to PDF
 » Convert web pages to PDF
Full article list in the knowledgebase.