[pdftex] including pdf with layers

Alexander Grahn A.Grahn at fzd.de
Tue Sep 15 11:33:43 CEST 2009


Hi Carlos and Martin,

On Mon, Sep 14, 2009 at 10:13:47PM +0200, Martin Heller wrote:
> Carlos.General at gmx.de wrote:
>
>> Also, I found no way to create layers with pdftex. It would also be
>> possible to export the original image to several files and reassemble the
>> layers again later. Does anybody have a working example or package that
>> can handle that?
>
> See this post on creating optional contents groups with pdfLaTeX:
> <http://groups.google.com/group/comp.text.tex/browse_thread/thread/4eb3b173df26324/ca260ff378f405fe>
>

Martin refers to the ocg.sty package.

However, ocg.sty doesn't provide a means to create text links that allow
to show/hide the layers. The `Layers' pane of AdobeReader must be used.
It might be unavailable, e. g. in full screen mode.

Instead, you could use the animate.sty package (available on CTAN). It
doesn't povide a link creation command either, but the hack shown below
defines the missing user commands:

\ShowHideLayer{<animation number (zero based)>}{<Layer number (zero based)>}{<link text>}

to create links which show/hide *individual* layers and

\ShowHideAll{<animation number (zero based)>}{<link text>}

to create a link to show/hide *all* layers.

Given a set of graphics files, one for every layer, e. g. layer-0.pdf,
layer-1.pdf, layer-2.pdf, layer-3.pdf. It is embedded into the document
using \animategraphics from package animate.sty.

animate.sty internally counts the animations in a document, starting
at zero. This number must be passed as the first argument to the
commands given above (e. g. 0 for the first animated layer-set in the
document). The first of the commands above reqires the layer number
(also zero-based) as the second argument. For example `2' to control the
visibility of layer-2.pdf.

Below you find an (almost) working example. You have to provide the
graphics files layer-0.pdf through layer-3.pdf.

Alexander

\documentclass{article}
%----------------------------------------------------------------%
\usepackage{animate}
\usepackage{hyperref}
%----------------------------------------------------------------%

\makeatletter
% command to create a toggle link
\newcommand{\ShowHideLayer}[3]{%
  % #1: anim No. (zero-based),
  % #2: layer No. (zero-based),
  % #3: link text
  \leavevmode%
  \pdfstartlink user {
    /Subtype /Link
    /Border [\@pdfborder]%
    /A <<
      /S/JavaScript
      /JS (
        \if at anim@useocg%
          if(a#1.fr[#2].state==true){
            a#1.fr[#2].state=false;
          }else{
            a#1.fr[#2].state=true;
          }
        \else
          if (a#1.fr[#2].display==display.visible){
            a#1.fr[#2].display=display.hidden;
          }else{
            a#1.fr[#2].display=display.visible;
          }
          this.dirty=false;
        \fi
      )
    >>
  }#3%
  \pdfendlink%
}

% command to create a link to show/hide all layers
\newcommand{\ShowHideAll}[2]{%
  % #1: anim No. (zero-based),
  % #2: link text
  \leavevmode%
  \pdfstartlink user {
    /Subtype /Link
    /Border [\@pdfborder]%
    /A <<
      /S/JavaScript
      /JS (
        var countvisible=0;
        for(var i in a#1.fr){
          \if at anim@useocg
            if(a#1.fr[i].state==true){countvisible++;}
          \else
            if (a#1.fr[i].display==display.visible){countvisible++;}
          \fi
        }
        if(countvisible){
          for(var i in a#1.fr){
            \if at anim@useocg
              a#1.fr[i].state=false;
            \else
              a#1.fr[i].display=display.hidden;
              this.dirty=false;
            \fi
          }
        }
        else{
          for(var i in a#1.fr){
            \if at anim@useocg
              a#1.fr[i].state=true;
            \else
              a#1.fr[i].display=display.visible;
              this.dirty=false;
            \fi
          }
        }
      )
    >>
  }#2%
  \pdfendlink%
}
\makeatother

\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{center}
  %animated layer-set No. 0
  %                                          v-- frame rate ignored
  \animategraphics[width=0.6\linewidth,step]{1}{layer-}{0}{3}

  \ShowHideLayer{0}{0}{Show/hide layer 0}\\
  \ShowHideLayer{0}{1}{Show/hide layer 1}\\
  \ShowHideLayer{0}{2}{Show/hide layer 2}\\
  \ShowHideLayer{0}{3}{Show/hide layer 3}\\
  \ShowHideAll{0}{Show/hide all layers }
\end{center}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}


More information about the pdftex mailing list