Limited time promo

C++ Sample - Temporary printer

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 28, 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.
Temporary printer sample is similar with Hello World sample but it uses temporary printers.
It demonstrates the use of AddNovaPrinter and DeleteNovaPrinter.

Source code

#include "stdafx.h"

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

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

#include "nova.h"

//name of novaPDF Printer Demo
#define PRINTER_NAME    L"novaPDF temporary printer"
#define PORT_NAME    L"novaPDF<%VERSION_MAJOR%>temp"

//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<%VERSION_MAJOR%>), NULL, CLSCTX_INPROC_SERVER, __uuidof(INovaPdfOptions<%VERSION_MAJOR%>), (LPVOID*) &pNova);

	if (SUCCEEDED(hr)) 
	{
		//add temporary printer
		pNova->AddNovaPrinter(PRINTER_NAME, PORT_NAME, L"nPdfSdk<%VERSION_MAJOR%>_Softland", L"8501", L"");
		// set optional PDF settings 
		LPWSTR pwsNewProfileID = NULL;
		//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 novaPDF options 
		// 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;
			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)};
			// 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); 
		}

		//delete newly created profile
		pNova->DeleteProfile(pwsNewProfileID);
		//free memory
		CoTaskMemFree(pwsNewProfileID);
		//delete temporarry printer
		pNova->DeleteNovaPrinter(PRINTER_NAME);	}
	

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