20

I'm embedding an external file in my pdf with \usepackage{attachfile2} and\textattachfile[color=1 1 1]{description}{file.pdf}. How can I make the text look the same as it would if I had used the hyperref package and done something like \hyperref{file.pdf}{description}? (By look the same, I mean black text, cyan border that disappears on printing, hand icon on mouse over, name of file in a box on hover (at least in acrobat/reader).)

Jason Gross
  • 2,135

1 Answers1

19

I'd replace attachfile2 with a combination of embedfile and hypgotoe:

Instead of \textattachfile[color=0 0 0]{file.pdf}{description}, you'll have to use \embedfile{file.pdf}. This command does not create any visible entry, it just adds the file to the resulting PDF file (and Adobe Reader's attachment tab), so you can use the macro anywhere in your document.

In order to produce the clickable link, use the extended functionality of hypgotoe's \href command: To make "description" a link to the embedded file.pdf, the correct syntax is

\href{gotoe:embedded=file.pdf}{description}

(More information can be found in the hypgotoe manual.) The appearance of the links can be controlled with \hypersetup using the keys gotoecolor and gotoebordercolor.

A minimal example:

\documentclass{article}
\usepackage{embedfile}
\usepackage{hypgotoe}
\embedfile{file.pdf}
\begin{document}
\href{gotoe:embedded=file.pdf}{description}
\end{document}

Note that this solution only works with pdf(la)tex and only for embedded PDF files.

diabonas
  • 25,784
  • The problem with this solution is that it requires you to provide the file names you are embedding in two locations. – vy32 Sep 03 '11 at 15:11
  • 2
    @vy32 You can avoid specifying the file name twice by creating a new macro for this purpose: \newcommand*{\embedfileandcreatelink}[2]{\embedfile{#1}\href{gotoe:embedded=#1}{#2}} Use it like \embedfileandcreatelink{file.pdf}{description} – diabonas Sep 06 '11 at 16:17
  • I thought that \embedfile{} had to go in the preamble. Does it not? – vy32 Sep 07 '11 at 01:08
  • 3
    @vy32 You can safely use \embedfile anywhere in your document. The list of embedded files the PDF file contains is written at the very end of the document. – diabonas Sep 07 '11 at 09:57
  • 2
    Is there a way to get this to work with non-pdf files? – Jason Gross Feb 28 '15 at 03:56
  • @JasonGross Not with the GoToE links used in this answer, I'm afraid. There might be a solution using JavaScript, even though this will probably work only in Adobe Reader/Acrobat (not sure whether GoToE links are supported by more PDF viewers, though). I guess this would warrant a new question. – diabonas Feb 28 '15 at 05:35
  • Asked at http://tex.stackexchange.com/q/230581/2066 – Jason Gross Feb 28 '15 at 05:59
  • At least for evince 13.8.2 and okular 0.24.2 on Ubuntu Linux 16.04.5 I can report that these links will not trigger any action. – math Aug 20 '18 at 14:41
  • @math Yes, unfortunately Poppler, the PDF library used by these programs, does not support GoToE links. Generally, many of these interactive PDF features only work with Adobe Acrobat Reader. – diabonas Aug 20 '18 at 14:47
  • This works with Adobe Reader. However I could not make it work with Edge. – Maesumi Sep 11 '22 at 03:14