Graphics, Figures & TablesEmbed clickable Pictures in a PDF Document

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Embed clickable Pictures in a PDF Document

Post by Cham »

I'm wondering if this is possible in pdfLaTeX, and if it is, how can we do it ?

Is there a way to place a small clickable icon in a PDF such that, once clicked on it, it brings a large picture to the front (using the default OS viewer) ?

Ideally, the large picture should be included in the PDF document, but stay invisible by default. The clickable visible icon is the "door" to the large picture.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

alex
Posts: 32
Joined: Mon May 19, 2008 10:53 am

Embed clickable Pictures in a PDF Document

Post by alex »

It is possible.

Here is a solution based on JavaScript. It requires AdobeReader, though. There is only one image file, the final one at final resolution, to be embedded. It is scaled down to the size of a thumbnail using the `scale` option of `\includegraphics`. When it is clicked on, it will be enlarged to fit into the document window of the Reader, when clicked again, Reader zoomes out again.

The code example below defines the command `\zoombox[<box line width>]{<contents>}` where the optional `<box line width>` controls the width of the dotted line around the zoom box to be created. The content for zooming can be arbitrary material, such as text or a downscaled graphic, and is put into the mandatory second argument of `\zoombox`.

Code: Select all

% (C) 2011, Alexander Grahn, Lic: Public Domain
\documentclass{article}
\usepackage{graphicx}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newsavebox\zbox
\newcounter{zoom}
\newcommand{\zoombox}[2][0]{%
  \leavevmode%
  \sbox\zbox{#2}%
  \pdfdest name {zb\thezoom.out} fit
  \pdfdest name {zb\thezoom.in} fitr
    width  \the\wd\zbox\space
    height \the\ht\zbox\space
    depth  \the\dp\zbox\space
  \immediate\pdfannot
    width  \the\wd\zbox\space
    height \the\ht\zbox\space
    depth  \the\dp\zbox\space
  {%
    /Subtype/Link/H/N
    /Border [0 0 #1 [1 2]]
    /A <<
      /S/JavaScript
      /JS (
        if(typeof(zoomed\thezoom)=='undefined'){
          this.gotoNamedDest('zb\thezoom.out');
          var zoomed\thezoom=false;
        }
      )
      /Next <<
        /S/GoTo/D (zb\thezoom.in)
        /Next <<
          /S/JavaScript
          /JS(
            if(zoomed\thezoom){
               this.gotoNamedDest('zb\thezoom.out');
               zoomed\thezoom=false;
            }else{
               zoomed\thezoom=true;
            }
          )
        >>
      >>
    >>
  }%
  \usebox{\zbox}%
  \stepcounter{zoom}%
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\zoombox{\includegraphics[scale=.05]{tiger}}
\end{document}
zoombox.pdf
Example document from LaTeX source code listed above.
(39.92 KiB) Downloaded 453 times
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Embed clickable Pictures in a PDF Document

Post by Cham »

I tried your code above. After compilation, I get a PDF document with a thumbnail, but it doesn't do anything when I click on it. It doesn't work ! :-( (I'm not using AcrobatReader. I'm using Preview, in OS X).

Is there an "universal" way to define a "zoombox" in pdfLaTeX ?
alex
Posts: 32
Joined: Mon May 19, 2008 10:53 am

Embed clickable Pictures in a PDF Document

Post by alex »

Cham wrote:Is there an "universal" way to define a "zoombox" in pdfLaTeX ?
As I said, a PDF Reader with JavaScript interpreter is needed for the above example to fully work. The most reliable one is Adobe Reader, Evince (open-source) or Foxit may work as well but I didn't test.

Any PDF reader should at least fulfil the PDF specification and allow for zooming-in on the first click. The JavaScript in my example is only needed to zoom-out on the second click. It seems that Preview is not a conforming PDF reader, something that Adobe cannot be blamed for.
Last edited by alex on Thu Aug 04, 2011 9:28 am, edited 1 time in total.
Post Reply