Hello World - SDK sample

Download Sample
Download size: 38.9KB
[Format: ZIP]
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.

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.

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 v5"

//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;
}

© 2002-2008 Softland. All Rights Reserved.
PDF Creator novaPDF
Create PDF for your paperless office
home Home   contact usContact us   partnershipsPartnerships   bookmarkBookmark   Site updated: 28-August-2008
Downloads Download Now

Download, install and use novaPDF. Latest version: 5.5 build 271

Download PDF creator
promotionsSpecial Offers

Current discounts:
- up to 25% for Desktop Editions
- up to 25% for Server Editions
- 20% Academic Discount
- up to 16% for Developer Tools

buyBuy Now
Professional edition $39.95
(Save $10)
Standard edition $29.95
(Save $10)
Lite edition $19.95
(Save $5)
Buy Now
newsLatest News

novaPDF 5.5 released
August 28, 2008

What’s new (read more):
 » Added Import/Export profiles option
 » Added external function exit code to cancel print job in novaPDF OEM
 » Added "/RegisterWin32COM" install parameter for silent installers
 » Added "/DoNotAskReason" command line parameter for silent uninstalling
 » Added new large printing forms (A0)
 » Added Norwegian and Turkish interface languages

sdkDeveloper Tools

novaPDF SDK is a software development toolkit that can be used by software developers and programmers to add the ability to create PDF files in their applications.
novaPDF OEM is a PDF solution that programmers can use to generate PDF files that will be post-processed by their applications. […]

Reviews & User QuotesPDF Creator Reviews
"novaPDF is a really neat tool. Basically, it’s a PDF printer driver that prints to a PDF file instead of paper.
See more reviews about nova PDF
[…] novaPDF is a great product that works without forcing the user to configure anything. The convenience alone is worth the money." […]
Jason Pullara, Editor
Currently supported languagesLanguages

News:
- Norwegian language added.

Help us translate novaPDF and we will give you licenses for novaPDF Professional and mention you on the Languages page. […]

awardsAwards

novaPDF received the Editor’s Pick from Soft32, a major download site:

PDF creator reviews

See the Awards page for more ratings.

NewsletterKnowledgebase articles

Latest articles on how to:
 » Create PDF files
 » Convert JPG to PDF
 » Convert Powerpoint to PDF
 » Convert Publisher to PDF
 » Convert web pages to PDF
Full article list in the knowledgebase.