227

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 ?

Martin Scharrer
  • 262,582
Suresh
  • 16,511
  • 14
  • 55
  • 64
  • 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? – Martin Scharrer Apr 03 '11 at 15:21
  • @Suresh Try this post http://tex.stackexchange.com/questions/7869/how-do-you-say-happy-new-year-with-latex, if this is what you are looking post a comment and I will add some explanations. Also search the JavaScript tag. – yannisl Apr 03 '11 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 Apr 03 '11 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 Apr 03 '11 at 15:33
  • @Yiannis that just makes me sad :) – Suresh Apr 03 '11 at 15:49
  • It would help some one can help for this http://tex.stackexchange.com/q/28830/5149 – Prabhanjan Naib Sep 18 '11 at 18:47
  • @Brendan It's not the easiest thing to transfer from one type of program to the other. Also, I highly doubt that HTML websites are printable. – Someone Apr 16 '20 at 15:31

6 Answers6

142

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

Faheem Mitha
  • 7,778
Martin Scharrer
  • 262,582
  • 1
    while both answers are great, this one is closer to what I need because it allows extraction of the data via a CGI script. – Suresh Apr 03 '11 at 15:48
  • @Suresh: Give it a few hours (to see if there is another solution), then accept this one if it's still the best approach for you. – Joseph Wright Apr 03 '11 at 15:51
  • yes, I was planning to wait a bit. I just wanted to clarify the things that were more important (extraction being one) – Suresh Apr 03 '11 at 15:54
  • 3
    I've just created an example: http://martin-thoma.com/creating-pdf-forms-with-latex/ – Martin Thoma Mar 03 '12 at 06:35
  • Does this work for local files? I.E. can I set the action of this to a local $.cgi$ file? – Goodies Mar 27 '15 at 17:56
  • 2
    See here for available fields and optional parameters (e.g. borderwidth=0 to remove red boxes). – Skippy le Grand Gourou Sep 18 '15 at 09:11
  • 1
    We can found a tutorial (english and german) at https://www.ctan.org/tex-archive/info/pdf-forms-tutorial – Eduardo Santana Jul 10 '16 at 10:34
  • Is there an easy way to make a field where users can fill out a date? i.e., I want something with 3 fillable entries that looks like //____ – nukeguy Feb 15 '17 at 15:36
  • This works, but I would recommend a file upload instead currently: https://tex.stackexchange.com/a/366238/19083 – Ciro Santilli OurBigBook.com Apr 23 '17 at 14:18
  • Does this work for printing? AFAICT the forms end up as part of the annotation layer, not the content layer. I can only seem to print content, rather than annotations. I'd really like a solution that works for both print and digital. – alphabetasoup Oct 24 '22 at 07:52
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.

  • 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 Jul 08 '13 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 Aug 25 '15 at 03: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. – Skippy le Grand Gourou Sep 18 '15 at 09: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? – Léo Léopold Hertz 준영 Aug 15 '17 at 20:50
  • @LéoLéopoldHertz준영 Well, I think this looks exactly like what I was suggesting… :P The difference with Adobe's comment being the value after \kern. Your last comment probably deserves a question of its own. – Skippy le Grand Gourou Aug 15 '17 at 21:10
  • 2
    @SkippyleGrandGourou Expanded here https://tex.stackexchange.com/q/386576/13173 for many lines on \q{}. – Léo Léopold Hertz 준영 Aug 15 '17 at 21:14
  • Note on this answer, name cannot include a period https://tex.stackexchange.com/questions/641508/begin-end-form-makes-textfield-inputs-unfillable – user202729 Apr 21 '22 at 09:37
  • Why not \TextField[bordercolor=0 0 0,borderstyle=U,width=.9\textwidth]{Name} ? to remove the box, change the color and expand the field to 90% of textwidth ? – Malik Koné Dec 16 '23 at 21:44
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.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • @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 Apr 03 '11 at 15:36
  • Or, if you use a Mac, Preview allows you to save the data. – Alan Munn Apr 03 '11 at 15:50
  • @Alan: I tried that, but it did not seem to work – Joseph Wright Apr 03 '11 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 Apr 03 '11 at 15:54
  • It is possible to save the data with Reader for some forms. An example is the US Tax form at http://www.irs.gov/pub/irs-pdf/f1040.pdf – Boris Bukh Apr 03 '11 at 16:36
  • @Boris: I guess they've made the form with Acrobat Pro. That can enable features in Reader with suitably-prepared PDF files. – Joseph Wright Apr 03 '11 at 20:30
  • I've found that PDF-Xchange Viewer (it's free) on Windows and the evince Document Viewer on Linux can save the data with PDF forms. – imnothere Apr 29 '11 at 01:26
  • 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 Apr 30 '11 at 11:41
  • Okular can also now save the filled out form with the data in the PDF itself, provided it is compiled with a recent version of poppler (which is probably what evince uses, as well). – cfr Dec 24 '13 at 22:14
  • Output: ! LaTeX Error: File 'eforms.sty' not found. in TeXLive 2017. How do you get the package there? – Léo Léopold Hertz 준영 Aug 15 '17 at 20:58
  • @LéoLéopoldHertz준영you download from CTAN and compile the files that are needed from the ins Format, e.g.: pdflatex eforms.ins for the package and pdflatex eforms.dtx for the documentation. – vv01f Oct 11 '20 at 00:32
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.

Y. Zhai
  • 163
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.

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