如何使用C++如何在特定页面或命名目的地打开PDF Shellexecute命令

发布于 2025-02-05 16:49:40 字数 1070 浏览 3 评论 0 原文

我正在尝试使用 shellexecute()在特定命名目标上打开 .pdf ,但我无法弄清楚应该如何格式化参数。我在这里使用的paramater是 pagew

有人尝试过吗?我找到了几个答案,但是他们没有我需要的帮助。

PS:仅打开 .pdf 工作正常。

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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故人的歌 2025-02-12 16:49:41

在评论中,您说:

我正在使用Adobe Acrobat Reader DC作为默认查看器。

根据这个杂技

您可以使用命令或URL打开PDF文档,该命令或URL完全指定要显示的内容(命名目的地或特定页面),以及如何显示(使用特定视图,scrollbars,scrollbars,bookmarks,notations或突出显示)。

大多数浏览器都支持URL的参数,并且可以通过编程打开PDF文档时使用。

这些参数中的许多可以传递给以下核心API函数(有关详细信息,请参见Acrobat和PDF Library API参考):

avdocopenfilewithparamstring
avdopenfilewithparamstring
avdocopenfrompddocwithparamstring

从命令shell打开PDF文档时,您可以使用/a 开关将参数传递给打开命令,并使用以下语法

< Acrobat Path> /a“<参数> =< value>'' “< pdf Path>“

例如:

acrobat.exe/a“ zoom = 1000”“ c:\ example.pdf”

在Mac OS中,您可以使用Apple事件打开PDF文档时使用这些参数。

我使用Adobe Acrobat Reader DC安装了DC,并通过查看Windows注册表中注册的方式,使用 Shellexecute(“ Open”)(“ open”),只需 .pdf file> file> file> file> 将无法在使用/a 参数时产生上面概述的命令语法。您传递给 lpparameters shelexecute()的参数都没关系,Acrobat将忽略它。

因此,我看到的方式有两个选择:

  • 通过使用 createProcess()直接运行Acrobat .exe 程序,指定完整的命令行您需要。

  • 使用 shellexecute()启动一个Web浏览器,以 .pdf 文件指定URL,以ACROBAT文档中概述的格式,如下:

您可以在单个URL中指定多个参数。将每个参数与ampersand(&)或磅(#)字符分开。动作在出现在URL中时从左到右处理和执行。

由于执行了所有指定的操作,因此以后的操作可能会覆盖先前动作的影响,因此使用正确的顺序很重要。例如,应在Zoom Action之前出现页面操作。

命令除了命名目的地的值外,命令不敏感。 URL中没有空格。

URL示例

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

我尚未尝试使用URL,但是此方法可能需要您运行本地HTTP服务器,或者至少使用文件: url,以打开本地。 PDF 通过URL文件。

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.

鞋纸虽美,但不合脚ㄋ〞 2025-02-12 16:49:41

在Windows上,您将在我的情况下使用命令级别的

应用程序/PDF处理程序运行Windows Windows,

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

其中包括片段(片段需要替换为%20),并且Windows可以将其转换为\,如果传统命令呼叫使用了file)

file:// /c:/users/asura/downloads/asuras.pdf#page=52

//i.sstatic.net/owt8a.png“ alt =“在此处输入图像说明”>

对语法可能会更加挑剔

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

Acrobat在称为Direct时, “ https://i.sstatic.net/ue9hh.png” rel =“ nofollow noreferrer”>

命名的目的地可能非常善变,需要准确地建造(避免连字符或空格),并针对观众特定,因此Adobes自己的文档是Yardstick测试的一个好指标。

在这里,Edge中的第三书标记为#parameters或第二名为#Contents

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

或使用命名为最终,但它的薄片。页面可靠。

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


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

听闻余生 2025-02-12 16:49:41

“ c:\ program文件(x86)\ adobe \ acrobat读取器dc \ reader /acrord32.exe” /a“ natydest = onenameddest”“ c:\ users \ users \ asur \ asur \ asur \ asur \ asur \ asur \ asur \ test.pdf”

上述解决方案或任何开放式动作在命名目的地而不是书签上。

"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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文