Limited time promo

Hello World (C++) - SDK sample

English en

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
We strive to keep our articles as accurate as possible. If you notice any inconsistencies or outdated info please let us know.
Sample .cpp SDK code for printing "Hello World" text to the novaPDF Printer.
Hello World sample is a simple Windows console application that prints one page with the Hello World text to the novaPDF Printer. It demonstrates the basic use of the INovaPDFOptions interface. The printing job is made with Windows API calls OpenPrinter, StartDoc,… It generates a Hello World.pdf file in the working folder.
Note: To be able to use the samples you must install novaPDF SDK as samples work only with it. Download it here: novaPDF SDK.

Note: If you do not use Windows API calls to print to novaPDF Printer, but you perform a print job by calling other controls Print() method, or if you print an existing document using ShellExecute() function, you should check the MFC Converter sample instead.

Sample Code:
cpp // HelloWorld.cpp #include "stdafx.h" //Include novaPDF headers #include "..\..\include\novaOptions.h" #include "..\..\include\novapi.h" //name of <%SDK_SAMPLE_PRINTER%> #define PRINTER_NAME L"<%SDK_SAMPLE_PRINTER%>" //text to be written in the PDF file #define PDF_TEXT L"Hello world!" //PDF file name #define PDF_FILE_NAME L"HelloWorld.pdf" //Print profile name #define PROFILE_NAME L"HelloWorld Profile" #define PROFILE_IS_PUBLIC 0 //entry point for the console application int _tmain(int argc, _TCHAR* argv[]) { HRESULT hr = S_OK; //initialize COM hr = CoInitialize(NULL); if (FAILED (hr)) { MessageBox(NULL, L"Failed to initialize COM", L"novaPDF", MB_OK); return hr; } //create one NovaPdfOptions instance INovaPdfOptions *pNova = 0; hr = CoCreateInstance(__uuidof(NovaPdfOptions80), NULL, CLSCTX_INPROC_SERVER, __uuidof(INovaPdfOptions80), (LPVOID*) &pNova); if (FAILED(hr)) { MessageBox(NULL, L"Failed to create novaPDF COM object", L"novaPDF", MB_OK); return hr; } // initialize the NovaPdfOptions object to use with a printer licensed for SDK hr = pNova->Initialize(PRINTER_NAME, L""); if (SUCCEEDED(hr)) { LPWSTR pwsOldActiveProfileID = NULL; LPWSTR pwsNewProfileID = NULL; hr = pNova->GetActiveProfile(&pwsOldActiveProfileID); //create a new profile with default settings hr = pNova->AddProfile(PROFILE_NAME, PROFILE_IS_PUBLIC, &pwsNewProfileID); //load the newly created profile if (SUCCEEDED(hr) && pwsNewProfileID) { hr = pNova->LoadProfile(pwsNewProfileID); } else { MessageBox(NULL, L"Failed to create profile", L"novaPDF", MB_OK); return hr; } // set PDF document Title pNova->SetOptionString(NOVAPDF_DOCINFO_TITLE, L"My Doc Title"); //what type of save dialog to be shown (none, simple or extended) pNova->SetOptionLong(NOVAPDF_SAVE_PROMPT_TYPE, PROMPT_SAVE_SIMPLE); //you may choose a predefined folder like "My Documents" pNova->SetOptionLong(NOVAPDF_SAVE_FOLDER_TYPE, SAVEFOLDER_MYDOCUMENTS); //set a macro for the file name pNova->SetOptionString(NOVAPDF_SAVE_FILE_NAME, L"test_[N]"); //in case a file with the same name exists in the selected folder, choose what to do pNova->SetOptionLong(NOVAPDF_SAVE_FILEEXIST_ACTION, FILE_CONFLICT_STRATEGY_AUTONUMBER_NEW); //save profile changes hr = pNova->SaveProfile(); //set as active profile for printer pNova->SetActiveProfile(pwsNewProfileID); HANDLE hPrinter; PDEVMODEW pDevmode = NULL; PRINTER_DEFAULTS pd = { NULL, NULL, PRINTER_ACCESS_USE }; //start print job if (OpenPrinter(PRINTER_NAME, &hPrinter, &pd)) { //get default printer DEVMODE int nSize = DocumentProperties(NULL, hPrinter, PRINTER_NAME, NULL, NULL, 0); pDevmode = (PDEVMODEW)LocalAlloc(LPTR, nSize); DocumentProperties(NULL, hPrinter, PRINTER_NAME, pDevmode, NULL, DM_OUT_BUFFER); //set page size in DEVMODE pDevmode->dmPaperSize = DMPAPER_USER; pDevmode->dmPaperLength = 2970;//5940; pDevmode->dmPaperWidth = 2100;//4200; pDevmode->dmFields = DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH; DocumentProperties(NULL, hPrinter, PRINTER_NAME, pDevmode, pDevmode, DM_IN_BUFFER | DM_OUT_BUFFER); //Print a page HDC hDC = CreateDC(L"", PRINTER_NAME, NULL, pDevmode); DOCINFO docInfo = {sizeof(DOCINFO)}; //document name docInfo.lpszDocName = PDF_FILE_NAME; StartDoc(hDC,&docInfo); StartPage(hDC); // Draw text on page TextOut(hDC, 100, 80, PDF_TEXT, (int) wcslen(PDF_TEXT)); EndPage(hDC); EndDoc(hDC); DeleteDC(hDC); //print job sent to printer MessageBox(NULL, L"Print job finished", L"novaPDF", MB_OK); LocalFree(pDevmode); ClosePrinter(hPrinter); } else { WCHAR wsMessage[255]; wsprintf(wsMessage, L"OpenPrinter failed, error = %d", GetLastError()); MessageBox(NULL, wsMessage, L"novaPDF", MB_OK); } //restore default profile if (pwsOldActiveProfileID) { pNova->SetActiveProfile(pwsOldActiveProfileID); } //delete newly created profile pNova->DeleteProfile(pwsNewProfileID); //free memory CoTaskMemFree(pwsNewProfileID); CoTaskMemFree(pwsOldActiveProfileID); } else { MessageBox(NULL, L"Failed to initialize novaPDF Printer", L"novaPDF", MB_OK); } //release NovaPdfOptions pNova->Release(); CoUninitialize(); return 0; }