Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Open PDF to Page Number


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 6 - Last Post: Jul 7, 2014 11:10 PM Last Post By: Alain Bastien
Andrew McIsaac

Posts: 93
Registered: 3/12/13
Open PDF to Page Number  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 8:01 AM
Good Morning,

Using XE3 C++,

And the excellent code supplied by : Remy Lebeau (TeamB) to open a PDF in pdf viewer :

#include <shellapi.h>

void __fastcall TForm1::Button_Open_PDFClick(TObject *Sender)
{
// call the ANSI version of the function directly
ShellExecuteA(
NULL, "open",
"C:
DIR
pdf_document.pdf", // document to launch
NULL, // parms -- not used when launching a document
NULL, // default dir (don't care here)
SW_SHOWNORMAL
);
}

This works well to open the pdf.

Changed to have Name of PDF as a variable (which is selected from listbox) :

file_name="C:
DIR
pdf_document.pdf"; // Full code here was combined from a known DIR and String Selected from listbox
char *file_Dname = AnsiString(file_name).c_str();
then :

ShellExecuteA(
NULL,
"open",
file_Dname, // document to launch
NULL, // parms -- not used when launching a document
NULL, // default dir (don't care here)
SW_SHOWNORMAL
);

This works with the file name being selected from a listbox

What I can Not do is to get this to open the pdf at a certain page.

Googled :
tried many different versions of adding
"/A page=5 to the file name,or as a string in place of the second NULL, but so far unsuccessful.

Trying not to wreck the code supplied from Remy Lebeau too much :)

Anyone know how to open a pdf to a page ?

Thanks in Advance,
Andrew
Borja Serrano

Posts: 172
Registered: 1/10/13
Re: Open PDF to Page Number  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 8:50 AM   in response to: Andrew McIsaac in response to: Andrew McIsaac
If you know in advance where the PDF reader is located it wouldn't be difficult:

http://www.daniweb.com/software-development/c/threads/174040/open-pdf-files-at-a-certain-page
Andrew McIsaac

Posts: 93
Registered: 3/12/13
Re: Open PDF to Page Number  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 10:01 AM   in response to: Andrew McIsaac in response to: Andrew McIsaac
Yes,
ShellExecuteA(
NULL,
"open",
"\"C:
Program Files (x86)
Adobe
Reader 11.0\\Reader\\AcroRd32.exe\"",
"/A page=4 \"C:\\DIR\\pdf_file.pdf\"",
NULL, // default dir (don't care here)
SW_SHOWNORMAL
);

Worked great, thanks for your help
Andrew McIsaac

Posts: 93
Registered: 3/12/13
Re: Open PDF to Page Number  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 10:05 AM   in response to: Andrew McIsaac in response to: Andrew McIsaac
FYI

Also, can remove DIR from source attribute :

ShellExecuteA(
NULL,
"open",
"\"AcroRd32.exe\"",
"/A page=4 \"C:\\DIR\\pdf_file.pdf\"",//"page=3",
NULL,
SW_SHOWNORMAL
);

Thanks
Andrew
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: Open PDF to Page Number  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 1:02 PM   in response to: Andrew McIsaac in response to: Andrew McIsaac
Hi Andrew

Andrew McIsaac wrote:

file_name="C:
DIR
pdf_document.pdf"; // Full code here was combined from a
//known DIR and String Selected from listbox
char *file_Dname = AnsiString(file_name).c_str();
then :

ShellExecuteA(
NULL,
"open",
file_Dname, // document to launch
NULL, // parms -- not used when launching a document
NULL, // default dir (don't care here)
SW_SHOWNORMAL
);

Using XE3 you should really use UnicodeString and wchar_t* instead of
AnsiString and char* especially this line looks suspect:

char *file_Dname = AnsiString(file_name).c_str();

I think the buffer holding the AnsiString is deleted right after the
line is passed, because it runs out of scope. In other words you are
handing ShellExecuteA a string that no longer is yours.

   String file_name = listbox->Items->Strings[listbox->ItemIndex];
 
   ShellExecute( // or ShellExecuteW
       NULL,
       L"open",
       file_name.c_str(),  
       NULL,     	
       NULL,     	
       SW_SHOWNORMAL
     );


If you insist on using char* you should do like this instead:

   AnsiString file_Dname = listbox->Items->Strings[listbox->ItemIndex];
 
   ShellExecuteA( 
       NULL,
       "open",
       file_Dname.c_str(),  
       NULL,     	
       NULL,     	
       SW_SHOWNORMAL
     );


Best regards
Asger-P

MacroMenu: Store clips, make macros, mail templates, bogmarks and a lot more.
View the video: http://asger-p.dk/software/macromenu.php#QuickTour
Andrew McIsaac

Posts: 93
Registered: 3/12/13
Re: Open PDF to Page Number  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 1:57 PM   in response to: Andrew McIsaac in response to: Andrew McIsaac
Much cleaner.

Thank you,
Andrew
Alain Bastien

Posts: 153
Registered: 11/12/01
Re: Open PDF to Page Number  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 7, 2014 11:10 PM   in response to: Andrew McIsaac in response to: Andrew McIsaac
Cleanest but quite but pricey !! You don't even need Acrobat reader
installed
http://www.gnostice.com/eDocEngine_VCL.asp
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02