KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FormVoodoo


1 import org.faceless.pdf2.*;
2 import java.io.*;
3 import java.awt.Color JavaDoc;
4
5 // This example shows some of the stranger and more interesting
6
// things you can do with forms.
7
//
8
// $Id: FormVoodoo.java,v 1.2 2003/11/03 10:56:14 mike Exp $
9
//
10
public class FormVoodoo
11 {
12     public static void main(String JavaDoc[] args)
13         throws IOException
14     {
15         PDF pdf = new PDF();
16     PDFPage page = pdf.newPage(PDF.PAGESIZE_A4);
17
18     Form form = pdf.getForm();
19     PDFStyle background = new PDFStyle();
20     background.setFillColor(new Color JavaDoc(240,240,255));
21     background.setLineColor(Color.blue);
22     background.setFormStyle(PDFStyle.FORMSTYLE_BEVEL);
23     form.setBackgroundStyle(background);
24
25     // First example - a popup help field.
26
//
27
// Create the popup field and make it invisible
28
//
29
FormText popup = new FormText(page, 170, 650, 350, 790);
30         popup.setType(FormText.TYPE_MULTILINE);
31     popup.setReadOnly(true);
32     popup.setValue("This example shows some of the more unusual things you can do with forms.\n\nThis particular example shows how you could do a popup help box.\n\nThe example below shows using event handlers to do simple calculations on form fields.");
33     PDFAnnotation popupannot = popup.getAnnotation(0);
34     popupannot.setVisible(false);
35     popupannot.setPrintable(false);
36     form.addElement("Popup", popup);
37
38     // Create a button which has MouseOver and MouseOut events
39
// to show the popup help.
40
//
41
FormButton help = new FormButton(page, 100, 700, 150, 720);
42         help.getAnnotation(0).setValue("Help!");
43     WidgetAnnotation helpannot = help.getAnnotation(0);
44         helpannot.setAction(Event.MOUSEOVER,PDFAction.showWidget(popup.getAnnotation(0)));
45         helpannot.setAction(Event.MOUSEOUT,PDFAction.hideWidget(popup.getAnnotation(0)));
46     form.addElement("Help", help);
47
48
49     // Second example - adding up totals
50
//
51
// First create a JavaScript function in the document, which we
52
// can call. You could do it inline, which is what most JavaScript
53
// examples seem to do, but we feel this is much cleaner.
54
//
55
String JavaDoc javascript = "function myTotal(fields)\n{\n var tot=0;\n for (var i=0;i<fields.length;i++) {\n var f = this.getField(fields[i]);\n tot += f.value;\n } event.value=tot;\n}";
56     pdf.setJavaScript(javascript);
57
58     FormText field1 = new FormText(page, 100,600,150,620);
59     field1.setAction(Event.KEYPRESS,PDFAction.formJavaScript("AFNumber_Keystroke(2,1,1,0,'',true);"));
60     form.addElement("Field1", field1);
61
62     FormText field2 = new FormText(page, 170,600,220,620);
63     field2.setAction(Event.KEYPRESS,PDFAction.formJavaScript("AFNumber_Keystroke(2,1,1,0,'',true);"));
64     form.addElement("Field2", field2);
65
66     FormText field3 = new FormText(page, 250,600,300,620);
67     field3.setReadOnly(true);
68     field3.setAction(Event.OTHERCHANGE, PDFAction.formJavaScript("myTotal(new Array(\"Field1\", \"Field2\"));"));
69     form.addElement("Field3", field3);
70
71     pdf.render(new FileOutputStream("FormVoodoo.pdf"));
72     }
73 }
74
Popular Tags