MFC Scribble - 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 22, 2019

novaPDF SDK

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

MFC Scribble - SDK sample

Sample SDK code (csharp) of how to integrate for printing "Hello World" text to the novaPDF Printer from remote computer.

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: novaPDF SDK.

Source Code

// 1. Register <%SDK_SAMPLE_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(NovaPdfOptions80), NULL, CLSCTX_INPROC_SERVER, __uuidof(INovaPdfOptions80), (LPVOID*) &m_pNova);
  // initialize the NovaPdfOptions object to use with a printer licensed for SDK
  hr = m_pNova->Initialize(PRINTER_NAME, L"");}
  //...

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

// 4. Set <%SDK_SAMPLE_PRINTER%> Options
BOOL CScribbleView::OnPreparePrinting(CPrintInfo* pInfo)
{
  //set novaPDF default printer
  if (m_pNova){
        m_pNova->SetDefaultPrinter();
        HRESULT hr;
        hr = m_pNova->GetActiveProfile(&m_wsDefaultProfile);

        //create a new profile with default settings
        hr = m_pNova->AddProfile(PROFILE_NAME, PROFILE_IS_PUBLIC, &m_wsNewProfile);

        //load the newly created profile
        if (SUCCEEDED(hr) && m_wsNewProfile) 
        {
            hr = m_pNova->LoadProfile(m_wsNewProfile);
        }
        else
        {
            MessageBox(L"Failed to create profile", L"novaPDF", MB_OK); 
            return hr;
        }

        // set PDF document Title
        m_pNova->SetOptionString(NOVAPDF_DOCINFO_TITLE, L"MFC Scribble Sample");
        // set resulting file name
        m_pNova->SetOptionLong(NOVAPDF_SAVE_FOLDER_TYPE, SAVEFOLDER_CUSTOM);
        m_pNova->SetOptionLong(NOVAPDF_SAVE_LOCATION, LOCATION_TYPE_LOCAL);
        m_pNova->SetOptionLong(NOVAPDF_SAVE_FOLDER_TYPE, SAVEFOLDER_CUSTOM);
        m_pNova->SetOptionString(NOVAPDF_SAVE_FOLDER, L"C:\\temp");
        m_pNova->SetOptionString(NOVAPDF_SAVE_FILE_NAME, PDF_FILE_NAME);
        //do not show prompt dialog
        m_pNova->SetOptionLong(NOVAPDF_SAVE_PROMPT_TYPE, PROMPT_SAVE_NONE);
        //if file exists, override
        m_pNova->SetOptionLong(NOVAPDF_SAVE_FILEEXIST_ACTION, FILE_CONFLICT_STRATEGY_OVERWRITE);

        //save profile changes
        hr = m_pNova->SaveProfile();
        //set as active profile for printer
        m_pNova->SetActiveProfile(m_wsNewProfile);

        //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
        if (m_wsDefaultProfile)
        {
            m_pNova->SetActiveProfile(m_wsDefaultProfile);
        }

        //delete newly created profile
        m_pNova->DeleteProfile(m_wsNewProfile);
        //free memory
        CoTaskMemFree(m_wsNewProfile);
        CoTaskMemFree(m_wsDefaultProfile);
        m_wsDefaultProfile = NULL;
        m_wsNewProfile = NULL;
        //restore default printer
        m_pNova->RestoreDefaultPrinter();
    }
}

// 6. <%SDK_SAMPLE_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;
}