[Tex/LaTex] Why are some of the links in the Contents page pointing to the wrong page

hyperreftable of contents

\documentclass[a4paper,12pt]{book}
\usepackage[hidelinks]{hyperref}

\begin{document}

\tableofcontents
\newpage

\addcontentsline{toc}{section}{from there}
no text
\newpage

\addcontentsline{toc}{section}{to here}
some text
\newpage

\addcontentsline{toc}{subsection}{and here}
still more text
\newpage

\addcontentsline{toc}{subsection}{not here}
or maybe less text
\newpage

\addcontentsline{toc}{section}{also here}
and more text
\newpage

\addcontentsline{toc}{subsection}{maybe here}
and still more text

\end{document}

Best Answer

\addcontentsline does not create an anchor, it uses the anchor, that is created before, even if the anchor belongs to one of the previous pages. This is by design, it allows, e.g.:

\section*{Introduction}
\addcontentsline{toc}{section}{Introduction}

If \addcontentsline would create an anchor, then it would point below the section title.

The solution here is to use \phantomsection, which creates the needed anchor:

\newpage
\phantomsection
\addcontentsline ...