Limited time promo

Delphi Sample - Convert Office Docs

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.
The Convert Office Docs sample is a simple Windows console application that converts some Microsoft Office documents (Word, Excel, Publisher, PowerPoint and Visio) using Convert... functions. It uses Office OLE automation so Microsoft Office has to be installed.
The sample documents converted are located in the "OfficeDocuments" folder in novaPDF SDK installation folder. The pdf files are saved in the same folder.
Hidden links and internal document links are converted in pdf hyperlinks (except for Excel documents).

Source code

program ConvertOfficeDocs;

{$APPTYPE CONSOLE}

uses
   ActiveX,
   Printers,
   Windows,
   sysutils,
   novaOptions in '..\..\..\include\novaOptions.pas',
   novaEvents in '..\..\..\include\novaEvents.pas',
   novapiLIB10_TLB in '..\..\..\include\novapiLIB<%VERSION_MAJOR%>_TLB.pas',
   program ConvertOfficeDocs;

const

//name of novaPDF Printer
PRINTER_NAME    = 'novaPDF SDK <%VERSION_MAJOR%>';

//PDF file name
PDF_FILE_NAME   = 'ConvertOffice_Delphi';

//Print profile name
PROFILE_NAME    = 'Convert Office Delphi';
PROFILE_IS_PUBLIC = 0;

var
   hr : HRESULT;
   pNova : INovaPdfOptions10;
   strOldActiveProfileID : WideString;
   strNewProfileID : WideString;
   bTimeout : Integer;
   strDocumentsPath : WideString;
   strDocPath : WideString;
   strHeadings : WideString;

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_NovaPdfOptions<%VERSION_MAJOR%>, 
                                 nil,
                                 CLSCTX_INPROC_SERVER,
                                 IID_INovaPdfOptions<%VERSION_MAJOR%>,
                                 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, '');

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

   //DO NOT OPEN PDF
   //an Open action is added by default in the profile - delete or disable this open action
   //pNova.DisableActionType(NOVA_ACTION_OPEN);

   // set novaPDF save options
   AddSaveOptions(pNova);

   //save profile changes
   pNova.SaveProfile();

   try
      //get current default profile id
      pNova.GetActiveProfile2(strOldActiveProfileID);
   except
        strOldActiveProfileID := '';
   end;
   //set as active profile for printer
   pNova.SetActiveProfile2(strNewProfileID);

   //Path for novaPDF 10 SDK sample documents
   strDocumentsPath := GetEnvironmentVariable('ALLUSERSPROFILE');
   strDocumentsPath := strDocumentsPath + '\Documents\novaPDF 10\SDK\OfficeDocuments\';

   // --------------------------------------------------
   // Convert Word document
   // Microsoft Office Word has to be installed
   // --------------------------------------------------
   //Document file path
   strDocPath := strDocumentsPath;
   strDocPath := strDocPath +'Word.docx';
   //style name | level | type
   strHeadings := 'Heading 1|1|0;Heading 2|2|0;Heading 3|3|0';
   pNova.LicenseApplication('WINWORD.EXE');
   pNova.LicenseApplication('SPLWOW64.EXE');
   pNova.ConvertWordDocument2(strDocPath, 1, 0, 1, 1, 1, 1, 1, 1, 1, 3, strHeadings);

   // --------------------------------------------------
   // Convert PowerPoint document
   // Microsoft Office PowerPoint has to be installed
   // --------------------------------------------------
   //Document file path
   strDocPath := strDocumentsPath;
   strDocPath := strDocPath +'PowerPoint.pptx';
   pNova.LicenseApplication('POWERPNT.EXE');
   pNova.LicenseApplication('SPLWOW64.EXE');
   pNova.ConvertPowerPointDocument2(strDocPath, 1, 1, 1, 1, 1, 1);

   // --------------------------------------------------
   // Convert Publisher document
   // Microsoft Office Publisher has to be installed
   // --------------------------------------------------
   //Document file path
   strDocPath := strDocumentsPath;
   strDocPath := strDocPath +'Publisher.pub';
   pNova.LicenseApplication('MSPUB.EXE');
   pNova.LicenseApplication('SPLWOW64.EXE');
   pNova.ConvertPublisherDocument2(strDocPath, 1, 1, 1, 1, 1, 1);

   // --------------------------------------------------
   // Convert Excel document
   // Microsoft Office Excel has to be installed
   // hidden links in Excel documents will not be converted to pdf links
   // --------------------------------------------------
   //Document file path
   strDocPath := strDocumentsPath;
   strDocPath := strDocPath +'Excel.xlsx';
   pNova.LicenseApplication('EXCEL.EXE');
   pNova.LicenseApplication('SPLWOW64.EXE');
   pNova.ConvertExcelDocument2(strDocPath, 1);

   // --------------------------------------------------
   // Convert Visio document
   // Microsoft Office Visio has to be installed
   // --------------------------------------------------
   //Document file path
   strDocPath := strDocumentsPath;
   strDocPath := strDocPath +'Visio.vsd';
   pNova.LicenseApplication('VISIO.EXE');
   pNova.LicenseApplication('SPLWOW64.EXE');
   pNova.ConvertVisioDocument2(strDocPath, 1, 1, 1, 1, 1);

   if Length(strOldActiveProfileID) > 0 then begin
      //restore default profile
      pNova.SetActiveProfile2(strOldActiveProfileID);
   end;   
   
   pNova.DeleteProfile2(strNewProfileID);

   ActiveX.CoUninitialize();
end.