1 import org.faceless.pdf2.*; 2 import java.io.*; 3 import java.awt.Color ; 4 5 public class FormVoodoo 11 { 12 public static void main(String [] 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 (240,240,255)); 21 background.setLineColor(Color.blue); 22 background.setFormStyle(PDFStyle.FORMSTYLE_BEVEL); 23 form.setBackgroundStyle(background); 24 25 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 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 String 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 |