A package such as zref-totpages
that counts the absolute number of pages helps.
The critical part is the timing:
\documentclass{book}
\usepackage{zref-totpages}
\AtBeginDocument{%
\ifnum\ztotpages>0 %
\hypersetup{pdfstartpage=\ztotpages}%
\fi
}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\chapter{Hello World}
\chapter{Last Chapter}
\end{document}
Update: In the first run, \ztotpages
is not yet known and has value 0
causing an error with pdfTeX: page number must be positive
. (hyperref
6.83o will add some validation for the value of pdfstartpage
.)
Timing:
- Loading of package
\zref-totpages
.
- Explicit call of
\AtBeginDocument
for setting pdfstartpage
.
- Loading of package
hyperref
that
- calls
\AtBeginDocument
for using pdfstartpage
.
\begin{document}
reads the .aux
file.
⇒ \zreftotpages
is valid.
- The hook
\AtBeginDocument
is executed, first with \hypersetup
to set pdfstartpage
.
- At last
hyperref
uses pdfstartpage
to set the starting page.
The last page is known at the end of the document, hyperref
uses pdfstartpage
earlier (TeX does not execute \special
after the last DVI output page), thus two LaTeX runs are needed.
Remark: Package lastpage
does not help here, because the page number of the last
page can be different from the number of absolute pages, especially if there are
resettings of the page counter (\frontmatter
, \mainmatter
, ...).
Named action LastPage
In a comment to the question Stephan Lehmke noted a named action LastPage
. Package hyperref
does not directly support this for the open action. But hyperref
's open action specification can be disabled by an empty start page and the named action can be added manually, e.g. for pdfTeX:
\documentclass{book}
\usepackage{ifpdf}
\usepackage{hyperref}
\hypersetup{pdfstartpage={}}% disable openaction of hyperref
\ifpdf
\pdfcatalog{}openaction user{<</S/Named/N/LastPage>>}{}\relax
\fi
\begin{document}
\tableofcontents
\chapter{Hello World}
\chapter{Last Chapter}
\end{document}
LastPage
, so with\Acrobatmenu{LastPage}{go to end}
you get a "link" to the last page, but I don't know whether it's possible to trigger that when opening the PDF...