Defining a width that fills remaining horizontal space for text fields in hyperref forms environment

The mandatory argument to \TextField is the text that precedes the box. It's sufficient to measure it (and cut out another

\documentclass{article}
\usepackage[pass,showframe]{geometry}
\usepackage{lipsum}
\usepackage{hyperref}

\newlength\TextFieldLength
\newcommand\TextFieldFill[2][]{%
  \setlength\TextFieldLength{\linewidth}%
  \settowidth{\dimen0}{#2 }%
  \addtolength\TextFieldLength{-\dimen0}%
  \addtolength\TextFieldLength{-2.22221pt}%
  \TextField[#1,width=\TextFieldLength]{\raisebox{2pt}{#2 }}%
}

\begin{document}
\lipsum[2]

\begin{Form}
\noindent\TextFieldFill[name=description]{\textbf{DESCRIPTION OF ACTIVITY}}

\noindent\TextFieldFill[name=procrastination]{\textbf{WAYS TO PROCRASTINATE}}

\end{Form}
\end{document}

The lipsum and geometry packages are just to provide text and the frame. The 2.22221pt excess has been determined from the Overfull \hbox message that one gets without it.

One can get rid of this adjustment by doing

\newcommand\TextFieldFill[2][]{%
  \setlength\TextFieldLength{\linewidth}%
  \settowidth{\dimen0}{#2 }%
  \addtolength\TextFieldLength{-\dimen0}%
  \TextField[#1,width=\TextFieldLength]{\raisebox{2pt}{#2}}%
}

because the macro that is called by \TextField adds one between the text and the box (thanks to Heiko Oberdiek for pointing out this).

enter image description here


It's easiest if you measure the earlier text then you can subtract its width from \linewidth

\documentclass[10pt,a4paper]{article}

\usepackage[showframe,headheight=2in,headsep=0.1in,left=0.8in,right=0.8in,bottom=0.5in]{geometry} 
\usepackage{xcolor} 
\usepackage{hyperref}
\newbox\formbox

\begin{document}
\begin{Form}
\noindent
\setbox\formbox\hbox{\textbf{DESCRIPTION OF ACTIVITY}}\usebox\formbox
\raisebox{-2pt}{\TextField[name=description, width=\dimexpr\linewidth-\wd\formbox\relax]{}}
\end{Form}

\end{document}

Tags:

Positioning