[Tex/LaTex] Defining a width that fills remaining horizontal space for text fields in hyperref forms environment

positioning

This code

\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}

\begin{document}
\begin{Form}
\noindent\textbf{DESCRIPTION OF ACTIVITY}\raisebox{-2pt}{\TextField[name=description,width=4.3in]{~}}
\end{Form}

\end{document}

gives the output

enter image description here

Notice that the text field box did not end at the right margin. This results to ugly alignment of the boxes after typing several lines with text fields. I could have used \hfill before the text field but that will leave ugly white space between DESCRIPTION… and the text field.

So, to my question: How can I define a width, remwidth, such that I can write

\noindent\textbf{DESCRIPTION OF ACTIVITY}\raisebox{-2pt}{\TextField[name=description,width=remwidth]{}}

so that the text field box stops exactly at the right margin rule?

I would appreciate it very much if the the answer would include an explanation of what is happening at every step.

Edit: October 15, 2012 I edited the question since the problem is really about the \TextField command and the hyperref package putting a padding in it as noted in the comments. The other part of my question on defining a width command that I can use elsewhere is actually solved by \linewidth, which I just learned last night even after 3 years of using LaTeX. The question Difference between \textwidth, \linewidth and \hsize clarified its use for me.

Best Answer

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

\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

Related Question