Click to search Andy Jarrett.co.uk RSS feed

Loading Twitter

PDF Form filling with java and iText

I've been looking at PDF form filling with Java so I can use it with Railo and I came across iText, a Free Java-PDF Library.

I couldn't find a simple explanation of populating a PDF form in java so my below example assumes you have a PDF called "SimpleRegistrationForm.pdf" and it has 4 fields in it: name, address, postal_code, and email The completed form will be saved to a file called filledOutForm.pdf

view plain print about
1/**
2 *
3 */

4
5import java.io.FileOutputStream;
6import java.io.IOException;
7
8import com.lowagie.text.DocumentException;
9import com.lowagie.text.pdf.AcroFields;
10import com.lowagie.text.pdf.PdfReader;
11import com.lowagie.text.pdf.PdfStamper;
12
13/**
14 * @author andyjarrett
15 *
16 */

17public class SimpleRegistrationForm {
18
19    /**
20     * @param args
21     * @throws IOException
22     * @throws DocumentException
23     */

24    public static void main(String[] args) throws IOException, DocumentException {
25
26            PdfReader reader = new PdfReader(
27                    "/dir/to/pdf/SimpleRegistrationForm.pdf");
28            PdfStamper filledOutForm = new PdfStamper(reader, new FileOutputStream(
29                    "/dir/to/pdf/filledOutForm.pdf"));
30            
31            AcroFields form = filledOutForm.getAcroFields();
32            form.setField("name", "Andy Jarrett");
33            form.setField("address", "1 infinite road");
34            form.setField("postal_code", "1SW");
35            form.setField("email", "mail@example.com");
36
37            filledOutForm.close();
38    }
39
40}

Comments Comments (2) | Print Print | Send Send | 1971 Views

If you like what you see on the website and/or this post has helped you out in some way please consider donating to help keep me in beer vodka. The donations are made through Paypal, which accepts almost any credit card or eCheck.

(Comment Moderation is enabled. Your comment will not appear until approved.)
Yes, iText is great stuff. I have written some entries on using it from CF (standard). You might find some of it useful.

http://cfsearching.blogspot.com/search/label/iText...

- Leigh
Arsalan's Gravatar Posted By Arsalan @ 6/28/09 6:08 PM
I am an amateur web designer. I want to know how can I use iText for dynamically populating my PDF Forms using .NET or ColdFusion?
BlogCFC by Raymond Camden + Twitter @AndyJ + ColdFusion jobs + Contact Me + Snippets/Downloads + RSS .