6

I am using pdfpages to tie several PDF documents together (yes, they must be separate documents) into a portfolio. My problem is that I need to place many, many links within one child document that link to the first page of other child documents (which are bookmarked in the outer document). Here is an MWE:

******ChildOne.tex:******

\documentclass{article}

\usepackage[bookmarksopenlevel=1,%
     pdfpagemode=UseOutlines]{hyperref}
\hypersetup{
    pdfinfo={
        Title={Child One},
        Author={Me}
    }
}

\author{Me}
\title{Child One}

\begin{document}
\maketitle
Here is where I want to refer to Child Two.

I can do it by \href[page=3]{OuterDoc.pdf}{absolute page reference},
but the zoom level changes, and I don't always know the absolute
page number ahead of time (because there are so many child documents
of varying lengths).

My preference would be to refer to Child Two automatically, and
\href{OuterDoc.pdf#nameddest=ChildTwo}{a named destination} doesn't
seem to work.

\end{document}

******ChildTwo.tex:******

\documentclass{article}

\usepackage{lipsum}

\usepackage[bookmarksopenlevel=1,%
     pdfpagemode=UseOutlines]{hyperref}
\hypersetup{
    pdfinfo={
        Title={Child Two},
        Author={Me}
    }
}

\author{Me}
\title{Child Two}

\begin{document}
\maketitle
\lipsum[1-5]
\end{document}

After compiling ChildOne.tex and ChildTwo.tex, I run pax on ChildOne.pdf (using the information in this question), and then compile OuterDoc.tex.

******OuterDoc.tex:******

\documentclass{article}

\author{Me}
\title{Big Title}

\usepackage{pdfpages}

\newcommand{\singlepageinpdf}{1}
\newcommand{\pagesinpdf}{1}

\newcounter{includepdfpage}
\newcounter{currentpagecounter}
\newcommand{\addlabelstoallincludedpages}[1]{%
   \refstepcounter{includepdfpage}%
   \stepcounter{currentpagecounter}%
   \label{#1.\thecurrentpagecounter}}
\newcommand{\modifiedincludepdf}[4]{%
    \setcounter{currentpagecounter}{0}%
    \includepdf[pages=1,pagecommand={\pdfbookmark[#1]{#2}{#3}\addlabelstoallincludedpages{#3}}]{#4}%
    \pdfximage{#4}%
    \renewcommand{\pagesinpdf}{\the\pdflastximagepages}%
    \if\pagesinpdf\singlepageinpdf%
      \relax%
    \else%
        \includepdf[pages=2-,pagecommand=\addlabelstoallincludedpages{#3}]{#4}%
    \fi%
}

\usepackage{pax}

\usepackage[bookmarksopenlevel=1,%
     pdfpagemode=UseOutlines,%
     pdfpagelayout=OneColumn,%
     pdfnewwindow=false,%
     pdfstartview=FitH]{hyperref}
\hypersetup{
    pdfinfo={%
        Title={Outer Doc},%
        Author={Me}%
    }%
}

\usepackage{bookmark}

\begin{document}
\bookmark[page=1, level=0]{Title Page}
{\begin{center}
\Huge \textbf{My Big Document}
\end{center}
}

\modifiedincludepdf{0}{First Child}{ChildOne}{ChildOne.pdf}
\modifiedincludepdf{0}{Second Child}{ChildTwo}{ChildTwo.pdf}

\end{document}

Because my \href[page=3]{OuterDoc.pdf}{absolute page reference} link works correctly, it looks to me like the linking system, including pax, is working as expected. However, \href{OuterDoc.pdf#nameddest=ChildTwo}{a named destination} doesn't give me any joy.

Am I possibly out of luck because a bookmark is not a valid destination?? (And if so, what brilliant person out there can think of a workaround?)

An additional (but small) problem is that the pdfstartview=FitH option of hyperref (2012/11/06 v6.83m) doesn't load the page in fitted width (for the [page=3] link) -- it zooms to the whole-page view.

4
  • In OuterDoc.tex, create the named destination by adding \hypertarget{#3}{} to the pagecommand key in \modifiedincludepdf. (I haven't bothered pasting the whole file with just that one change.)

  • Linking to the named destination with \hyperref{OuterDoc.pdf}{}{ChildTwo}{a named destination} seems to work.

  • For the last small problem you mentioned, add pdfremotestartview=FitH \hypercalcbp{\paperheight-\topmargin-1in -\headheight-\headsep} key to \href. (Gets the correct place if all pages in ChildTwo have same paper size and margins as those containing the link, ie ChildOne.)

ChildOne.tex

\documentclass{article}

\usepackage[bookmarksopenlevel=1,%
     pdfpagemode=UseOutlines]{hyperref}
\hypersetup{
    pdfinfo={
        Title={Child One},
        Author={Me}
    }
}

\author{Me}
\title{Child One}

\begin{document}
\maketitle
Here is where I want to refer to Child Two.

I can do it by \href[page=3,pdfremotestartview=FitH \hypercalcbp{\paperheight-\topmargin-1in
-\headheight-\headsep}]{OuterDoc.pdf}{absolute page reference},
but the zoom level changes, and I don't always know the absolute
page number ahead of test (because there are so many child documents
of varying lengths).

My preference would be to refer to Child Two automatically, and
\hyperref{OuterDoc.pdf}{}{ChildTwo}{a named destination} doesn't
seem to work.


\end{document}
  • Hurrah -- it works perfectly! Why didn't I think of that solution? I've been struggling with this for days. Thank you so much. – Brian Dec 19 '12 at 1:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.