Limited time promo

Word OLE (Delphi PDF) - SDK sample

English en

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

Download now Buy licenses
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.
Convert MS Word document to PDF from Delphi using Word OLE automation.
The Word OLE (Delphi PDF) sample is a simple Windows console application that converts a MS Word document (*C:\Test.doc*) to PDF using Word OLE automation. The sample can be used to elaborate more complex code and applications with the novaPDF SDK.
Note: To be able to use the samples you must install novaPDF SDK as samples work only with it. Download it here: novaPDF SDK.


Sample Code:
program WordOLEDelphi;
{$APPTYPE CONSOLE}

uses
  ActiveX,
  Printers,
  ComObj,
  SysUtils,
  Dialogs,
  novaOptions in '..\..\..\include\novaOptions.pas',
  novapiLIB80_TLB in '..\..\..\include\novapiLIB80_TLB.pas';

const

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

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

var
  hr : HRESULT;
  pNova : INovaPdfOptions80;
  strOldActiveProfileID : WideString;
  strNewProfileID : WideString;
  Word : VARIANT;
  NewDoc : VARIANT;
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 NovaPdfOptions and pass printer name
  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 (Length(strNewProfileID) > 0) then begin
	pNova.LoadProfile2(strNewProfileID);
  end else begin
	System.Writeln('Failed to create profile');
	exit;
  end;

  // set PDF document Title
  pNova.SetOptionString2( NOVAPDF_DOCINFO_TITLE, 'Hello World Delphi Sample');
  // set resulting file name
  pNova.SetOptionString2(NOVAPDF_SAVE_FOLDER, 'C:\');
  //do not show prompt dialog
  pNova.SetOptionLong(NOVAPDF_SAVE_PROMPT_TYPE, PROMPT_SAVE_NONE);
  //if file exists, override
  pNova.SetOptionLong(NOVAPDF_SAVE_FILEEXIST_ACTION, FILE_CONFLICT_STRATEGY_OVERWRITE);
  //do not open document in PDF viewer
  pNova.SetOptionBool(NOVAPDF_ACTION_DEFAULT_VIEWER, 1);

  //save profile changes
  pNova.SaveProfile();

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

  //Print Word Document
  try
	pNova.InitializeOLEUsage('Word.Application');
	Word := CreateOleObject('Word.Application');
	Word.DisplayAlerts := 0;
	pNova.LicenseOLEServer();
	NewDoc:= Word.Documents.Open('C:\temp\Test.doc', False, True);
	NewDoc.PrintOut(False);
	NewDoc.Close(False);
	Word.Quit(False);
  except
	on E: Exception do
	  ShowMessage(E.Message);
  end;

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

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

  ActiveX.CoUninitialize();

end.