\hyperref within included pdfs

Any interactive elements, such as links, embedded multimedia, PDF Layers, are stripped off while embedding a PDF by means of the usual commands from the graphicx and pdfpages packages.

There is an experimental package on CTAN, ↗pax, which attempts to re-create such elements using an external Java programme. But the project seems to be discontinued.

Currently, re-inserting interactive elements require a tedious, manual procedure, which can be simplified a bit by package onimage, introduced ↗here. It can be downloaded as onimage.dtx↗here. Run pdflatex twice on it to get onimage.sty and the documentation onimage.pdf.

onimage was designed to facilitate annotating embedded image files. Optionally it can show a grid of helper lines that greatly simplifies finding the link rectangle coordinates.

For convenience, we define \linkRect(<lowerleft>)(<upper right>){...} which can be used inside a tikzonimage or tikzpicture environment to create a link rectangle between (<lowerleft>) and (<upper right>), e. g. as

\linkRect(llx,lly)(urx,ury) {\href{URL}{~}};

Use ~ as a placeholder for the link text.

The Perl-Script extractURIs.pl listed below extracts all URI (i. e. external) links from a given uncompressed PDF and writes readily formatted

\linkRect(llx,lly)(urx,ury) {\href{URL}{~}};

commands to the terminal. For uncompressing, use pdftk commandline tool:

pdftk input.pdf output - uncompress | extractURIs.pl

enter image description here

\documentclass{article}

\usepackage{graphicx}
\usepackage{tikz}
\usepackage{hyperref}
\usepackage{onimage}

% use `~' as placeholder for the link text
% \linkRect(llx,lly)(urx,ury) {\href{URL}{~}};
\def\linkRect(#1)(#2){
  \coordinate (ll) at (#1); \coordinate (ur) at (#2);\def~{\tikz \useasboundingbox (ll) rectangle (ur);}
  \node [anchor=south west, inner sep=0] at (ll)
}

\begin{document}

  \begin{tikzonimage}[width=5cm]{example-image-a}[tsx/show help lines]
    \linkRect(0.35,0.25)(0.65,0.75) {\href{https://www.google.de/search?q=onimage.dtx&ie=utf-8&oe=utf-8&client=firefox-b&gfe_rd=cr&dcr=0&ei=6lW6Wq_yEt6CgAfZq5mgDA}{~}};
  \end{tikzonimage}

\end{document}

Perl scipt extractURIs.pl:

#!/usr/bin/perl -w

@linkRect=();
@linkUri=();
$isLink=0;
$isUri=0;

while(<>){
  if (/\/MediaBox\s+\[([\d\.\s]+\d)\]/) {
    @mediaBox=split /\s+/, $1;
  }
  elsif(/\/Subtype\s*\/Link/) {$isLink=1;}
  elsif(/\/Rect\s+\[([\d\.\s]+\d)\]/) {
    $rect=$1;
  }
  elsif(/\/URI\s+\((.*)\)\s*$/) {
    $uri=$1;
    $isUri=1;
  }
  elsif(/endobj/) {
    if($isUri && $isLink) {
      push @linkUri, $uri;
      push @linkRect, $rect;
    }
    $isUri=0;
    $isLink=0;
  }
}

$width=$mediaBox[2]-$mediaBox[0];
$height=$mediaBox[3]-$mediaBox[1];

for($i=0; $i<@linkUri; $i++) {
  ($llx,$lly,$urx,$ury) = split /\s+/, $linkRect[$i];
  $llx=($llx-$mediaBox[0])/$width;
  $urx=($urx-$mediaBox[0])/$width;
  $lly=($lly-$mediaBox[1])/$height;
  $ury=($ury-$mediaBox[1])/$height;

  print "\\linkRect($llx,$lly)($urx,$ury) {\\href{$linkUri[$i]}{~}};\n";
}

In your final document, you can add hyperlinks on top of the images/PDFs you include. Here is an example where I

  • draw a transparent rectangle with the size the hyperlink should take,
  • turn this rectangle into a hyperlink,
  • draw the hyperlink on top of an included image.
\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{hyperref}

\def\Put(#1,#2)#3{\leavevmode\makebox(0,0){\put(#1,#2){#3}}}

\begin{document}
    \href{www.google.de}{\includegraphics[width=5cm]{example-image-a}}
    \hspace{1cm}    
    \includegraphics[width=5cm]{example-image-a}\Put(-92,105){\href{www.google.de}{\tikz \fill [opacity=0] (0,0) rectangle (1.5,2) ;}}
\end{document}

enter image description here

This requires a lot of manually chosen dimensions, so it might be to cumbersome to use if you have plenty images of this type. However, as a quick fix it works.