As in this question,
Assume that you have some text in parts of your LaTeX document that you only want people with a password to see. A reason could e.g. be a case with an guiding answer. You want to make the case public but the answer only to teachers.
I would like to have a link action in the PDF that, when clicked, will decrypt the content after asking for a password, using the Adobe Acrobat Javascript API. (Motivated by this question and answer: How to add print link action of specific pages?)
You can choose any source of encrypted material as a test ASCII content, but a minimum example is in the code below. In the answers to the question Encrypt and decrypt parts of text in a LaTeX source file, it is indicated that the problem of encrypting parts of an ASCII file is really an easy piece of the main task, which can be done by a good editor such as Emacs.
In the code below, which is based on this answer, the line \password"this.getField("myinput").value; tries to cast the user input to integer. This does not work. How can I make it work?
\documentclass{article}
\usepackage{fontspec}
% https://tex.stackexchange.com/a/275434/13173
\newcount\password
\newcount\crycount
\newcount\crycounta
\newcount\crycountb
\def\private#1{%
\ifx#1\end
\expandafter\end
\else
\crycount`#1
% ↓↓↓ some math involving \crycount and \password
\advance\crycount-\password
\crycounta\crycount\crycountb\crycount\divide\crycounta16\multiply\crycounta16\advance\crycountb-\crycounta\divide\crycounta16
\xdef\crychara{\ifcase\crycounta0\or1\or2\or3\or4\or5\or6\or7\or8\or9\or a\or b\or c\or d\or e\else f\fi}
\xdef\crycharb{\ifcase\crycountb0\or1\or2\or3\or4\or5\or6\or7\or8\or9\or a\or b\or c\or d\or e\else f\fi}
% ↑↑↑ some math involving \crycount and \password
\xdef\crystring{\crystring\string^\string^\crychara\crycharb}
\expandafter\private
\fi
}
% https://tex.stackexchange.com/a/387474/13173
\usepackage{xparse,calc}
\usepackage{eforms
\begin{document}
\medskip
\textField[
\Ff\FfMultiline
\Ff\FfDoNotSpellCheck
]
{myinput}{\dimexpr\linewidth-10ex}{5\baselineskip}
If key is set to be 2600, you can see the hint below:
\password"2600
\def\crystring{}
\begin{private}
♕♳♥☠♭♡♴♨♥♭♡♴♩♣♡♬☠♩♮♤♵♣♴♩♯♮☠♯♮☠☤♜♬♡♭♢♤♡☤☮
\end{private}
\newwrite\cryfile
\immediate\openout\cryfile=\jobname.cry
\immediate\write\cryfile{\crystring}
\immediate\closeout\cryfile
\input{\jobname.cry}
If key is not 2600, you see random characters:
\password"0
\def\crystring{}
\begin{private}
♕♳♥☠♭♡♴♨♥♭♡♴♩♣♡♬☠♩♮♤♵♣♴♩♯♮☠♯♮☠☤♜♬♡♭♢♤♡☤☮
\end{private}
\newwrite\cryfile
\immediate\openout\cryfile=\jobname.cry
\immediate\write\cryfile{\crystring}
\immediate\closeout\cryfile
\input{\jobname.cry}
% TODO how to pass the value as integer there?
\AACalculate{event.value=getField("myinput").value;}
If user input is set to be 7777, you can see the hint below:
\password"this.getField("myinput").value;
\def\crystring{}
\begin{private}
♕♳♥☠♭♡♴♨♥♭♡♴♩♣♡♬☠♩♮♤♵♣♴♩♯♮☠♯♮☠☤♜♬♡♭♢♤♡☤☮
\end{private}
\newwrite\cryfile
\immediate\openout\cryfile=\jobname.cry
\immediate\write\cryfile{\crystring}
\immediate\closeout\cryfile
\input{\jobname.cry}
\end{document}
Fig. 1 Output
TeXLive: 2017
OS: Debian 9.1
PDF viewer: Adobe Acrobat 9.x (acroread) installed as described here
Acrotex installation: here
