Jump to content
JDS2018

Open PDF File

Recommended Posts

Var

application, appParams, fileName,
  shellParams: string;

 

How to open PDF File in Delphi  using ShellExecute

 

application := 'AcroRd32.exe';
  appParams:= '/A "zoom=100=OpenActions"';
  fileName := 'C:\test.pdf';
  shellParams := Format('%s "%s"', [appParams, fileName]);

  ShellExecute(Handle, nil, PChar(application), PChar(shellParams), nil, SW_SHOW);

 

this not working, pdf open and says file cannot to be open,

but if i open manfully its open no issue

Share this post


Link to post

If you are going to start a new process, as your code does here, then you should use CreateProcess. ShellExecute is for invoking shell verbs. I mean, it will start new processes, but if you know you want to start a new process, you may as well do so rather than get a third party to do it.

 

If you are going to invoke a shell verb you should always use ShellExecuteEx. ShellExecute has been obsolete for over 20 years now. The primary benefit of ShellExecuteEx is that it handles errors in a rational manner.

 

As for your issue, perhaps it's just that AcroRd32.exe can't be found. What happens when you supply a complete path?

Share this post


Link to post

Nothing Happen,it wont open.

 i am not have that Experience with coding  i am just beginner, 

 

what is the best way to Open Pdf file when i give File Path ?

 

Can you help me please i am using Delphi 10.2

 

Share this post


Link to post

@JDS2018You should check return value for error.

 

You said opening the file manually works. Do you mean you entered the exact same command line in the command prompt or you just double clicked on the PDF file to open it? They are different operations...

 

By the way, you can use ShellExecuteEx (Or ShellExecute) with the "open" verb to do the exact same thing as double clicking on the file using Windows Explorer.

 

Share this post


Link to post
Guest
1 hour ago, JDS2018 said:

this not working, pdf open and says file cannot to be open,

Do you mean by "pdf open" the reader itself ?

 

in case the answer is 

No )

Then make sure ShellExecute is working

ShellExecute(Handle, nil, PChar('notepad'), nil, nil, SW_SHOW);

 

Yes )

Then start with you Command Prompt (cmd) and find the working format for Acrobat Reader to open a file and perform you needed action of zooming, only after that build your ShellExecute parameters, ( check if that double quote is needed) 

Share this post


Link to post
2 hours ago, FPiette said:

By the way, you can use ShellExecuteEx (Or ShellExecute) with the "open" verb to do the exact same thing as double clicking on the file using Windows Explorer.

Is that true? Doesn't double click in explorer map to the default verb, which may not be open. 

Share this post


Link to post
2 hours ago, JDS2018 said:

Nothing Happen,it wont open.

 i am not have that Experience with coding  i am just beginner, 

 

what is the best way to Open Pdf file when i give File Path ?

 

Can you help me please i am using Delphi 10.2

 

My advice is to show your code, a complete program, a console app say. This will include the call to CreateProcess or ShellExecuteEx. With the full path to the executable. And with error checking code included. 

Share this post


Link to post

Try this:

uses
  System.SysUtils, System.IOUtils, Winapi.Windows, Winapi.ShellAPI;

procedure OpenPdf(const AFileName: string);
var
  LURL: string;
begin
  LURL := TPath.GetFullPath(AFileName).Replace('\', '/', [rfReplaceAll]);
  LURL := 'file://' + LURL;
  ShellExecute(0, 'open', PChar(LURL), nil, nil, SW_SHOWNORMAL);
end;

 

  • Sad 1

Share this post


Link to post
6 hours ago, FPiette said:

By the way, you can use ShellExecuteEx (Or ShellExecute) with the "open" verb to do the exact same thing as double clicking on the file using Windows Explorer.

The correct way to invoke the default action is to set the verb parameter to nil, not 'open'.

Share this post


Link to post
Posted (edited)
3 hours ago, vfbb said:

 


  LURL := TPath.GetFullPath(AFileName).Replace('\', '/', [rfReplaceAll]);
  LURL := 'file://' + LURL;
  ShellExecute(0, 'open', PChar(LURL), nil, nil, SW_SHOWNORMAL);

 

Interesting idea, you intend to open PDF in the default browser, right ?

What prerequisites do you need for that, does all modern browser have PDF viewer already integrated ?

I think Firefox can do that without any AddOn, but I'm not so sure about all the others ( Chrome, Edge ).

Edited by Rollo62

Share this post


Link to post
28 minutes ago, Remy Lebeau said:

The correct way to invoke the default action is to set the verb parameter to nil, not 'open'.

Sure, but the OP ask to open the file, not execute the default action. For PDF files, the default action is to open the file, that's why double clicking a file in Windows Explorer has opened the PDF file for the OP. I just told the OP that using open verb will open the file like Explorer did and which is not the same as the command he was using.

Share this post


Link to post
3 hours ago, vfbb said:

Try this: I have try not working 😞


uses
  System.SysUtils, System.IOUtils, Winapi.Windows, Winapi.ShellAPI;

procedure OpenPdf(const AFileName: string);
var
  LURL: string;
begin
  LURL := TPath.GetFullPath(AFileName).Replace('\', '/', [rfReplaceAll]);
  LURL := 'file://' + LURL;
  ShellExecute(0, 'open', PChar(LURL), nil, nil, SW_SHOWNORMAL);
end;

 

 

Share this post


Link to post
58 minutes ago, Rollo62 said:

IWhat prerequisites do you need for that, does all modern browser have PDF viewer already integrated ?

I think Firefox can do that without any AddOn, but I'm not so sure about all the others ( Chrome, Edge ).

Edge, Firefox and Chrome open any pdf without addon.

  • Like 1

Share this post


Link to post
4 hours ago, FPiette said:

For PDF files, the default action is to open the file

This is simply not true. If depends entirely on how the file association has been registered. Which depends on which program is controlling that association. Easy to imagine that UltraHyperMegaPDF might do it differently from other programs. 

Share this post


Link to post
9 hours ago, David Heffernan said:

My advice is to show your code, a complete program, a console app say. This will include the call to CreateProcess or ShellExecuteEx. With the full path to the executable. And with error checking code included. 

Why is this so hard? 

Share this post


Link to post
5 hours ago, FPiette said:

Sure, but the OP ask to open the file, not execute the default action.

Technically, the OP asked how to execute a specific command, so shouldn't be using ShellExecute/Ex() at all.

5 hours ago, FPiette said:

For PDF files, the default action is to open the file, that's why double clicking a file in Windows Explorer has opened the PDF file for the OP.

Double-clicking a file in Explorer executes the default action.  And 'open' is not always the default action.  It depends on the file extension's actual registration in the Registry, whether the user or 3rd-party apps have customized it, etc (for instance, on my system, the default action of a PDF file is to pass the file to AVG for scanning before then opening it).

5 hours ago, FPiette said:

I just told the OP that using open verb will open the file like Explorer did

Which is incorrect, because that is not what Explorer does.  Maybe on YOUR system, perhaps.  Not 100% guaranteed on every other system.

Share this post


Link to post
Posted (edited)
9 hours ago, Remy Lebeau said:

Which is incorrect, because that is not what Explorer does. 

That is what the explorer do for that particular user as well as 99% percent of user.

Edited by FPiette

Share this post


Link to post
Posted (edited)
10 hours ago, David Heffernan said:

This is simply not true.

This is true for that particular user as well as 99% percent of user.

Edited by FPiette

Share this post


Link to post
1 hour ago, FPiette said:

This is true for that particular user as well as 99% percent of user.

So you agree with me then.

Share this post


Link to post
Posted (edited)
14 hours ago, FPiette said:

That is what the explorer do for that particular user as well as 99% percent of user.

Technically, when double-clicking on a file, Explorer will ask the Shell to invoke the file's default action, and the Shell will execute the "open" action if that is the default action.  Sure, this may be the case in 99% of the cases, but there is always that 1% that will be different.

 

Double-clicking on a file, and calling ShellExecute/Ex("open") on the file, are NOT the same thing, even if they usually end up performing the same action on most systems,  But they are still logically different operations.

Edited by Remy Lebeau

Share this post


Link to post

@Remy Lebeau I perfectly know all that. I said the the OP to use the "open" verb to open his PDF with ShellExecute and I told him that would be the same as double clicking on the file with Explorer. I'm sure at 99.999% that THIS PARTICULAR user has the open verb as default for PDF file so I said that the open verb will do the same as the double-click. I never said that it is not possible to tweak the verb associated with PDF file so that open is no more the default and that for an incredibly small amount of peoples double-click on PDF would not open it. This has nothing to do with the question I am answering. You are just confusing the OP with all this pointless discussion and you are not alone. We could have had this discussion if the OP had said that double clicking on the PDF didn't worked for him.

Share this post


Link to post
1 hour ago, FPiette said:

@Remy Lebeau I perfectly know all that. I said the the OP to use the "open" verb to open his PDF with ShellExecute and I told him that would be the same as double clicking on the file with Explorer. I'm sure at 99.999% that THIS PARTICULAR user has the open verb as default for PDF file so I said that the open verb will do the same as the double-click. I never said that it is not possible to tweak the verb associated with PDF file so that open is no more the default and that for an incredibly small amount of peoples double-click on PDF would not open it. This has nothing to do with the question I am answering. You are just confusing the OP with all this pointless discussion and you are not alone. We could have had this discussion if the OP had said that double clicking on the PDF didn't worked for him.

It's not pointless. It's really important to know how the system works. These details matter. Details are important when programming. Makes no sense to recommend the 99.99% correct option over the 100% correct option. Obviously it's not likely to go wrong, but once you know how the system works why on earth would anybody opt to make an intentional mistake?

  • Thanks 1

Share this post


Link to post
1 minute ago, David Heffernan said:

why on earth would anybody opt to make an intentional mistake?

What I said is perfectly correct. To open the PDF file, with ShellExecute (The question here), the open verb must be used. Using the nil verb would cause trouble in the 0.01% people having tweaked their system. The point is to open the PDF with ShellExecute, not what happens when anyone double click on a PDF in Windows Explorer. The only important thing - beside opening the PDF - is that when THAT PARTICULAR user, the OP, double click on a PDF it is opens the PDF. That's it. Anything else is out of topic and pointless. You are of course free to think otherwise but don't try to make me think like you do. Don't try to teach the whole operating system when someone has a simple problem, especially to a beginner, you'll just confuse him. At some point in the study, certain points of detail must be taken for granted. Later the student will understand all the details. Later is the most important word here.

 

In any case, we have something in common with Remy: when we have an idea, we defend it fiercely. The other people must have a good laugh, admitting that they are following the conversation. Or it bothers them deeply.

Share this post


Link to post
6 minutes ago, FPiette said:

Using the nil verb would cause trouble in the 0.01% people having tweaked their system.

It's not that people have tweaked the system. They've just chosen to associate the PDF extension with a program that chooses to use different verbs as default.

7 minutes ago, FPiette said:

The point is to open the PDF with ShellExecute, not what happens when anyone double click on a PDF in Windows Explorer

That contradicts your earlier points where you said double click on PDF is the same as invoking open verb.

Share this post


Link to post

If a user has modified the behaviour or installed an app that causes the "open" verb to behave differently than the default behaviour - that is a user problem, IMO.

If you want to open a .pdf file, "open" is the way to go.

  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×