KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FormProcess


1 import org.faceless.pdf2.*;
2 import java.io.*;
3 import java.util.*;
4
5 // The third and final stage in the "Form" series of examples - take a form
6
// which has been filled out and extract the results. A more sophisticated
7
// example would cycle through the values returned from form.getValues()
8
// and print them out depending on type.
9
//
10
// $Id: FormProcess.java,v 1.2 2003/11/03 10:56:14 mike Exp $
11
//
12
public class FormProcess
13 {
14     public static void main(String JavaDoc[] args)
15     throws IOException
16     {
17     // Load the existing PDF and extract the form
18
//
19
PDF pdf = new PDF(new PDFReader(new FileInputStream(args[0])));
20     Form form = pdf.getForm();
21
22     // Get the values from the form. Although the method is always
23
// called "getValue", different elements return different objects
24
// so we need to cast each one separately. We replace the '\n' with
25
// '\t' in the FormText one too, to make printing of multiline text
26
// objects easier.
27
//
28
System.out.println("PROCESSING FORM:");
29     Map m = form.getElements();
30     for (Iterator i = m.keySet().iterator();i.hasNext();) {
31         String JavaDoc key = (String JavaDoc)i.next();
32         FormElement elt = (FormElement)m.get(key);
33         String JavaDoc val=null;
34         if (elt instanceof FormText) val = ((FormText)elt).getValue();
35         else if (elt instanceof FormChoice) val = ((FormChoice)elt).getValue();
36         else if (elt instanceof FormRadioButton) val = ((FormRadioButton)elt).getValue();
37         else if (elt instanceof FormCheckbox) val = ((FormCheckbox)elt).getValue();
38         if (val!=null) {
39         val=val.replace('\n','\t'); // Make printing Address easier
40
System.out.println(key+"=\""+val+"\"");
41         }
42     }
43     }
44 }
45
Popular Tags