Delphi PDF - Hello World sample

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

DOWNLOAD novaPDF
Feb 21, 2019
  • NovaPDF 9.x
  • NovaPDF 8.x

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

Delphi PDF - Hello World sample

Hello World (Delphi PDF) sample is a simple Windows console application that prints one page with the "Hello World from Delphi!" text to the novaPDF Printer.
It demonstrates the basic use of the INovaPDFOptions interface. The printing job is made with calls to the global Printer object defined by Delphi. Text is printed using Canvas.TextOut method.
It generates a "Hello World.pdf" file in the working folder.

Important: To be able to use the samples you must install novaPDF SDK as samples work only with it. Download it here: novaPDF SDK.

Note: If you print an existing document using "ShellExecute()" function or you want to handle printing events, you should check the VCL Converter sample instead.

program HelloWorld;

{$APPTYPE CONSOLE}

uses
  ActiveX,
  Printers,
  Windows,
  novaOptions in '..\..\..\include\novaOptions.pas',
  novapiLIB80_TLB in '..\..\..\include\novapiLIB80_TLB.pas',
  Nova in 'Nova.pas';

const

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

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

  //PDF file name
  PDF_FILE_NAME   = 'HelloWorld_Delphi';

  //Print profile name
  PROFILE_NAME    = 'HelloWorld Delphi Profile';
  PROFILE_IS_PUBLIC = 0;

var
  hr : HRESULT;
  pNova : INovaPdfOptions80;
    strOldActiveProfileID : WideString;
    strNewProfileID : WideString;
  //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_NovaPdfOptions80, //CLSID_CNovaPdfSource,
                                 nil,
                                 CLSCTX_INPROC_SERVER,
                                 IID_INovaPdfOptions80,
                                 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
  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);

  //create a new profile with default settings
  pNova.AddProfile2(PROFILE_NAME, PROFILE_IS_PUBLIC, strNewProfileID);

  //load the newly created profile
  if SUCCEEDED(hr) and (Length(strNewProfileID) > 0) then begin
    pNova.LoadProfile2(strNewProfileID);
  end else begin
    System.Writeln('Failed to create profile');
    exit;
  end;

  // set novaPDF options
  // uncomment the function calls for the options you wish to set and change the options in nova.cpp unit
  //AddProfileOptions(pNova);
  //AddDocumentInformation(pNova);
  //AddViewerOptions(pNova);
  //AddLinksOptions(pNova);
  //AddAdvancedOptions(pNova);
  //AddGraphicsOptions(pNova);
  //AddEmbedFontsOptions(pNova);
  //AddBookmarksDefinitions(pNova);
  //AddSecurity(pNova);
  //AddSaveOptions(pNova);
  //AddAfterSaveActions(pNova);
  //AddEmailOptions(pNova);
  //AddWatermarkImage(pNova);
  //AddWatermarkText(pNova);
  //AddPageContentOptions(pNova);
  //AddOverlayOptions(pNova);
  //AddSignatureOptions(pNova);

  //save profile changes
  pNova.SaveProfile();

  //get current default profile id
  pNova.GetActiveProfile2(strOldActiveProfileID);
  //set as active profile for printer
  pNova.SetActiveProfile2(strNewProfileID);

  //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');

  //restore default profile
  pNova.SetActiveProfile2(strOldActiveProfileID);
  pNova.DeleteProfile2(strNewProfileID);
  //resore default printer
  pNova.RestoreDefaultPrinter();

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

  ActiveX.CoUninitialize();

end.