[Tex/LaTex] How to create a link in a pdf document to open a file using a specified application installed on the machine

hyperreflinks

Assume I have a file on my machine at file:///c:\figure.bmp.

From my pdf document, that is created by LaTeX using hyperref.sty, how can I open figure.bmp using mspaint.exe (microsoft built-in image editor)?

What is the correct command to do this scenario?

If I call as follows

\href{file:///c:\figure.bmp}{To open the figure, click here.}

the figure.bmp will be opened using Illustrator (for example).

Actually I can change the file association to open bmp using mspaint instead of illustrator. But, I prefer to find another elegant approach, i.e., by specifying both file to be opened and the application used to open.

Best Answer

It looks like you want a "Launch" action; see Table 8.49 (p. 622) in Section 8.5.3 of the PDF Reference, version 1.6, or Table 8.53 (p. 660) in the same section of version 1.7.

Both versions can be obtained from the Adobe PDF Reference Archive; due to size, version 1.6 (8.8 MiB) might be preferred over version 1.7 (31 MiB). (The ISO standard should be avoided like the plague due to its poor typography.)

It looks like the easiest way to actually use this is to use the pdfLaTeX primitives \pdfstartlink and \pdfendlink; see the manual on the pdfTeX page.

Update:

Well, it didn't turn out to be particularly easy, but here is a complete example that almost works for me -- it seems to be interpreted correctly, but fails because (according to Reader) "it is currently disallowed by your system administrator".

(This might seem obvious, but the following is in the "plain" format, not LaTeX -- it's what they call a SpikeSolution.)

Oh, and a warning: I wouldn't suggest testing modifications of this in Adobe's Reader initially; when I tried it on a slightly malformed file, it would forget to close the file when I closed the window, so I would have to close Reader itself before I could re-run TeX. Instead, try it in gsview32, which has the added advantage of giving some sort of diagnostics rather than just not woring.

%&plain
\pdfoutput=1
\pdfcompresslevel=0 % for debuggable PDF output

% taken from http://tex.stackexchange.com/questions/7136/.../7139#7139
{
\catcode`\^0
\catcode`\\12
^gdef^dirsep{\}
}

% This is surprisingly tricky to get right; check the PDF file in a
% text editor to make sure the correct string was output! For example,
% I had {} instead of a space after the \dirsep, and that got copied
% to the output.
\def\figurepath{C:\dirsep figure.bmp}

See the file
\pdfstartlink
  attr {/C [0.9 0 0] /Border [0 0 2]}
  user {
  /Subtype /Link
  /A <<
    /Type /Action       % This is a PDF "Action" dictionary ...
    /S /Launch          % ... for a "Launch" action
    /Win <<             % Nested dictionary of Windows-only stuff
      /F (mspaint)      % Application
      % Parameters; parens to delimit it as a PDF string, quotes so
      % that spaces won't foul things up, and \pdfescapestring to deal
      % with any (, ), or \ characters in the path
      /P (\pdfescapestring{"\figurepath"})
    >>
  >>}
{\tt \figurepath}
\pdfendlink

\bye
Related Question