Home | Blog | Twitter @AndyJ | Contact Me | Snippets/Downloads | RSS

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 plainprintabout
 /**
  *
  */

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

17  public 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  }
TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Leigh's Gravatar 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
# Posted By Leigh | 5/12/09 9:05 AM
Arsalan's Gravatar 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?
# Posted By Arsalan | 6/28/09 6:08 PM
BlogCFC / created by Raymond Camden / running version 5.9.5.003 / Contact AndyJarrett.com / Pet Rescue SOS www.redgiraffes.co.uk