Hyperref Forms – How to Calculate TextField Width in LaTeX

formshyperreftextfield

I'm trying to create a Form with hyperref. This is what I've got so far. (I'll remove the boarder as well. But to see what I'm doing it is still there.)

enter image description here

I have a view issues with my current solution:

  1. Is there some way, to calculate the width for the text fields automatically, so they will fill up the remaining space in the cell.
  2. Why is there this small indentation at the start of the row, when I use multicolumn?

My current Idea is this:

  • Add another parameter to DefaultTextField which is the current cell width
  • Calculate the space needed for the label (Unfortunately, I have no Idea how to do this)
  • Set the TextField width parameter to width=#<paramnr>-/reservedspace
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{hyperref}

\usepackage{multirow}
\usepackage{tabularx}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}

\renewcommand\LayoutTextField[2]{%
    #1 \raisebox{-3.0pt}{#2}%
}
\newcommand{\DefaultTextField}[2][]{%
    \mbox{\TextField[align=1,bordercolor=0 1 1,backgroundcolor={},#1]{#2}}%
}

\renewcommand\LayoutCheckField[2]{%
    \raisebox{-1.0pt}{#2}\hspace*{2mm}#1%
}
\newcommand{\DefaultCheckBox}[2][]{%
    \mbox{\CheckBox[bordercolor=0 0 0,backgroundcolor={},borderwidth=0.4mm,height=3mm,width=3mm,#1]{#2}}%
}

\begin{document}
    \begin{Form}
        \renewcommand{\arraystretch}{1.5}%
        \begin{tabular}{@{}L{0.5\textwidth}L{0.5\textwidth}@{}}
            \DefaultTextField[name=firstname,width=0.25\textwidth]{Firstname} & 
            \DefaultTextField[name=lastname,width=0.25\textwidth]{Lastname}\\ \hline
            
            \DefaultTextField[name=address1,width=0.25\textwidth]{Address} &
            \DefaultTextField[name=address2,,width=0.25\textwidth]{ZIP, City}\\ \hline
            
            \multicolumn{2}{l}{\DefaultTextField[name=mail,width=0.25\textwidth]{E-Mail}}\\ \hline
            
            \multicolumn{2}{l}{\DefaultTextField[name=phone,width=0.25\textwidth]{Phone/Mobile}}\\ \hline
            
            Foo\hfil\DefaultCheckBox[name=bar]{Bar}\hfil & \DefaultTextField[name=something,width=0.25\textwidth]{other} \\ \cline{2-2}
            
            \multicolumn{2}{l}{\DefaultTextField[name=number,width=0.25\textwidth]{Something}}\\ \hline
        \end{tabular}
    \end{Form}
\end{document}

Best Answer

You can easily get the width of the labels and then calculate the length:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{hyperref}

\usepackage{multirow}
\usepackage{tabularx}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}

\renewcommand\LayoutTextField[2]{%
    #1 \raisebox{-3.0pt}{#2}%
}
\newlength\templength
\newcommand{\DefaultTextField}[2][]{%
    \settowidth\templength{#2 }%
    \mbox{\TextField[align=1,bordercolor=0 1 1,backgroundcolor={},#1,width=\dimexpr\linewidth-\templength]{#2}}%
}

\renewcommand\LayoutCheckField[2]{%
    \raisebox{-1.0pt}{#2}\hspace*{2mm}#1%
}
\newcommand{\DefaultCheckBox}[2][]{%
    \mbox{\CheckBox[bordercolor=0 0 0,backgroundcolor={},borderwidth=0.4mm,height=3mm,width=3mm,#1]{#2}}%
}

\begin{document}
    \begin{Form}
        \renewcommand{\arraystretch}{1.5}%
        \noindent
        \begin{tabular}{@{}L{\dimexpr0.5\textwidth-\tabcolsep}L{\dimexpr0.5\textwidth-\tabcolsep}@{}}
            \DefaultTextField[name=firstname]{Firstname} &
            \DefaultTextField[name=lastname]{Lastname}\\ \hline

            \DefaultTextField[name=address1]{Address} &
            \DefaultTextField[name=address2]{ZIP, City}\\ \hline

            \multicolumn{2}{@{}p{\textwidth}@{}}{\DefaultTextField[name=mail,width=0.25\textwidth]{E-Mail}}\\ \hline

            \multicolumn{2}{@{}p{\textwidth}@{}}{\DefaultTextField[name=phone,width=0.25\textwidth]{Phone/Mobile}}\\ \hline

            Foo\hfil\DefaultCheckBox[name=bar]{Bar}\hfil & \DefaultTextField[name=something,width=0.25\textwidth]{other} \\ \cline{2-2}

            \multicolumn{2}{@{}p{\textwidth}@{}}{\DefaultTextField[name=number,width=0.25\textwidth]{Something}}\\ \hline
        \end{tabular}
    \end{Form}
\end{document}

enter image description here

Related Question