VCL Converter (Delphi PDF) - SDK sample

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

DOWNLOAD novaPDF
Feb 25, 2019

We strive to keep our articles as accurate as possible. If you notice any inconsistencies or outdated info please let us know.

VCL Converter (Delphi PDF) - SDK sample

The VCL Converter (Delphi PDF) sample demonstrates how to convert an existing file by printing it to novaPDF Printer using the ShellExecutefunction. It also demonstrates how to set different options and manage profiles in your novaPDF development.

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.

The same approach should be taken if you print using a Print() method from another object (like an internet browser or a report control). Just replace the ShellExecute call with the call of your Print method.

When the application starts, it creates a few profiles and makes different settings in the profiles. Then it shows a dialog from where the user can select the active profile and change its settings using the controls from the dialog. After that a document can be selected from the harddisk and printed to novaPDF Printer using the ShellExecute function call.

When using this technique to convert a file to PDF, you have to take care of the fact that ShellExecute prints to the default printer. This function returns immediately and does not wait until the print is finished (it may return before the printing is actually started).

Therefore you have to set the default printer to novaPDF Printer before calling ShellExecute (using the SetDefaultPrinter method), register FileSaved message (or any other novaPDF Printer message) to be sure that the print job was started. In this message handler restore the default printer (with the RestoreDefaultPrinter method). This way you made sure that the default printer was restored and your document is printed to novaPDF Printer.

Source Code Snippets

//1. DECLARE INovaPdfOptions variable
//declare an INovaPdfOptions member variable
PRIVATE
  m_novaOptions : INovaPdfOptions;

//2. Register <%SDK_SAMPLE_PRINTER%> messages
// register event messages
WM_NOVAPDF2_FILESAVED  := RegisterWindowMessage(MSG_NOVAPDF2_FILESAVED);
WM_NOVAPDF2_PRINTERROR := RegisterWindowMessage(MSG_NOVAPDF2_PRINTERROR);

// handle event messages
PUBLIC
  PROCEDURE WndProc(var Message: TMessage); override;
PROCEDURE TForm1.WndProc(var Message: TMessage);
BEGIN
  IF Message.Msg = WM_NOVAPDF2_FILESAVED then BEGIN
    // ...
  END ELSE IF Message.Msg = WM_NOVAPDF2_PRINTERROR then BEGIN
    // ...
  END ELSE BEGIN
    inherited WndProc(Message);
  END;
END;

//3. Initialize INovaPdfOptions
PROCEDURE TForm1.FormCreate(Sender: TObject);
BEGIN
  // ...

  // initialize COM libraries
  hr := ActiveX.CoInitialize(nil);
  if FAILED(hr) then begin
    MessageDlg('Failed to initialize COM' +#13+SysErrorMessage(hr) +#13+
               SysErrorMessage(GetLastError()), mtWarning, [mbOK], 0);
  end;

  //create an instance of INovaPdfOptions
  m_novaOptions := nil;
  hr := ActiveX.CoCreateInstance(CLASS_NovaPdfOptions80, //CLSID_CNovaPdfSource,
                                 nil,
                                 CLSCTX_INPROC_SERVER,
                                 IID_INovaPdfOptions80,
                                 m_novaOptions);
  if (FAILED(hr))    then begin
    MessageDlg('Failed to create novaPDF COM object', mtWarning, [mbOK], 0);
    exit;
  end;

  // initialize the NovaPdfOptions object to use with a printer licensed for SDK
  m_novaOptions.Initialize( PRINTER_NAME, '' );
  if (FAILED(hr))    then begin
    MessageDlg('Failed to initialize NovaPdfOptions', mtWarning, [mbOK], 0);
    exit;
  end;

  // add 2 profiles
  CreateProfiles();

  // load profiles in list
  LoadProfiles();
END;

//4. Release INovaPDFOptions
PROCEDURE TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
BEGIN
    //...
    //delete profiles
    m_novaOptions.DeleteProfile2( m_strSmallSizeProfileID );
    m_novaOptions.DeleteProfile2( m_strFullOptProfileID );

    // destroy m_novaOptions object
    //  - no need for this as the Delphi takes care of it automatically

    // uninitialize COM libraries
    ActiveX.CoUninitialize();   
    //...
END;

//5. Set <%SDK_SAMPLE_PRINTER%> Options
PROCEDURE TForm1.CreateProfiles();
BEGIN
    // Add a profile called "Small size"
    m_novaOptions.AddProfile2(SMALL_SIZE_PROFILE, PROFILE_IS_PUBLIC, m_strSmallSizeProfileID);
    m_novaOptions.LoadProfile2(m_strSmallSizeProfileID);

    // Set some options to this profile

    // disable the "Save PDF file as" prompt
    m_novaOptions.SetOptionLong(NOVAPDF_SAVE_PROMPT_TYPE, PROMPT_SAVE_NONE);
    // set generated Pdf files destination folder to the application path
    m_novaOptions.SetOptionLong(NOVAPDF_SAVE_FOLDER_TYPE, SAVEFOLDER_CUSTOM);
    m_novaOptions.SetOptionString2(NOVAPDF_SAVE_FOLDER,
                                       ExtractFilePath(Application.ExeName));
    // set output file name
    m_novaOptions.SetOptionString2(NOVAPDF_SAVE_FILE_NAME, 'PDF Converter small size.pdf');

    //Set other options and profiles
//...

END;

//6. Start a print job
PROCEDURE TForm1.btnStartPrintClick(Sender: TObject);
var
  hExec : HINST;
BEGIN

  //...

  // set the active profile to be used for printing
  m_novaOptions.SetActiveProfile2(strProfileId);

  // register our window to receive messages from the printer
  m_novaOptions.RegisterEventWindow(self.Handle);

  // set novaPDF as default printer, so it will be used by ShellExecute
  m_novaOptions.SetDefaultPrinter();

  // license the file to be converted with ShellExecute
  m_novaOptions.LicenseShellExecuteFile(efFileToConvert.Text);

  // print the document
  m_bPrintJobPending := TRUE;

  hExec := ShellAPI.ShellExecute(self.handle, 'print', PChar(efFileToConvert.Text),
                                 PChar(''), PChar(''), SW_HIDE);

  if (hExec <= 32) then begin // failed to execute program
    m_bPrintJobPending := FALSE;
    m_novaOptions.UnRegisterEventWindow();
    m_novaOptions.RestoreDefaultPrinter();
  end; 
END;

//7. Restore default printer when printing finished
PROCEDURE TForm1.WndProc(var Message: TMessage);
BEGIN
  if Message.Msg = WM_NOVAPDF2_FILESAVED then begin

    // restore original default printer
    m_novaOptions.UnRegisterEventWindow();
    m_novaOptions.RestoreDefaultPrinter();
    m_bPrintJobPending := FALSE;

  end else if Message.Msg = WM_NOVAPDF2_PRINTERROR then begin

    case (Message.WParam) of
      ERROR_MSG_TEMP_FILE : begin
        MessageDlg('Error saving temporary file on printer server', mtWarning, [mbOK], 0);
      end;
      ERROR_MSG_LIC_INFO : begin
        MessageDlg('Error reading license information', mtWarning, [mbOK], 0);
      end;
      ERROR_MSG_SAVE_PDF : begin
        MessageDlg('Error saving PDF file', mtWarning, [mbOK], 0);
      end;
      ERROR_MSG_JOB_CANCELED : begin
        MessageDlg('Print job was canceled', mtWarning, [mbOK], 0);
      end;
    end;
    // restore original default printer
    m_novaOptions.UnRegisterEventWindow();
    m_novaOptions.RestoreDefaultPrinter();
    m_bPrintJobPending := FALSE;

  end else begin

    inherited WndProc(Message);

  end;
END;