Incompatibility of the ocg-p package with the ocgcolorlinks option of the hyperref package

Both packages, hyperref and ocg-p, try to create optional content groups (OCGs) without knowing from one another. This results in incorrect PDF files (to be more specific, duplicate /OCProperties and /Properties entries), which prevent the OCGs from working as expected.

It is not particularly easy to make the two packages cooperate, as the implementations are completely independent. The following code should work for the current versions hyperref v6.83m and ocg-p v0.4:

\documentclass{article}
\usepackage{ocg-p}
\usepackage[ocgcolorlinks]{hyperref}

% Fix incompability between ocg-p and the hyperref option ocgcolorlinks
% (http://tex.stackexchange.com/a/104227)
\makeatletter
\Hy@colorlinkstrue
\Hy@ocgcolorlinksfalse
\newcommand*{\reenable@ocglinks@pdftex}{%
  \Hy@AtBeginDocument{%
    \def\Hy@colorlink##1{%
      \begingroup
        \def\Hy@ocgcolor{##1}%
        \setbox0=\hbox\bgroup\color@begingroup
    }%
    \def\Hy@endcolorlink{%
      \color@endgroup\egroup
      \mbox{%
        \pdfliteral page{/OC/OCPrint BDC}%
        \rlap{\copy0}%
        \pdfliteral page{EMC/OC/OCView BDC}%
        \begingroup
          \expandafter\HyColor@UseColor\Hy@ocgcolor
          \box0 %
        \endgroup
        \pdfliteral page{EMC}%
      }%
      \endgroup
    }%
  }%
}
\newcommand*{\reenable@ocglinks@dvipdfm}{%
  \Hy@AtBeginDocument{%
    \def\Hy@colorlink##1{%
      \begingroup
        \def\Hy@ocgcolor{##1}%
        \setbox0=\hbox\bgroup\color@begingroup
    }%
    \def\Hy@endcolorlink{%
      \color@endgroup\egroup
      \mbox{%
        \@pdfm@mark{content /OC/OCPrint BDC}%
        \rlap{\copy0}%
        \@pdfm@mark{content EMC/OC/OCView BDC}%
        \begingroup
          \expandafter\HyColor@UseColor\Hy@ocgcolor
          \box0 %
        \endgroup
        \@pdfm@mark{content EMC}%
      }%
      \endgroup
    }%
  }%
}
\def\Hy@temp{hpdftex}
\ifx\Hy@driver\Hy@temp
  \reenable@ocglinks@pdftex
\else
  \def\Hy@temp{hdvipdfm}
  \ifx\Hy@driver\Hy@temp
    \reenable@ocglinks@dvipdfm
  \else
    \def\Hy@temp{hxetex}
    \ifx\Hy@driver\Hy@temp
      \reenable@ocglinks@dvipdfm
    \fi
  \fi
\fi
\@ocgp@newocg{View}{View}{1}{printocg=never,listintoolbar=never}
\@ocgp@newocg{Print}{Print}{0}{printocg=always,listintoolbar=never}
\makeatother

\begin{document}
    \section{Test}
    See \autoref{next}
    \section{Next}
    \label{next}
    Target.
\end{document}

Explanation of the code

We deliberately disable the ocgcolorlinks feature to stop the interference with the OCG management provided by the ocg-p package:

\Hy@colorlinkstrue
\Hy@ocgcolorlinksfalse

Now we re-enable the necessary OCG markup of the hyperlinks by hand. For the three drivers currently supporting ocgcolorlinks (pdftex, dvipdfm and xetex), different code is necessary (cf. hpdftex.def, ll. 292-318, hdvipdfm.def, ll. 280-306, and hxetex.def, ll. 362-388):

\newcommand*{\reenable@ocglinks@pdftex}{%
  % see above
}
\newcommand*{\reenable@ocglinks@dvipdfm}{%
  % see above
}
\def\Hy@temp{hpdftex}
\ifx\Hy@driver\Hy@temp
  \reenable@ocglinks@pdftex
\else
  \def\Hy@temp{hdvipdfm}
  \ifx\Hy@driver\Hy@temp
    \reenable@ocglinks@dvipdfm
  \else
    \def\Hy@temp{hxetex}
    \ifx\Hy@driver\Hy@temp
      \reenable@ocglinks@dvipdfm
    \fi
  \fi
\fi

At last, we create the optional content groups View and Print with the necessary visibility options using ocg-p:

\@ocgp@newocg{View}{View}{1}{printocg=never,listintoolbar=never}
\@ocgp@newocg{Print}{Print}{0}{printocg=always,listintoolbar=never}

Like this, the OCGs are introduced correctly to the PDF viewer, avoiding misguiding duplicate definitions.