Link to home
Create AccountLog in
Avatar of futurelinksw
futurelinksw

asked on

Opening PDFs with Adobe Reader

We are currently having issues with PDF files being called. Below is the code that executes a PDF file from within the program. However we have a client where they have two versions of Adobe installed. (Normal Adobe Reader – Free, and Adobe Acrobat Standard - paid for).
We have checked the file extensions associated with PDFs and its set to open with the free version. However, when we try and open the PDF from within our program it opens up with the paid for version, which they only have a couple of licences for.
Is there any way we can force the PDF to open in the free version of adobe acrobat?

function stExecProgram(flname : string) : Boolean;
var
  ShellInfo : TShellExecuteInfo;
  msg, holdflname : string;
  res : DWord;
begin
  holdflname := flname;
  if (holdflname[1] = '"') then
  begin
    holdflname := copy(holdflname, 2, Length(holdflname) - 2);
  end;
  if (not FileExists(holdflname)) then
  begin
    MessageDlg('Unable to find File: ' + holdflname, mtError, [mbOK], 0);
    result := False;
    exit;
  end
  else
  begin
    FillChar(ShellInfo, SizeOf(TShellExecuteInfo), 0);
    ShellInfo.cbSize := SizeOf(TShellExecuteInfo);
    ShellInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
    ShellInfo.Wnd := HWnd_Desktop;
    ShellInfo.lpFile := pchar(holdflname);
    ShellInfo.lpVerb := 'OPEN';
    ShellInfo.nShow := SW_NORMAL; //sw_ShowMaximized;
    result := ShellExecuteEx(@ShellInfo);
    if (not result) then
    begin
      res := GetLastError;
      case res of
        ERROR_FILE_NOT_FOUND : msg := 'The specified file was not found';
        ERROR_PATH_NOT_FOUND : msg := 'The specified path was not found';
        ERROR_DDE_FAIL         : msg := 'The DDE transaction failed';
        ERROR_NO_ASSOCIATION : msg := 'There is no application associated with the given filename extension';
        ERROR_ACCESS_DENIED     : msg := 'Access to Document of Application Denied';
        ERROR_DLL_NOT_FOUND     : msg := 'Application Extension (DLL) Not Found';
        ERROR_CANCELLED         : msg := 'Operation Cancelled by User';
        ERROR_NOT_ENOUGH_MEMORY : msg := 'Insufficient Memory to Execute Application';
        ERROR_SHARING_VIOLATION : msg := 'A sharing violation occurred';
      end;
      MessageDlg(msg, mtError, [mbOK], 0);
    end;
  end;
end;
Avatar of jimyX
jimyX

You can try something like this:

ShellExecute(Handle,'open', 'C:\Program Files\Adobe\AcrReader.exe','c:\YourFile.pdf', nil, SW_SHOWNORMAL);
{Supply a fully qualified path name in ProgramName}
procedure ExecNewProcess(ProgramName : String);
var
  StartInfo  : TStartupInfo;
  ProcInfo   : TProcessInformation;
  CreateOK   : Boolean;
begin

  { fill with known state }
  FillChar(StartInfo,SizeOf(TStartupInfo),#0);
  FillChar(ProcInfo,SizeOf(TProcessInformation),#0);
  StartInfo.cb := SizeOf(TStartupInfo);

  CreateOK := CreateProcess(PChar(ProgramName),nil, nil, nil,False,
              CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS, 
              nil, nil, StartInfo, ProcInfo);

  { check to see if successful }
  if CreateOK then
    //may or may not be needed. Usually wait for child processes
    WaitForSingleObject(ProcInfo.hProcess, INFINITE);
end;

Open in new window

Please discard the last code snippet that I attached.

You only have to use this line:

ShellExecute(Handle,'open', 'C:\Program Files\Adobe\AcrReader.exe','c:\YourFile.pdf', nil, SW_SHOWNORMAL);

Please note:
"C:\Program Files\Adobe\AcrReader.exe" to be replaced with the path to the free version of adobe acrobat.

"c:\YourFile.pdf" this is your file that you want to open.


uses ShellApi;

function stExecProgram(PDFApp, flname : string) : Boolean;
begin
 ShellExecute(Handle,'open', PDFApp,flname, nil, SW_SHOWNORMAL);
end;

Open in new window

You can make your program to look up for the different Acrobat Reader Applications installed on the Clients' PC's and if more than to give and option to select which one the client wants to use to open the intended PDF file. Still figuring it out, will get back to you.

After trying the code, there is an update:

uses ShellApi;

function stExecProgram(PDFApp, flname : string) : Boolean;
begin
  ShellExecute(0,'open', PChar(PDFApp), PChar(flname), nil, SW_SHOWNORMAL);
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of futurelinksw

ASKER

Thanks all for your suggestions - I will be meeting with the developers again soon to discuss the options.

Kind regards.
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.