Code: Select all
\makeatletter
\gdef\pdfcommentfile#1{%
\begingroup
\everyeof{\noexpand}%
\long\edef\temp{\noexpand\pdfcomment{\StrSubstitute{\@@input{#1}}{\noexpand\noexpand\noexpand\newline}{\textLF}}}%
\temp
\endgroup
}%
\makeatother
Any ideas how to overcome this?ERROR: Illegal parameter number in definition of \temp.
Note as well that there are other characters that \pdfcomment chokes on, like “&” and “%”, so this code needs to accommodate multiple replacements. In fact, this must be a common problem whenever importing external text. Is there a package that generally sanitizes input text for LaTeX?
Here is some sample broken code:
Code: Select all
\documentclass{article}
\usepackage{pdfcomment}
\usepackage{newfile}
% (heredoc) generate a text file that has a blank line, to feed back in as sample input:
\newoutputstream{sampleinput}
\openoutputfile{\jobname_sample.txt}{sampleinput}
\addtostream{sampleinput}{line 1}
\addtostream{sampleinput}{} % blank line
\addtostream{sampleinput}{line 2}
\closeoutputstream{sampleinput}
% Furnish a \pdfcommentfile command which takes a filename as input and feeds the contents of the file to \pdfcomment:
\makeatletter
\gdef\pdfcommentfile#1{%
\begingroup
\everyeof{\noexpand}%
\long\edef\temp{\noexpand\pdfcomment{\@@input{#1}}}%
\temp
\endgroup
}%
\makeatother
\begin{document}
\pdfcommentfile{\jobname_sample.txt}
\end{document}
Code: Select all
\addtostream{sampleinput}{} % blank line
(update) This shows a working implementation but it’s ugly as fuck because text must be written to a temporary file then read back in from the file:
Code: Select all
\documentclass{article}
\usepackage{mwe}
\usepackage{pdfpages}
\usepackage{pdfcomment}
\usepackage{newfile}
\usepackage{xstring}
\usepackage{catchfile}
% heredoc holding text that normally breaks the \pdfcomment command:
\begin{filecontents*}{\jobname_sample.txt}
line one
line two
tricky symbols: _&%
\end{filecontents*}
% Create \pdfcommentfile, which is a version of \pdfcomment that can read from a file:
\makeatletter
\gdef\pdfcommentfile#1{%
\begingroup
\everyeof{\noexpand}%
\long\edef\temp{\noexpand\pdfcomment{\@@input{#1}}}%
\temp
\endgroup
}%
\makeatother
\CatchFileDef{\cfile}{\jobname_sample.txt}{} % side-effects: replaces blank lines with “\par” and drops percent symbols
% Replace blank lines with \textLF and replace special symbols with those that are safe for \pdfcomment. Warning: this is probably not a complete list of all constructs that break \pdfcomment!
\StrSubstitute{\cfile}{\par}{\string\noexpand\string\textLF\ }[\pdfannotationtxt]
\StrSubstitute{\pdfannotationtxt}{\%}{\string\noexpand\string\%}[\pdfannotationtxt]
\StrSubstitute{\pdfannotationtxt}{_}{\string\noexpand\string\_}[\pdfannotationtxt]
\StrSubstitute{\pdfannotationtxt}{&}{\string\noexpand\string\&}[\pdfannotationtxt]
% the \pdfcomment command cannot directly handle the above substitutions (nor can it handle the original unsubstituted version). So we write the new version to another file:
\newoutputstream{filteredresult}
\openoutputfile{\jobname_filtered.txt}{filteredresult}
\addtostream{filteredresult}{\pdfannotationtxt}
\closeoutputstream{filteredresult}
\begin{document}
\pdfcommentfile{\jobname_filtered.txt}
\includepdf{example-image-a.pdf}
\end{document}