2

I am trying to open a .pdf at a specific named destination using ShellExecute(), but I couldn't figure out how the parameters should be formatted. The paramater I am using here is pagew.

Has anyone tried this before? I found a couple of answers, but they were not helpful as I need.

PS: opening just the .pdf works fine.

int main()
{
    std::string url = "\"C:/Users/asura/Downloads/asuras.pdf\"";
    std::wstring stemp = std::wstring(url.begin(), url.end());
    LPCWSTR sw = stemp.c_str();

    std::string action = "open";
    std::wstring atemp = std::wstring(action.begin(), action.end());
    LPCWSTR actiont = atemp.c_str();
    //1 INTRODUCTION

    string strPageDestination = "/A \"page=52\" \"pdf\"";
    std::wstring pagetemp = std::wstring(strPageDestination.begin(), strPageDestination.end());
    LPCWSTR pagew = pagetemp.c_str();
    //The line below works fine, it opens pdf with default pdf opener at first page.
    //ShellExecute(NULL, actiont, sw, NULL, NULL, SW_SHOWNORMAL);

    //The line below attempting to open file at specific page number doesn't work
    ShellExecute(NULL, actiont, sw, pagew, NULL, SW_SHOWNORMAL);
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
the_asura
  • 21
  • 3
  • Using `std::wstring(str.begin(), str.end())` is not the correct way to convert a `std::string` to a `std::wstring`. You need to *convert* the data, such as with `MultiByteToWideChar()`, `std::wstring_convert`, etc. Or, just start with wide string literals to begin with, eg: `std::wstring url = "\"...asuras.pdf\""; std::wstring action = L"open"; std::wstring strPageDestination = L"/A \"page=52\" \"pdf\"";` – Remy Lebeau Jun 08 '22 at 19:35
  • In any case, the input parameters you are allowed to specify are entirely dependent on the particular PDF viewer you are using, which you did not specify. There is no common set of parameters that every PDF viewer implements uniformly. – Remy Lebeau Jun 08 '22 at 19:37
  • So if I start with std::wstring, what would be the correctly formatted parameter to open the pdf at specific page or named-destination. ** std::wstring pagetemp = "/A \"page=52\" \"pdf\"; LPCWSTR pagew = pagetemp.c_str(); ** – the_asura Jun 08 '22 at 19:48
  • @RemyLebeau so I am using Adobe Acrobat Reader DC as default viewer. What would be the input parameter for Acrobat reader, ever Chrome's parameter would work. Would appreciate if anyone has the idea. – the_asura Jun 08 '22 at 19:52
  • Acrobat has a COM component, but I wouldn't recommend it, other options will be easier for your purpose at least – demberto Jun 08 '22 at 20:41

3 Answers3

0

In comments, you state:

I am using Adobe Acrobat Reader DC as default viewer.

According to this Acrobat documentation (and others like it that I found):

You can open a PDF document with a command or URL that specifies exactly what to display (a named destination or specific page), and how to display it (using such characteristics as a specific view, scrollbars, bookmarks, annotations, or highlighting).

The parameters for URLs are supported by most browsers, and can be used when opening PDF documents programmatically.

Many of these parameters can be passed to the following core API functions (see the Acrobat and PDF Library API Reference for details):

AVDocOpenFromFileWithParamString
AVDocOpenFromASFileWithParamString
AVDocOpenFromPDDocWithParamString

When opening a PDF document from a command shell, you can pass the parameters to the open command using the /A switch with the following syntax:

<Acrobat path> /A "<parameter>=<value>" "<PDF path>"

For example:

Acrobat.exe /A "zoom=1000" "C:\example.pdf"

In Mac OS, you can use the parameters when opening a PDF document with an Apple event.

I have Adobe Acrobat Reader DC installed, and from looking at the way it is registered in the Windows Registry, using ShellExecute("open") with just a .pdf file path WILL NOT be able to produce the command syntax outlined above that Acrobat requires when using the /A parameter. It doesn't matter what you pass in to the lpParameters parameter of ShellExecute(), it will be ignored by Acrobat.

So, the way I see it, you have two choices:

  • run the Acrobat .exe program directly by using CreateProcess(), specifying the complete command-line you need.

  • use ShellExecute() to launch a web browser specifying a URL to the .pdf file, in the format outlined in the Acrobat documentation, as follows:

You can specify multiple parameters in a single URL. Separate each parameter with either an ampersand (&) or a pound (#) character. Actions are processed and executed from left to right as they appear in the URL.

Because all specified actions are executed, it is possible that later actions will override the effects of previous actions, so it is important to use the correct order. For example, page actions should appear before zoom actions.

Commands are not case sensitive except for the value of a named destination. There can be no spaces in the URL.

URL examples

...
http://example.org/doc.pdf#page=3
...

I have not tried using a URL, but this approach may require you to run a local HTTP server, or at least use a file: url, in order to open a local .pdf file via a URL.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

On windows you would run at command level

Application/pdf handler in my case Windows Own

\"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe\" 

with unquoted URL argument including fragment (spaces require replace with %20 and windows can reverse the \ if they were used by a conventional command call)

file:///C:/Users/asura/Downloads/asuras.pdf#page=52

enter image description here

Acrobat can be a lot fussier about the syntax when called direct you place the /Action before the file name

"C:\SandBox\apps\PDF\Adobe\Reader\AcroRd32.exe" /A page=52 "C:\Users\asura\Downloads\asuras.pdf"

enter image description here

Named destinations can be very fickle, they need to be accurately built (avoid hyphens or spaces) and specific to viewer, so Adobes own documentation is one good indicator for yardstick test.

Here calling the 3rd bookmark in edge as simply #Parameters or 2nd as #Contents

file:///C:/Users/WDAGUtilityAccount/Desktop/SandBox/pdf_open_parameters_acro8.pdf#Parameters

or use nameddest but its flakey. Pages are reliable.

file:///C:/Users/WDAGUtilityAccount/Desktop/SandBox/pdf_open_parameters_acro8.pdf#nameddest=Contents

enter image description here enter image description here

K J
  • 8,045
  • 3
  • 14
  • 36
  • @K J does this command works for the named destination(nameddest parameter) too? I mean while opening on the the Microsoft Edge? Mine is not working.... – the_asura Jun 09 '22 at 17:44
  • I figured out it was working on somebody's, not sure but looks like it. https://i.stack.imgur.com/xpQ7o.gif So, I was wondering if it actually does. nameddest worked on chrome browser tho. So, it is pissing me off why it's not working on Edge. – the_asura Jun 09 '22 at 19:16
  • Found why I might have been facing the issue with updated browser. https://helpx.adobe.com/acrobat/kb/pdf-browser-plugin-configuration.html – the_asura Jun 09 '22 at 20:29
0

"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader/AcroRd32.exe" /A "nameddest=OneNamedDest" "C:\Users\asur\Downloads\Test.pdf" Named destination and Bookmarks are totally different for Acrobat. The above solution or any open action works on named destination not bookmarks.

the_asura
  • 21
  • 3