Display novaPDF for SDK printing preferences from code

Post here any questions/suggestions you have about novaPDF SDK or novaPDF OEM.
Post Reply
zarkogajic
Posts: 15
Joined: Tue Nov 30, 2010 11:40 am

Display novaPDF for SDK printing preferences from code

Post by zarkogajic »

Hi,

Is there a way to display (call) the "Printing Preferences" dialog, for the "novaPDF for SDK" printer, from my code using the SDK?


-zarko

Claudiu (Softland)
Posts: 283
Joined: Wed Dec 16, 2009 12:46 pm

Re: Display novaPDF for SDK printing preferences from code

Post by Claudiu (Softland) »

Hello,

The "Printing Preferences" dialog for a printer can be opened by calling the DocumentProperties Windows API function. Here is the C++ code for this:

BOOL bPrinterOpened = OpenPrinter(pwszPrinterName, &hPrinter, NULL);
if (!bPrinterOpened)
{
wsprintf(wsMsg, L"Could not open printer (%s)", pwszPrinterName);
MessageBox(wsMsg);
}
else
{
// get the number of bytes required for the full DEVMODE structure by
// calling DocumentProperties and specifying zero in the fMode parameter
HWND hwndActiveWindow = ::GetActiveWindow();
LONG nBytesNeeded = ::DocumentPropertiesW(hwndActiveWindow, hPrinter, pwszPrinterName, NULL, NULL, 0);
pDevMode = (LPDEVMODE)malloc(nBytesNeeded);

// get in pDevMode the changes made by user to printer settings
LONG nReturnValue = ::DocumentPropertiesW(hwndActiveWindow, hPrinter, pwszPrinterName, pDevMode, NULL, DM_IN_PROMPT | DM_OUT_BUFFER);
if (nReturnValue < 0)
{
wsprintf(wsMsg, L"The printer is unavailable (%s)", pwszPrinterName);
MessageBox(wsMsg);
}
else if (IDOK == nReturnValue)
{
// if the user clicked on OK button, save the new printer settings
nReturnValue = ::DocumentPropertiesW(hwndActiveWindow, hPrinter, pwszPrinterName, pDevMode, pDevMode, DM_IN_BUFFER | DM_OUT_BUFFER);
if (IDOK != nReturnValue)
{
wsprintf(wsMsg, L"Could not save printer settings (%s)", pwszPrinterName);
MessageBox(wsMsg);
}
}
}
if (NULL != pDevMode)
{
free(pDevMode);
}
if (NULL != hPrinter)
{
ClosePrinter(hPrinter);
}

Thank you.
Follow us to stay updated:

zarkogajic
Posts: 15
Joined: Tue Nov 30, 2010 11:40 am

Re: Display novaPDF for SDK printing preferences from code

Post by zarkogajic »

Great, thanks!

Here's Delphi version:

Code: Select all

const
  PRINTER_NAME = 'novaPDF for SDK v7';
var
  hPrinter : Cardinal;
  hwndActiveWindow : HWND;
  nBytesNeeded, nReturnValue : integer;
  pDevMode : PDeviceMode;
begin
  if OpenPrinter(PRINTER_NAME, hPrinter, nil) then
  try
    // get the number of bytes required for the full DEVMODE structure by
    // calling DocumentProperties and specifying zero in the fMode parameter
    hwndActiveWindow := GetActiveWindow();
    nBytesNeeded := DocumentProperties(hwndActiveWindow, hPrinter, PRINTER_NAME, nil, nil, 0);
    pDevMode := AllocMem(nBytesNeeded);
    try
      // get in pDevMode the changes made by user to printer settings
      nReturnValue := DocumentProperties(hwndActiveWindow, hPrinter, PRINTER_NAME, pDevMode, nil, DM_IN_PROMPT OR DM_OUT_BUFFER);
      if nReturnValue < 0 then
      begin
        MessageDlg('Printer unavailable', mtError, [mbOK], -1);
      end
      else if nReturnValue = IDOK then
      begin
        // if the user clicked on OK button, save the new printer settings
        nReturnValue := DocumentProperties(hwndActiveWindow, hPrinter, PRINTER_NAME, pDevMode, pDevMode, DM_IN_BUFFER OR DM_OUT_BUFFER);
      end;
    finally
      FreeMem(pDevMode, nBytesNeeded);
    end;
  finally
    ClosePrinter(hPrinter);
  end
  else
    MessageDlg('Could not open printer', mtError, [mbOK], -1);
end;

-zarko

Post Reply