The MFC Scribble sample extends the standard MFC Scribble sample with the generation of PDF files using novaPDF SDK. It demonstrates how to integrate novaPDF SDK in a document/view MFC architecture:
Source code snippets
Register 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()
Initialize INovaPDFOptions
CScribbleView::CScribbleView()
{
//...
HRESULT hr = CoInitialize(NULL);
//...
//create novaPDFOptions object
hr = CoCreateInstance(__uuidof(NovaPdfOptions<%VERSION_MAJOR%>), NULL, CLSCTX_INPROC_SERVER, __uuidof(INovaPdfOptions<%VERSION_MAJOR%>), (LPVOID*) &m_pNova);
// initialize the NovaPdfOptions object to use with a printer licensed for SDK
hr = m_pNova->Initialize(PRINTER_NAME, L"");}
//...
Release INovaPDFOptions
CScribbleView::~CScribbleView()
{
//release novaPDFOptions object
if (m_pNova){
m_pNova->Release();
}
CoUninitialize();
}
Set 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);
//...
}
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();
}
}
novaPDF SDK 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;
}