KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FormCreation


1 import org.faceless.pdf2.*;
2 import java.io.*;
3 import java.awt.Color JavaDoc;
4
5 // The first in a series of three examples demonstrating how to
6
// create, manipulate and parse PDF Forms or "AcroForms", which
7
// are a feature of the Extended Edition of the library since
8
// 1.1.23
9
//
10
// This example shows how to create a new form. This is filled
11
// out in the next example (FormFill.java) and processed in the
12
// last example (FormProcess.java)
13
//
14
// $Id: FormCreation.java,v 1.3 2003/11/04 14:46:02 mike Exp $
15
//
16
public class FormCreation
17 {
18     public static void main(String JavaDoc[] args)
19         throws IOException
20     {
21         PDF pdf = new PDF();
22     PDFPage page = pdf.newPage(PDF.PAGESIZE_A4);
23
24     // Create the page labels - nothing to do with forms,
25
// but included to make the document more complete.
26
//
27
PDFStyle label = new PDFStyle();
28     label.setFillColor(Color.black);
29     label.setFont(new StandardFont(StandardFont.HELVETICA), 11);
30     page.setStyle(label);
31     page.drawText("Name:", 100, 708);
32     page.drawText("Year of birth:", 100, 668);
33     page.drawText("Address:", 330, 708);
34     page.drawText("Rating:", 100, 628);
35     page.drawText("1", 155, 628);
36     page.drawText("5", 255, 628);
37     page.drawText("Yes, send me spam!:", 330, 628);
38
39
40     // Now we begin the forms specific bits - we get the form from the
41
// PDF, and set a default style for any new elements that we create.
42
// The background style can have a fill color, line color, line
43
// weighting, line dash pattern and form style set.
44
//
45
Form form = pdf.getForm();
46     PDFStyle background = new PDFStyle();
47     background.setFillColor(new Color JavaDoc(240,240,255));
48     background.setLineColor(Color.blue);
49     background.setFormStyle(PDFStyle.FORMSTYLE_BEVEL);
50     form.setBackgroundStyle(background);
51
52     // Create the first field - a text field called "name"
53
//
54
FormText name = new FormText(page, 170, 700, 300, 720);
55     form.addElement("Name", name);
56
57     // Create the second field - a multiline text box for the Address
58
//
59
FormText address = new FormText(page, 400, 660, 550, 720);
60     address.setType(FormText.TYPE_MULTILINE);
61     form.addElement("Address", address);
62
63     // Create the third field - a pull-down menu of months
64
//
65
FormChoice month = new FormChoice( FormChoice.TYPE_SCROLLABLE, page , 170, 660, 230, 680);
66     month.getOptions().put("Jan",null);
67     month.getOptions().put("Feb",null);
68     month.getOptions().put("Mar",null);
69     month.getOptions().put("Apr",null);
70     month.getOptions().put("May",null);
71     month.getOptions().put("Jun",null);
72     month.getOptions().put("Jul",null);
73     month.getOptions().put("Aug",null);
74     month.getOptions().put("Sep",null);
75     month.getOptions().put("Oct",null);
76     month.getOptions().put("Nov",null);
77     month.getOptions().put("Dec",null);
78     form.addElement("Month", month);
79
80     // Create the fourth field - a text field containing a year.
81
// Add some JavaScript constraints which limit this field to
82
// numeric values between 1900 and 2000
83
//
84
FormText year = new FormText(page, 240, 660, 300, 680);
85     year.setMaxLength(4);
86     WidgetAnnotation annot = year.getAnnotation(0);
87     year.setAction(Event.KEYPRESS, PDFAction.formJavaScript("AFNumber_Keystroke(0,1,1,0,'',true);"));
88     year.setAction(Event.CHANGE, PDFAction.formJavaScript("AFRange_Validate(true,1900,true,2000);"));
89     form.addElement("Year", year);
90
91     // Create a set of five radio buttons, the fifth field.
92
//
93
FormRadioButton rating = new FormRadioButton();
94
95     rating.addAnnotation("1", page, 170,627,180,637);
96     rating.addAnnotation("2", page, 185,627,195,637);
97     rating.addAnnotation("3", page, 200,627,210,637);
98     rating.addAnnotation("4", page, 215,627,225,637);
99     rating.addAnnotation("5", page, 230,627,245,637);
100     form.addElement("Rating", rating);
101
102     // Create the sixth field, a single checkbox.
103
//
104
FormCheckbox spam = new FormCheckbox(page, 450,627,460,637);
105     form.addElement("Spam", spam);
106
107     // Create the seventh and eighth fields - two buttons to reset
108
// and submit the form. The Submit option won't work unless the
109
// document is being viewed in a web browser, but it demonstrates
110
// how you'd go about it.
111
//
112
FormButton reset = new FormButton(page, 200, 580, 300, 600);
113     WidgetAnnotation widget = reset.getAnnotation(0);
114         widget.setValue("Reset Form");
115         widget.setAction(Event.CLICK,PDFAction.formReset());
116     form.addElement("Reset", reset);
117
118     FormButton submit = new FormButton(page, 320, 580, 420, 600);
119     widget = submit.getAnnotation(0);
120         widget.setValue("Submit Form");
121     widget.setAction(Event.CLICK, PDFAction.formSubmit("http://localhost", PDFAction.METHOD_HTTP_POST));
122     form.addElement("Submit", submit);
123
124     pdf.render(new FileOutputStream("FormCreation.pdf"));
125     }
126 }
127
Popular Tags