Search code examples
delphidelphi-xe8

Delphi XE8 Load PDF File


I want to know how to do either:

  1. On a button click, open a PDF file from a directory.

  2. View a PDF file on the Form.


Solution

  • You don't need all of the jumping-through hoops you're doing. Windows will find the application associated with PDF files for you.

    procedure TForm1.Button1Click(Sender: TObject);
    var
      s: String;     
      Ret: DWord;
    begin
      s := 'C:\MyFiles\MyFile.pdf';
      Ret := ShellExecute(Handle, nil, PChar(s), nil, nil, SW_SHOW);
      if Ret < 32 then
        ShowMessage(SysErrorMessage(GetLastError));
    end;
    

    Note: Normally you should never call a WinAPI function without checking the return value. In this case, you'll know if it didn't work because the PDF won't open.