# Escape special characters in text field of pdf form

I am using hyperref to render a form. I would like to initially fill the text fields with some dynamic content. Therefore, I have to escape special characters in the content. I managed to discover how I can escape &, %, $, #, _, and ^ by trial and error but how can I escape \, {, }, and ~? \documentclass{article} \usepackage{hyperref} \begin{document} \TextField[value={&\%$\#_^}]{}

% \TextField[value={\{}~}]{}

\end{document}

• For the ~, \TextField[value={\~{}}]{} seems to work, as well as \textasciitilde. The Comprehensive LaTeX symbol list, p.10, recommends to use \textbackslash, \textbraceleft, \{ and \} for the other symbols you are looking for, but that does not seem to work. – Clément Jun 26 '14 at 15:15

I found a solution using the macro \pdfstringdef.

\documentclass{article}

\usepackage{hyperref}

\begin{document}

\begin{Form}

\TextField[value={&\%\$\#_^}]{}
\pdfstringdef{\foo}{\textbackslash\{\}\~{}}
\TextField[value={\foo}]{}

\end{Form}

\end{document}