MFC Scribble - SDK sample


Related Articles Add comment Email article Print article

The MFC Scribble sample extends the standard MFC Scribble sample with the generation of PDF files and by using the novaPDF SDK application. It demonstrates how to integrate novaPDF SDK in your documents and also view the MFC architecture.

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.

Source Code Snippets

1. Register novaPDF Printer event

#define PROFILE_NAME      L"MFCScribble Profile"
#define PDF_FILE_NAME     L"MFCScribble.pdf"
#define PROFILE_IS_PUBLIC 0 

// This message is sent when the PDF file is finished and saved on the harddisk
const UINT  wm_Nova_FileSaved = RegisterWindowMessageW( MSG_NOVAPDF2_FILESAVED );
BEGIN_MESSAGE_MAP(CScribbleView, CScrollView)
//{{AFX_MSG_MAP(CScribbleView)
//...
//}}AFX_MSG_MAP
//...
ON_REGISTERED_MESSAGE(wm_Nova_FileSaved, OnNovaPDFFileSaved)
END_MESSAGE_MAP()


2. Initialize INovaPDFOptions
CScribbleView::CScribbleView()
{
//...
HRESULT hr = CoInitialize(NULL);
//...
//create novaPDFOptions object
hr = CoCreateInstance(__uuidof(NovaPdfOptions), NULL, CLSCTX_INPROC_SERVER, __uuidof(INovaPdfOptions), (LPVOID*) &m_pNova);
//...
//initialize novaPDFOptions object with desired printer name
// if you have an application license for novaPDF SDK,
// pass both the registration name and the license key to the Initialize() function
// m_pNova->Initialize(L"novaPDF Pro v6", L"<registration name>", L"<license key>");
m_pNova->Initialize(L"novaPDF Pro v6", L"", L"", L"");
}


3. Release INovaPDFOptions 

CScribbleView::~CScribbleView()
{
//release novaPDFOptions object
if (m_pNova){
  m_pNova->Release();
}
CoUninitialize();

4. Set novaPDF Printer Options
BOOL CScribbleView::OnPreparePrinting(CPrintInfo* pInfo)
{
//set novaPDF default printer
if (m_pNova){
  m_pNova->SetDefaultPrinter(); 

  // create a temporary profile for the current print job,
  // in order to not modify the default profile settings
  m_pNova->AddProfile(PROFILE_NAME, PROFILE_IS_PUBLIC);
  // set PDF document Title
  m_pNova->SetOptionString(NOVAPDF_INFO_TITLE, L"MFC Scribble Sample", PROFILE_NAME, PROFILE_IS_PUBLIC);
  // set resulting file name
  m_pNova->SetOptionString(NOVAPDF_SAVE_FOLDER, L"", PROFILE_NAME, PROFILE_IS_PUBLIC);
  m_pNova->SetOptionString(NOVAPDF_SAVE_FILE, PDF_FILE_NAME, PROFILE_NAME, PROFILE_IS_PUBLIC);
  //do not show prompt dialog
  m_pNova->SetOptionLong(NOVAPDF_SAVE_PROMPT, 0, PROFILE_NAME, PROFILE_IS_PUBLIC);
  //if file exists, override
  m_pNova->SetOptionLong(NOVAPDF_SAVE_CONFLICT_STRATEGY, FILE_CONFLICT_STRATEGY_OVERWRITE, PROFILE_NAME, PROFILE_IS_PUBLIC); 

  // set active profile
  m_pNova->GetActiveProfile(&m_wsDefaultProfile, &m_nActiveProfilePublic);
  m_pNova->SetActiveProfile(PROFILE_NAME, PROFILE_IS_PUBLIC); 

  //register window to receive messages from novaPDF printer
  m_pNova->RegisterEventWindow((LONG)m_hWnd);
}
//...
}

5. Restore options when printing finished
void CScribbleView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
if (m_pNova) {
  //unregister events
  m_pNova->UnRegisterEventWindow();
  //restore default profile
  m_pNova->SetActiveProfile(m_wsDefaultProfile, &m_nActiveProfilePublic);
  m_pNova->DeleteProfile(PROFILE_NAME, PROFILE_IS_PUBLIC);
  CoTaskMemFree(m_wsDefaultProfile);
  //restore default printer
  m_pNova->RestoreDefaultPrinter();
}

6. novaPDF Printer message handler
LRESULT CScribbleView::OnNovaPDFFileSaved(WPARAM, LPARAM)
{
//PDF is saved, so just show a message that the conversion to PDF was successful
MessageBox(L"PDF file was saved successfully", L"novaPrint");
return 0;
}

Related articles

Comments