[Tex/LaTex] Attach displayed image to the PDF without storing it twice

embeddinggraphicspdf

If one and the same image is included in a PDF multiple times it is only stored only once as an PDF object. Now I like to also attach an image which is shown in the document to the PDF to allow users to copy the file. I'm aware that it is sometimes possible to copy images from the PDF anyway, but AFAIK this is limited to raster images like PNG and JPG, and not possible for included PDF images/diagrams.

I'm aware of the packages like embedfile and attachfile, but the tricky part is: Is it possible to display and attach/embed an already used image as PDF attachment without having it stored twice in the PDF?

Or in other words, the other way around: I like to add an image file as PDF attachment and then display this image file also in the document, without storing it again. AFAIK the PDF format should support this, but exactly how is the question. I don't mind a solution which uses some \pdfobj code.

Here a minimal example so that people can understand it better:

\documentclass{article}

\usepackage{graphicx}
\usepackage{embedfile}

\begin{document}

% Used twice, but only stored once:
\includegraphics[width=\textwidth]{tiger.pdf}
\includegraphics[width=\textwidth,angle=45]{tiger.pdf}

% Also added as attachment:
\embedfile{tiger.pdf}
% This should reuse the image file above, but adds `tiger.pdf` a second time

\end{document}

Example

Best Answer

I followed Martin Schröder's idea to use reference XObjects. Here is an example to show that it can be done, in a very limited way, for embedded PDF files only:

\documentclass{article}
\usepackage{embedfile}
\begin{document}
\embedfile[id={foo}]{tst.pdf}% Embed the file and give it a ID (foo) to refer to it later
Some text
\setbox0=\vbox to 3 in { % Give a message to people who don't see the reference
To see the correct image, please:
   0)~ensure you are using Adobe Reader 9.0 or later; 
   1)~set the preference ``Show reference XObject Targets'' (on the 
     ``Page Display'' tab) to ``Always''; 
   2)~Add the path of this PDF file to the ``Privileged Locations'' box on the
      ``Security (Enhanced)'' tab}
\pdfxform attr {/Ref <</F \embedfilegetobject{foo}{Filespec}/Page 0 >>}0
\pdfrefxform\pdflastxform
\end{document} 

For non-PDF files, this isn't possible: when a file is embedded in the PDF, the "embedded file stream" contains the complete, say, PNG file including metadata, and other "chunks". When you include an image for display, then only the image data itself is included as the image data stream (what would live in the PNG IDAT chunk), and the other information, metadata, etc, is represented in PDF syntax. I don't think there will be any way around this.

Related Question