[Tex/LaTex] Typesetting fillable PDF forms with `hyperref`

formshyperref

I'm trying to create a fillable PDF form with hyperref and I want there to be a long line instead of a box for a text field. At the moment I'm using the following code:

\documentclass{scrartcl}

\usepackage{hyperref}
\usepackage{amsmath}

\renewcommand\LayoutTextField[2]{%
    #2
}

\begin{document}
    \begin{Form}
        Name: \dotfill$\overset{\TextField[width=4cm,borderwidth=0pt]{\null}}{\rule{4cm}{.4pt}}$
    \end{Form}
\end{document}

This works and achieves what I want but I was just wondering if there was a better way to do it? For some reason this solution seems inelegant to me.

Best Answer

You can set the borderstyle to underline. Or add some tikz and other code to the layout. In the second case set the colors to transparent. It is also often quite useful to move the field a bit down to get a better alignment with the label:

\documentclass[parskip=full-]{scrartcl}

\usepackage{hyperref}
\usepackage{tikz}
\usepackage{amsmath}


\begin{document}
\begin{Form}
\renewcommand\LayoutTextField[2]{%
 #2}

Name: \TextField[width=4cm,borderstyle=U]{nameA}{\rule{4cm}{.4pt}}

 \renewcommand\LayoutTextField[2]{%
   \raisebox{-3.2pt}{\tikz[overlay]\draw[dashed](0,-0.2\dp\strutbox)--++(\csname Fld@width\endcsname,0pt);#2}}

 Name: \TextField[width=4cm,bordercolor={},backgroundcolor={}]{nameB}{\rule{4cm}{.4pt}}


 \renewcommand\LayoutTextField[2]{%
  \raisebox{-\dp\strutbox}{\makebox[0pt][l]{\cleaders \hbox to .44em{\hss.\hss}\hskip\csname Fld@width\endcsname}}#2}

 Name: \TextField[width=4cm,bordercolor={},backgroundcolor={}]{nameC}{\rule{4cm}{.4pt}}
\end{Form}
\end{document}

enter image description here

Related Question