Limited time promo

C++ Sample - Override Options

English

This help topic applies only to novaPDF. If you don't have it yet, you must download it first.

Download now Buy licenses
Mar 22, 2022
novaPDF 11.x
We strive to keep our help as accurate as possible. If you notice any inconsistencies or outdated info please let us know.
Override Options sample is a simple Windows console application that demonstrates the basic use of the INovaPDFOptions interface, without using profiles options. Instead of creating a profile and setting options in the profile, the sample uses SetOverrideOptions... functions to quickly set some options that will overwrite active profile settings. This is useful if you have a simple application where you just wish to change the pdf file name, email address or other simple options for each printed document.
There is a limited set of options that can be set with SetOverrideOptions... functions, usually settings that users can also change from the Save As dialog or the other prompt dialogs that can be shown before printing a document. If you need more complex settings, like adding a watermark, overlay or signature you have to configure them in a profile.
You can combine usage of profiles and override options functions. For instance you can create a profile with a watermark that is set as active and then use the override function just to change the file name for each document.
Once set, override options are taken in account for all printed documents until they are removed with DeleteOverrideOptions function.
The sample prints one page with a simple text to the novaPDF SDK and generates a "OverrideOptions.pdf" file in the C:\temp folder.

Source code

#include "stdafx.h"

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

//NovaPdfOptions
#include "..\..\..\..\include\novapi.h"

//name of novaPDF Printer Demo
#define PRINTER_NAME    L"novaPDF SDK 11"

//text to be written in the PDF file
#define PDF_TEXT        L"Override options sample"

//PDF file name
#define PDF_FILE_NAME   L"OverrideOptions.pdf"

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
	INovaPdfOptions11 *pNova = 0;
	hr = CoCreateInstance(__uuidof(NovaPdfOptions11), NULL, CLSCTX_INPROC_SERVER, __uuidof(INovaPdfOptions11), (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
	// if you have an application license for novaPDF SDK, 
	// pass the license key to the Initialize() function
	hr = pNova->Initialize(PRINTER_NAME, L"");

	if (SUCCEEDED(hr)) 
	{
		//Ovveride options from active profile
		
		//Save options
		//do not show prompt save dialog
		pNova->SetOverrideOptionLong(NOVAPDF_OVERRIDE_SAVE_TYPE, PROMPT_SAVE_NONE);
		//save the pdf in next folder
		pNova->SetOverrideOptionLong(NOVAPDF_OVERRIDE_SAVE_FOLDER_TYPE, SAVEFOLDER_CUSTOM);
		pNova->SetOverrideOptionString(NOVAPDF_OVERRIDE_SAVE_FOLDER, L"C:\\temp");
		pNova->SetOverrideOptionString(NOVAPDF_OVERRIDE_SAVE_FILE, PDF_FILE_NAME);
		//access folder, if restricted
		pNova->SetOverrideOptionEncryptedString(NOVAPDF_OVERRIDE_SAVE_USER, L"user");
		pNova->SetOverrideOptionEncryptedString(NOVAPDF_OVERRIDE_SAVE_PASSWORD, L"password");
		//if a file with the same neme already exists
		pNova->SetOverrideOptionLong(NOVAPDF_OVERRIDE_SAVE_CONFLICT, FILE_CONFLICT_STRATEGY_OVERWRITE);

		//disable open PDF in viewer
		//pNova->SetOverrideOptionBool(NOVAPDF_OVERRIDE_OPEN_VIEWER, FALSE);

		//Embed fonts
		pNova->SetOverrideOptionBool(NOVAPDF_OVERRIDE_EMBEDFONTS, TRUE);

		//Set compression level
		//pNova->SetOverrideOptionLong(NOVAPDF_OVERRIDE_COMPRESSION, COMPRESSION_HIGHQUALITY);

		//Merge PDF
		//Enable merge
		/*
		pNova->SetOverrideOptionBool(NOVAPDF_OVERRIDE_MERGE_PDF, TRUE);
		//Merge with the same file, if the file exists in destination OR merge with another file
		pNova->SetOverrideOptionLong(NOVAPDF_OVERRIDE_MERGE_FILE_TYPE, MERGE_FILE_OTHER);
		//Append to existing PDF, insert at the beginning or insert at a page number
		pNova->SetOverrideOptionLong(NOVAPDF_OVERRIDE_MERGE_TYPE, MERGE_TYPE_APPEND);
		//if merge with other file, specify file name (and path)
		pNova->SetOverrideOptionString(NOVAPDF_OVERRIDE_MERGE_FILE, L"invoice-text.pdf");
		//if merge type is set to insert at page number
		//pNova->SetOverrideOptionLong(NOVAPDF_OVERRIDE_INSERT_PAGENO, 1);
		//if the existing PDF is encrypted
		//pNova->SetOverrideOptionEncryptedString(NOVAPDF_OVERRIDE_PDF_PASSWORD, L"password");
		*/

		//Document info
		pNova->SetOverrideOptionBool(NOVAPDF_OVERRIDE_DOC_INFO, TRUE);
		pNova->SetOverrideOptionString(NOVAPDF_OVERRIDE_DOC_TITLE, L"sdk title");
		pNova->SetOverrideOptionString(NOVAPDF_OVERRIDE_DOC_SUBJECT, L"sdk subject");
		pNova->SetOverrideOptionString(NOVAPDF_OVERRIDE_DOC_AUTHOR, L"sdk author");
		pNova->SetOverrideOptionString(NOVAPDF_OVERRIDE_DOC_KEY, L"sdk key");
		pNova->SetOverrideOptionString(NOVAPDF_OVERRIDE_DOC_CREATOR, L"sdk creator");

		//Encrypt PDF
		/*
		pNova->SetOverrideOptionBool(NOVAPDF_OVERRIDE_SECURITY, TRUE);
		pNova->SetOverrideOptionEncryptedString(NOVAPDF_OVERRIDE_SECURITY_U, L"user");
		pNova->SetOverrideOptionEncryptedString(NOVAPDF_OVERRIDE_SECURITY_O, L"owner");
		*/

		//Email
		
		pNova->SetOverrideOptionBool(NOVAPDF_OVERRIDE_SEND_EMAIL, TRUE);
		pNova->SetOverrideOptionString(NOVAPDF_OVERRIDE_REC_TO, L"to");
		pNova->SetOverrideOptionString(NOVAPDF_OVERRIDE_REC_FROM, L"from");
		pNova->SetOverrideOptionString(NOVAPDF_OVERRIDE_REC_CC, L"cc");
		pNova->SetOverrideOptionString(NOVAPDF_OVERRIDE_REC_BCC, L"bcc");
		

		//register for evants from novaPDF so we can wait until the document is printed
		pNova->RegisterNovaEvent(NOVAPDF_EVENT_ACTIONS);

		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;
			pDevmode->dmPaperWidth = 2100;
			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); 
		}

		//wait for the document to be finished before printing other documents
		BOOL bTimeout;
		pNova->WaitForNovaEvent(120000, &bTimeout);

		//remove override options
		pNova->DeleteOverrideOptions();
	}

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