[Tex/LaTex] Attach files to PDF with dvipdfmx

dvipdfmxembedding

The attachfile package allows files to be embedded within the output PDF of a pdfLaTeX document, and the attachfile2 extends this for dvips use as well.

In an article, the author of dvipdfmx gives a recipe for doing the same thing with that driver instead:

\begin{filecontents*}{example.txt}
this is a text file to be embedded
\end{filecontents*}

\documentclass{article}
\begin{document}
test
\special{pdf:fstream @myfile (example.txt)}%
\special{pdf:ann bbox 0 0 10 10 <<
  /Subtype /FileAttachment /FS << 
    /Type /Filespec /F (example.txt)
      /EF << /F @myfile >> >> 
  /Name /PushPin >>}
\end{document}

However, when I compile this with latex+dvipdfmx, I get the following warning:

** WARNING ** Could not open file: example.txt
** WARNING ** Interpreting special command fstream (pdf:) failed.
** WARNING ** >> at page="1" position="(168.14, 707.125)" (in PDF)
** WARNING ** >> xxx "pdf:fstream @myfile (example.txt)"
]
** WARNING ** Object @myfile used, but not defined. Replaced by null.

Does anyone know what I'm going wrong? (TeX Live 2010, Mac OS X.) It would be great to be able to embed files using this method, since that would facilitate XeTeX use with the attachfile packages.

Update: things seem to work fine in TeX Live 2009. I'm thinking that this is a bug in the latest version of dvipdfmx. Any confirmation on this?

Best Answer

Thanks to Akira Kakuto on the TeX Live mailing list for the answer to this. The current directory is no longer searched for security reasons, so the relative path must be included in the filename:

\special{pdf:fstream @myfile (./example.txt)}%
\special{pdf:ann bbox 0 0 10 10 <<
  /Subtype /FileAttachment /FS << 
    /Type /Filespec /F (./example.txt)
      /EF << /F @myfile >> >> 
  /Name /PushPin >>}