Hello World (C++) - SDK sample


Related Articles Add comment Email article Print article

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: nova PDF SDK.
Notice 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:

// HelloWorld.cpp
#include "stdafx.h" 

//Include novaPDF headers
#include "..\..\include\novaOptions.h"
#include "..\..\include\novapi.h"

//name of novaPDF Printer
#define PRINTER_NAME    L"novaPDF Pro v6"


//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(NovaPdfOptions), NULL, CLSCTX_INPROC_SERVER, __uuidof(INovaPdfOptions), (LPVOID*) &pNova);
if (FAILED(hr))
{
  MessageBox(NULL, L"Failed to create novaPDF COM object", L"novaPDF", MB_OK);
  return 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 = pNova->Initialize(PRINTER_NAME, L"<registration name>", L"<license key>");
hr = pNova->Initialize(PRINTER_NAME, L"", L""), L"";

if (SUCCEEDED(hr)) { 

  pNova->SetDefaultPrinter();
  // set optional PDF settings
  // create a temporary profile for the current print job,
  // in order to not modify the default profile settings
  pNova->AddProfile(PROFILE_NAME, PROFILE_IS_PUBLIC);
  // set PDF document Title
  pNova->SetOptionString(NOVAPDF_INFO_TITLE, L"Hello World Sample",
                         PROFILE_NAME, PROFILE_IS_PUBLIC);
  // set resulting file name
  pNova->SetOptionString(NOVAPDF_SAVE_FOLDER, L"",
                         PROFILE_NAME, PROFILE_IS_PUBLIC);
  pNova->SetOptionString(NOVAPDF_SAVE_FILE, PDF_FILE_NAME,
                         PROFILE_NAME, PROFILE_IS_PUBLIC);
  //do not show prompt dialog
  pNova->SetOptionLong(NOVAPDF_SAVE_PROMPT, 0,
                       PROFILE_NAME, PROFILE_IS_PUBLIC);
  //if file exists, override
  pNova->SetOptionLong(NOVAPDF_SAVE_CONFLICT_STRATEGY,
                       FILE_CONFLICT_STRATEGY_OVERWRITE,
                       PROFILE_NAME, PROFILE_IS_PUBLIC);
  // set active profile
  LPWSTR wsDefaultProfile = NULL;
   int    nDefProfilePublic = 0;
  pNova->GetActiveProfile(&wsDefaultProfile, &nDefProfilePublic);
  pNova->SetActiveProfile(PROFILE_NAME, PROFILE_IS_PUBLIC);

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

    //Print a page
    HDC hDC = CreateDC(L"", PRINTER_NAME, NULL, pDevmode);
    DOCINFO docInfo = {sizeof(DOCINFO)};
    // PDF document name and path
    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
  pNova->SetActiveProfile(wsDefaultProfile, nDefProfilePublic);
  pNova->DeleteProfile(PROFILE_NAME, PROFILE_IS_PUBLIC);
  CoTaskMemFree(wsDefaultProfile);
  //restore default printer
  pNova->RestoreDefaultPrinter();
}
else{
  MessageBox(NULL, L"Failed to initialize novaPDF Printer", L"novaPDF", MB_OK);
}

//release NovaPdfOptions
pNova->Release();
CoUninitialize();
return 0;
}

Related articles

Comments