5

I'm embedding an external file in my pdf with \usepackage{attachfile2} and \textattachfile[color=1 1 1]{description}{file.txt}. How can I make the text look the same as it would if I had used the hyperref package and done something like

\label{here}%
\rlap{\hyperref{here}{\phantom{description}}}%
\textattachfile[color=0 0 0]{description}{file.txt}

?

I'm looking for something that includes the non-printing border, but only opens the file when clicked. I suppose I'd be ok with some hacky code that modifies the internals of hyperref or attachfile2. (I haven't figured out myself how to redefine \Hy@StartlinkName to use, e.g., user instead of goto link, because I don't know what /A and /Subtype to pass to user. I also haven't figured out how to modify the internals of attachfile2 to behave more like hyperref.)

Note: the solution to my previous question only works for embedded pdf files.

7

You can create a link to an embedded file using the JavaScript function exportDataObject. The following code creates a new command \embeddedfilelink{<file>}{<text>} that creates a link to an embedded file specified by its file name with the given link text. The code is taken from hypgotoe and modified to create a JavaScript action instead of a GoToE action.

\documentclass{article}

\usepackage{embedfile}
\usepackage{hyperref}

% How do I make an attached non-pdf file display like a link?
% (http://tex.stackexchange.com/q/230581)
\makeatletter
\newcommand*{\embeddedfilelink}[2]{%
  \begingroup
    \leavevmode
    \pdfstartlink
      attr{%
        \Hy@setpdfborder
        \ifx\@pdfhighlight\@empty
        \else
          /H\@pdfhighlight
        \fi
        \ifx\@filebordercolor\relax
        \else
          /C[\@filebordercolor]%
        \fi
      }%
      user{%
       /Subtype/Link%
       /A<<%
         /Type/Action%
         /S/JavaScript%
         /JS(this.exportDataObject({cName: "#1", nLaunch: 2}))%
       >>%
      }%
      \relax
    \Hy@colorlink\@filebordercolor#2%
    \close@pdflink
  \endgroup
}
\makeatother

\embedfile{file.txt}

\begin{document}
\embeddedfilelink{file.txt}{description}
\end{document}

appearance of link to embedded file

Clicking the link opens the embedded file after confirming the obligatory security warning

Adobe Reader security warning when trying to open an embedded file

Note that this solution only works with readers supporting JavaScript, which probably few viewers other than Adobe Reader/Acrobat do.

  • Good one..... :-) – karlkoeller Feb 28 '15 at 10:34
  • Gives me the error: Package xcolor Error: Undefined color '0 .5 .5' – zed111 Jun 8 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.