-6

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.

Ammadeux
  • 325
  • 1
  • 5
  • 15
  • 2
    You're not trying to do something like this. Procedures don't return values, so there is no *Result* available. What you've assigned to *s* is nonsense; it's nothing close to the valid name of a PDF file. Post the **actual code** you've attempted to use, in a form that will actually compile. Also, this is a *question and answer* site, not *questionS*. If you have multiple questions, create multiple posts to ask them unless they're closely related. In this case they are not, because one is about opening a PDF which will work fine with the API, and the second is about embedding, which won't. – Ken White Jun 07 '16 at 23:58
  • Do you have the full version of Adobe Acrobat installed? The reason I ask is that if you do, it's possible to use its automation objects to open a PDF file and display it in a window hosted by your Delphi app. If that's what you want to do, post a new q asking specifically how to do that. There are other ways, but only this one will render the document as Acrobat does. – MartynA Jun 08 '16 at 09:27

3 Answers3

4

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Ken White
  • 123,280
  • 14
  • 225
  • 444
0

To embed a PDF, the first 2 thoughts that come to mind would be

1) search for a COM object that supports PDFs - a quick search produced this: http://www.biopdf.com/guide/com_interface.php, but there appear to be others.

2) Worst case you can embed a web panel that has HTML code along these lines in it:

<object data="test.pdf" type="application/pdf" width="500" height="300"> alt : <a href="test.pdf">test.pdf</a> </object>

Brian Riley
  • 926
  • 1
  • 7
  • 12
  • @ArnaudBouchez aren't they referring to OP's second question? – René Hoffmann Jun 08 '16 at 06:44
  • 1
    @RenéHoffmann I would never use a HTML panel for embedding a pdf within the app. The correct way is to use ActiveX IMHO, e.g. as https://www.youtube.com/watch?v=n8qDdal-6mY – Arnaud Bouchez Jun 08 '16 at 11:14
  • @ArnaudBouchez But you agree that a bad answer is not an irrelevant answer. Using a web panel (I didn't try this) or a TWebBrowser (which I know it works) might be not the best solution but they are one solution. – René Hoffmann Jun 09 '16 at 14:52
  • @RenéHoffmann Take a look at the answer added by the OP himself. And you will see that he was not speaking about embedding the PDF in the app, but about launching the official system app to open the PDF file. – Arnaud Bouchez Jun 10 '16 at 13:34
  • @RenéHoffmann At the time of the answer there was no code - please see the pre-edited OP question. Both were in reference to OP question #2. Lastly I agree that the web panel is a horrible solution, but if for some reason the OP cannot find a COM object that would render said document, an HTML panel would ultimately give the functionality requested - hence why I prefixed it with "worst case". – Brian Riley Jun 10 '16 at 14:21
0

thanks for the answers but I eventually got to it (Haven't been using Delphi for a couple of years now, forgot about the uses).

This was how: "On a button click, open a PDF file from a directory."

uses shellApi;

procedure TForm1.Button1Click(Sender: TObject);

begin
ShellExecute(Handle, 'open', 'C:\pathwaytopdf.pdf', nil, nil, SW_SHOWNORMAL);
end;
end.

Thank you for the answers.

Ammadeux
  • 325
  • 1
  • 5
  • 15