229

I'd like to create a LaTeX document that when rendered into PDF, has forms that can be filled out using Adobe Reader or other such programs. Then I'd like to be able to extract the data. I deliberately would like to avoid using Acrobat for all the usual reasons (non-free, need different versions for different platforms etc).

Can this be done ?

7
  • 2
    Creating a PDF form using pdflatex should be possible. However the "Then I'd like to be able to extract the data" part is then only a PDF issue. IIRC the PDF can be transmit the form to a web server. How exactly do you want to extract the data? Commented Apr 3, 2011 at 15:21
  • @Suresh Try this post tex.stackexchange.com/questions/7869/…, if this is what you are looking post a comment and I will add some explanations. Also search the JavaScript tag.
    – yannisl
    Commented Apr 3, 2011 at 15:23
  • @Yiannis: that's an interesting trick. Makes me think though that I should give up on the latex and just use HTML + javascript. @Martin, even extracting to a text file would be fine.
    – Suresh
    Commented Apr 3, 2011 at 15:27
  • @Suresh Best option would be in this case -- and I hate to say so to actually buy Acrobat Professional. It can do all these much easier transmit to the web collate the data etc...
    – yannisl
    Commented Apr 3, 2011 at 15:33
  • @Yiannis that just makes me sad :)
    – Suresh
    Commented Apr 3, 2011 at 15:49

6 Answers 6

143

The hyperref package provides a method to create PDF forms. The way I understand it, the form is either to be printed or to be transmitted to a webserver like a HTML form.

Here a small example:

\documentclass{article}

\usepackage{hyperref}

\begin{document}

\begin{Form}[action={http://your-web-server.com/path/receiveform.cgi}]
\begin{tabular}{l}
    \TextField{Name} \\\\
    \CheckBox[width=1em]{Check} \\\\
    \Submit{Submit}\\
\end{tabular}
\end{Form}


\end{document}

Gives:

Result

10
56

Here is some code that I use to create forms that can be either printed and filled out with a pen or filled out electronically in a PDF viewer. When printed, the form provides a line for each form field. Like Martin Scharrer's solution I'm using the hyperref package. The tricky bit was to define an input field of a given length (here textwidth minus 4 cm). It required to override the \LayoutTextField hook.

\documentclass[a4paper,11pt]{article}
\usepackage[latin1]{inputenc} 
\usepackage[pdftex]{hyperref}

\newdimen\longline
\longline=\textwidth\advance\longline-4cm

\def\LayoutTextField#1#2{#2} % override default in hyperref

\def\lbl#1{\hbox to 4cm{#1\dotfill\strut}}%
\def\labelline#1#2{\lbl{#1}\vbox{\hbox{\TextField[name=#1,width=#2]{\null}}\kern2pt\hrule}}

\def\q#1{\hbox to \hsize{\labelline{#1}{\longline}}\vskip1.4ex}

\begin{document}
  \begin{Form}
    \q{First Name}
    \q{Last Name}
    \q{Email}
   \end{Form}
 \end{document}

enter image description here

In a PDF viewer each line becomes a form field.

8
  • 3
    To turn off red boxes: \def\labelline#1#2{\lbl{#1}\vbox{\hbox{\TextField[name=#1,width=#2,borderwidth=0]{\null}}\kern0pt\hrule}}.
    – Adobe
    Commented Jul 8, 2013 at 17:27
  • This command from @Adobe comment does not work for me. It compiles only if i exclude borderwidth=0. Any other tips on "turnning of red boxes"?
    – DrBeco
    Commented Aug 25, 2015 at 3:47
  • 1
    @DrBeco : For some reason copypasting Adobe's line doesn't seem to work indeed. Just add ,borderwidth=‌​0 in Christian's original code. Commented Sep 18, 2015 at 9:17
  • @SkippyleGrandGourou Doing \def\labelline#1#2{\lbl{#1}\vbox{\hbox{\TextField[name=#1,width=#2,borderwidth=‌​0]{\null}}\kern2pt\hrule}} gives many warnings but almost completely removes the red surrounding boxes. What do you think? Commented Aug 15, 2017 at 20:50
  • 2
    @SkippyleGrandGourou Expanded here tex.stackexchange.com/q/386576/13173 for many lines on \q{}. Commented Aug 15, 2017 at 21:14
38

I've used the eforms package, which is used by DANTE for their membership form (which of course I can't now locate in .tex format!). You'll probably have to download and unpack eforms from CTAN yourself, as it's not in TeX Live (unpack eforms, insdljs and taborder, all in the eforms bundle). A short example from a registration form I've done:

\documentclass{article}
\usepackage[pdftex]{eforms}

% From DANTE's registration form!

\newcounter{infoLineNum}
\setcounter{infoLineNum}{0}
\newcommand{\infoInput}[2][4in]{%
  \stepcounter{infoLineNum}%
  \makebox[0pt][l]{%
    \kern 4 pt
    \raisebox{.75ex}
      {\textField[\W0\BC{}\BG{}\TU{#2}]{name\theinfoLineNum}{#1}{12bp}}%
  }
    \dotfill
}

\begin{document}
    \begin{tabular}{lp{4in}}
   Title                 & \infoInput{Title}\\[6pt]
   First name            & \infoInput{Firstname}\\[6pt]
   Last name             & \infoInput{Surname}\\[6pt]
   E-mail address        & \infoInput{Email}\\[6pt]
   Dietary requirements  & \infoInput{Dietary}\\[6pt]
\end{tabular}

\begin{tabular}{ll}
  Student (\pounds60) 
    & \raisebox{.75ex}{\radioButton{RegType}{10bp}{10bp}{Student}} 
      \\[6pt]
  Academic/post-doc (\pounds120)
    & \raisebox{.75ex}{\radioButton{RegType}{10bp}{10bp}{Academic}} 
      \\[6pt]
  Industrial (\pounds180)
    & \raisebox{.75ex}{\radioButton{RegType}{10bp}{10bp}{Industrial}} 
      \\[6pt]
\end{tabular}

\end{document}

This gives 'type in' boxes for the text areas and 'tick' boxes for the choices.

I'm not sure what happens about saving form data: according to Adobe Reader it can't be saved in this form. I've never actually seen a PDF form that can have the data saved, so whether even Acrobat can do this I do not know.

11
  • @Joseph Wright you need Acrobat professional to save the data, if I recall correctly, if you have it you can also enable the final copy of the pdf to be saved by those using the Reader alone.
    – yannisl
    Commented Apr 3, 2011 at 15:36
  • Or, if you use a Mac, Preview allows you to save the data.
    – Alan Munn
    Commented Apr 3, 2011 at 15:50
  • @Alan: I tried that, but it did not seem to work
    – Joseph Wright
    Commented Apr 3, 2011 at 15:50
  • @Joseph I haven't tried the eforms package itself, but I routinely generate a syllabus for a course I supervise using hyperref's pdf forms methods, and if you fill in the form and then Save As in Preview, it saves the filled in form.
    – Alan Munn
    Commented Apr 3, 2011 at 15:54
  • 1
    @Joseph: In case you're still interested in retrieving the membership form in .tex format, it is actually attached to the application form itself. Like a first goodie for new members :)
    – doncherry
    Commented Apr 30, 2011 at 11:41
7

action= submission dangers

https://tex.stackexchange.com/a/14844/19083 works, but the proposed method is:

  • dangerous:
    • user may submit form twice and not notice it
    • no authentication, so a malicious user could submit it multiple times
  • not portable: Evince 3.24.0 (default reader Ubuntu 17.04) doesn't implement HTTP submission (tested with nc localhost 8000).

I would recommend instead that you remove the action and submit button as in:

\documentclass{article}
\usepackage{hyperref}
\begin{document}
\begin{Form}
\begin{tabular}{l}
    \TextField{Name} \\
    \CheckBox[width=1em]{Check} \\
\end{tabular}
\end{Form}
\end{document}

and use a programmatic method to extract the PDF data as mentioned at:

and then ask users to upload the PDF if you are using a web server.

I have tested Python's pdfminer, and it just worked, outputting:

Name: asdf
Check: /Yes

Note that you have to save a new PDF first after modifying the fields with Evince.

6

I agree with Christian Lindig's answer. It looks good.

The problem is that there are red border around the fields. Guys in this community do gave some suggestions on how to remove that. To me, the simplest one is

\def\labelline#1#2{\lbl{#1}\vbox{\hbox{\TextField[name=#1,width=#2,bordercolor=white]{\null}}\kern2pt\hrule}} 

Just change the color of the border. Remember to include the xcolor package.

Maybe I should write this in the comments, but I am new here.

1
  • Confirm! This works best. borderwidth=0 like proposed before gives many warnings and is not complete. Commented Aug 15, 2017 at 21:04
0

We can search it at CTAN like this: https://www.ctan.org/topic/form-fillin

  • for­mu­lar (it will create fields to be filled by hand or computer. I did not like because it doesn't have checkbox) check the manual

  • hyperref - manual

The others results were not nice.

I will try hyperref later.

1
  • 2
    This is more commentary as you only mention some packages explaining the how and hyperref is already covered by another answer.
    – TeXnician
    Commented Feb 28, 2018 at 19:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .