954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Open PDF files at a certain page

Hi!
I'm using a C program to open PDF files with this code:

ShellExecute(GetDesktopWindow(), "open", "c:\\someFolder\\myFile.pdf",  NULL, NULL, SW_SHOWNORMAL);


Now I need to go furthrer; I need to open the PDF file at a certain page. I've tried

"c:\\someFolder\\myFile.pdf#page=5"...


...as is suggested in Adobe's docs, but this does not work.

Any hints?

marcosjp
Light Poster
43 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

EDIT:

On second thoughts, you are right.

Just a little change

ShellExecute(GetDesktopWindow(), "open", "c:\\someFolder\\myFile.pdf #page=4",  NULL, NULL, SW_SHOWNORMAL);


Just add a space, cause page=4 is an argument.

----------------
I don't know if there are anyreal ways to do it. But how about triggering some keyboard inputs with keybd_event()
All you have to do is Ctrl+Shift+N to open the "go to page" dialog box. and then the input the number 4(say) and also "Enter" with the same function. Before all this you have to give time for the pdf to get opened and become active(You may have to use the function Sleep())

You can get the complete set of Virtual key codes in msdn

Hope it helps

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 

The official way is with Win32 COM
(never use keybd_event, not professonal at all...)

marco93
Junior Poster
132 posts since Apr 2008
Reputation Points: -76
Solved Threads: 14
 
The official way is with Win32 COM (never use keybd_event, not professonal at all...)

I'll take a note of that.

And the actual answer is not either. Its just to give an argument to the pdf we are opening;)

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 

Well, so far I've not succeeded in making it work.
It is very easy to get the righ result when you open PDF files in a web browser: all you have to do is follow the PDF file name with the #page=n parameter (and without space).

However it does not work when calling the PDF from C/C++. I know it is possible, I've seen it work somewhere...

I'll post the solution here when I find it... :)

marcosjp
Light Poster
43 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

Sorry to have suggested an untested code.
My little Google search found me this

Have a look at it.

This time I have tested the code and it really works.

ShellExecute(GetDesktopWindow(), "open", "\"D:\\program files\\Adobe Reader 9.exe\"", "/A page=45 Ubuntu.Pdf", NULL, SW_SHOWNORMAL);


D:\Program files\Adobe Reader 9.exe is the path to the portable reader in my computer(I always try to get portable versions).

Ubuntu.pdf was in the same folder as my CPP program, hence I did not specify its path.

Details about parameter options could be found int the link above.

Good Luck

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 

THANK YOU!!!
Thank you VERY MUCH!
You saved my day!!!
Thanks!
All the Best!

marcosjp
Light Poster
43 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

Well.... I was too quick to celebrate...

It works fine in Win XP, but not on Vista.... oh well...

I wonder why!!

marcosjp
Light Poster
43 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

Hey Prabakar,

You mentioned you use a portable reader. Maybe this is a solution, where do I get it?

I've googled it and found so many different options... is there one from Adobe?

Thanks!

marcosjp
Light Poster
43 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

I do not have vista, to help you:( I don't know why it is not working in vista, these functions are ment to work on windows after all. Perhaps you'll have to wait for others to respond

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 

Hi Prabakar,

What I've found out is GetDesktopWindow() works a bit different in Vista: it does not accept the directory info in the third parameter. So you have to indicate the directory in the fifth parameter. Kind of an ugly solution, as I will have to place the PDF files in the same directory where the reader exe is. Not good, but this is the only solution I could find for the moment.
Thanks very much for your help!!

marcosjp
Light Poster
43 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

Same directory as the reader? Why not give the full address fo the file?

Like this,

ShellExecute(NULL, "open", "\"D:\\program files\\Adobe Reader 9.exe\"", 
				"/A page=45 \"C:\\Documents and Settings\\Prabakar\\My Documents\\Downloads\\How to Do Everything _ Ubuntu (McGraw-Hill).pdf\"", NULL, SW_SHOWNORMAL);
Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 
Same directory as the reader? Why not give the full address fo the file?

Paths mustnever be hard coded. You must get them from Shell.

marco93
Junior Poster
132 posts since Apr 2008
Reputation Points: -76
Solved Threads: 14
 
Paths must never be hard coded. You must get them from Shell.


That's what I always do, but, it looked like the OP was not satisfied with it.

What if he can't move it from a particular folder? ( happens often when it is a shared computer)

Copying the pdf and pasting it on a specific directory just to make your program work is not a good option

And adding the program to that directory might be irritating. It will be annoying to see a document in my songs folder.

Sometimes we need to use full address, or at least to use environment variables.;)

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You