Limited time promo

Delphi 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 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.
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

program OverrideOptions;

{$APPTYPE CONSOLE}

uses
  ActiveX,
  Printers,
  Windows,
  novaOptions in '..\..\..\include\novaOptions.pas',
  novaEvents in '..\..\..\include\novaEvents.pas',
  novapiLIB10_TLB in '..\..\..\include\novapiLIB11_TLB.pas';

const

  //name of novaPDF Printer
  PRINTER_NAME    = 'novaPDF SDK 11';

  //text to be written in the PDF file
  PDF_TEXT        = 'Hello world from Delphi!';

  //PDF file name
  PDF_FILE_NAME   = 'HelloWorld_Delphi';

var
  hr : HRESULT;
  pNova : INovaPdfOptions11;
  bTimeout : Integer;
  //decomment next code if you use workaround for printer index (see below)
  //Device, Driver, Port: array[0..80] of Char;
  //DevMode: THandle;
begin

  //initialize COM
  hr := ActiveX.CoInitialize(nil);
  if (FAILED (hr) then begin
    System.Writeln('Failed to initialize COM');
    exit;
  end;

  //create one NovaPdfOptions instance
  pNova := nil;
  hr := ActiveX.CoCreateInstance(CLASS_NovaPdfOptions11, //CLSID_CNovaPdfSource,
                                 nil,
                                 CLSCTX_INPROC_SERVER,
                                 IID_INovaPdfOptions11,
                                 pNova);
  if (FAILED(hr))   then begin
    System.Writeln('Failed to create novaPDF COM object');
    exit;
  end;

  // 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
  pNova.Initialize2(PRINTER_NAME, '');

  pNova.SetDefaultPrinter();

  // now the default printer is novaPDF printer but the Printer object is not updated
  // here is a workaround to update the Printer object with the default printer
  // you only need this code if you check later on the Printer.PrinterIndex to find out the default printer
  //Printer.GetPrinter(Device, Driver, Port, DevMode);
  //Printer.SetPrinter(PRINTER_NAME, Driver, Port, 0);

  //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.SetOverrideOptionString2(NOVAPDF_OVERRIDE_SAVE_FOLDER, 'C:\temp');
  pNova.SetOverrideOptionString2(NOVAPDF_OVERRIDE_SAVE_FILE, PDF_FILE_NAME);
  //access folder, if restricted
  pNova.SetOverrideOptionEncryptedString2(NOVAPDF_OVERRIDE_SAVE_USER, 'user');
  pNova.SetOverrideOptionEncryptedString2(NOVAPDF_OVERRIDE_SAVE_PASSWORD, '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, 0);

  //Embed fonts
  pNova.SetOverrideOptionBool(NOVAPDF_OVERRIDE_EMBEDFONTS, 1);

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

  //Merge PDF
  //Enable merge
{
  pNova.SetOverrideOptionBool(NOVAPDF_OVERRIDE_MERGE_PDF, 1);
  //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, '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.SetOverrideOptionEncryptedString2(NOVAPDF_OVERRIDE_PDF_PASSWORD, 'password');
}
  //Document info
  pNova.SetOverrideOptionBool(NOVAPDF_OVERRIDE_DOC_INFO, 1);
  pNova.SetOverrideOptionString2(NOVAPDF_OVERRIDE_DOC_TITLE, 'sdk title');
  pNova.SetOverrideOptionString2(NOVAPDF_OVERRIDE_DOC_SUBJECT, 'sdk subject');
  pNova.SetOverrideOptionString2(NOVAPDF_OVERRIDE_DOC_AUTHOR, 'sdk author');
  pNova.SetOverrideOptionString2(NOVAPDF_OVERRIDE_DOC_KEY, 'sdk key');
  pNova.SetOverrideOptionString2(NOVAPDF_OVERRIDE_DOC_CREATOR, 'sdk creator');

  //Encrypt PDF
{
  pNova.SetOverrideOptionBool(NOVAPDF_OVERRIDE_SECURITY, 1);
  pNova.SetOverrideOptionEncryptedString2(NOVAPDF_OVERRIDE_SECURITY_U, 'user');
  pNova.SetOverrideOptionEncryptedString2(NOVAPDF_OVERRIDE_SECURITY_O, 'owner');
}
  //Email
  pNova.SetOverrideOptionBool(NOVAPDF_OVERRIDE_SEND_EMAIL, 1);
  pNova.SetOverrideOptionString2(NOVAPDF_OVERRIDE_REC_TO, 'to');
  pNova.SetOverrideOptionString2(NOVAPDF_OVERRIDE_REC_FROM, 'from');
  pNova.SetOverrideOptionString2(NOVAPDF_OVERRIDE_REC_CC, 'cc');
  pNova.SetOverrideOptionString2(NOVAPDF_OVERRIDE_REC_BCC, 'bcc');

  pNova.RegisterNovaEvent2(NOVAPDF_EVENT_ACTIONS);
  //start print job
  Printer.Title := PDF_FILE_NAME;
  Printer.BeginDoc();
  Printer.Canvas.Font.Size := 24;
  Printer.Canvas.TextOut( 100, 80, PDF_TEXT);
  Printer.endDoc();
  System.Writeln('Print job finished');

  pNova.WaitForNovaEvent(120000, bTimeout);

  //resore default printer
  pNova.RestoreDefaultPrinter();

  //release NovaPdfOptions
  //  pNova._Release();

  ActiveX.CoUninitialize();

end.