\documentclass{article}
% Demonstrates populating a PDF annotation with text input from an external file.
%
% Bugs/Limitations/anti-features:
% * (serious) Percent symbols in the input text are lost entirely, along with all text on to the right of a % on that line!
% * Line breaks are lost. (this is a feature for some situations; use a blank line in the source text to force a line break)
% * Every blank line in the source triggers an extra space in the PDF comment.
% * (w.i.p) Sanitization is limited to “_”, “&”, “%” (lossy), and blank lines. There are likely many more strings that need to be controlled.
% * Some file i/o is likely unnecessary. Intermediate text is staged in \jobname_filecycle.txt.
\usepackage[final]{pdfcomment}
\usepackage{newfile} % furnishes \newoutputstream et al.
\usepackage{catchfile} % furnishes \CatchFileDef
\usepackage{xstring} % furnishes \StrSubstitute
\begin{filecontents*}[overwrite]{\jobname_sample.txt}
line one
line two
% pre-tricky line (lost completely because of the leading percent symbol!)
tricky symbols: _&%
\end{filecontents*}
% 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
\newcommand{\safepdfcomment}[1]{
\ifpc@gopt@final\else%
\CatchFileDef{\unsafetext}{#1}{} % side-effects: replaces blank lines with “\par”, loses linebreaks, and drops percent symbols+all text to the right of %
\message{The pre-sanitized PDF annotation text is: \unsafetext}