KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FormFill


1 import org.faceless.pdf2.*;
2 import java.io.*;
3 import java.awt.Color JavaDoc;
4
5 // The second stage in the "Form" series of examples - take a pre-existing
6
// form (created by "FormCreation.java") and fill the fields in. This is
7
// extremely easy, as you can see below. The resulting form is written to
8
// "FormFill.pdf", which is then processed by "FormProcess.java".
9
//
10
// To make this example a little more interesting, we've also changed the
11
// style of one of the existing buttons.
12
//
13
// $Id: FormFill.java,v 1.2 2003/11/03 10:56:14 mike Exp $
14
//
15
public class FormFill
16 {
17     public static void main(String JavaDoc[] args)
18         throws IOException
19     {
20     // Load the existing PDF and extract the form
21
//
22
PDF pdf = new PDF(new PDFReader(new FileInputStream("FormCreation.pdf")));
23     Form form = pdf.getForm();
24
25     // Set the values of the form
26
//
27
((FormText)form.getElement("Name")).setValue("John Tester");
28     ((FormText)form.getElement("Address")).setValue("12 Test St\nTestville\nTestLand");
29     ((FormText)form.getElement("Year")).setValue("1960");
30     ((FormChoice)form.getElement("Month")).setValue("May");
31     ((FormRadioButton)form.getElement("Rating")).setValue("2");
32     ((FormCheckbox)form.getElement("Spam")).setValue("Yes");
33
34     // Set the style of the "Submit" button. You wouldn't normally
35
// do this if you were filling out a form obviously, but the
36
// capability is there to completely restyle the form.
37
//
38
PDFStyle bg = new PDFStyle();
39     PDFStyle fg = new PDFStyle();
40     bg.setFillColor(Color.yellow);
41     bg.setLineColor(Color.red);
42     bg.setFormStyle(PDFStyle.FORMSTYLE_SOLID);
43     fg.setFont(new StandardFont(StandardFont.TIMES), 12);
44     fg.setFillColor(Color.black);
45         WidgetAnnotation widget = form.getElement("Submit").getAnnotation(0);
46     widget.setTextStyle(fg);
47         widget.setBackgroundStyle(bg);
48
49         // Save the completed form
50
//
51
pdf.render(new FileOutputStream("FormFill.pdf"));
52     }
53 }
54
Popular Tags