Example usage for com.lowagie.text.pdf PushbuttonField PushbuttonField

List of usage examples for com.lowagie.text.pdf PushbuttonField PushbuttonField

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PushbuttonField PushbuttonField.

Prototype

public PushbuttonField(PdfWriter writer, Rectangle box, String fieldName) 

Source Link

Document

Creates a new instance of PushbuttonField

Usage

From source file:classroom.filmfestival_c.Movies22.java

public static void main(String[] args) {
    // step 1//  w  w w .j  a v  a 2s  .  c  o m
    Document document = new Document(new Rectangle(WIDTH, HEIGHT));
    try {
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FORM));
        writer.setViewerPreferences(PdfWriter.PageLayoutSinglePage);
        // step 3
        document.open();
        // step 4
        PdfContentByte cb = writer.getDirectContent();

        // top part of the card
        cb.setColorFill(RED);
        cb.rectangle(0, mm2pt(57f), WIDTH, HEIGHT - mm2pt(57f));
        cb.fill();
        cb.setColorFill(BLACK);
        cb.rectangle(0, mm2pt(61), WIDTH, mm2pt(21f));
        cb.fill();
        cb.setColorFill(WHITE);

        Image kubrick = Image.getInstance(KUBRICK);
        kubrick.scaleToFit(WIDTH, mm2pt(21));
        kubrick.setAbsolutePosition(0, mm2pt(61));
        cb.addImage(kubrick);

        Image logo = Image.getInstance(LOGO);
        logo.scaleToFit(mm2pt(14), mm2pt(14));
        logo.setAbsolutePosition(mm2pt(37), mm2pt(65));
        cb.addImage(logo);

        cb.beginText();
        cb.setFontAndSize(FONT, 7);
        cb.showTextAligned(Element.ALIGN_CENTER, "Flanders International Film Festival-Ghent", WIDTH / 2,
                mm2pt(58.5f), 0);
        cb.endText();

        // bottom part of the card

        TextField filmfestival = new TextField(writer, new Rectangle(0, 0, WIDTH, mm2pt(9)), "filmfestival");
        filmfestival.setBackgroundColor(GRAY);
        filmfestival.setTextColor(WHITE);
        filmfestival.setText("www.filmfestival.be");
        filmfestival.setFontSize(14);
        filmfestival.setOptions(TextField.READ_ONLY);
        filmfestival.setAlignment(Element.ALIGN_CENTER);
        writer.addAnnotation(filmfestival.getTextField());

        cb.beginText();
        cb.moveText(mm2pt(1.5f), mm2pt(12));
        cb.setFontAndSize(FONT, 21);
        cb.setColorFill(RED);
        cb.showText("10");
        cb.setColorFill(BLUE);
        cb.showText("/");
        cb.setColorFill(RED);
        cb.showText("21");
        cb.setColorFill(BLUE);
        cb.showText("OCT");
        cb.setColorFill(GREEN);
        cb.showText("2006");
        cb.endText();
        cb.setColorStroke(BLUE);
        cb.moveTo(mm2pt(24.5f), mm2pt(29));
        cb.lineTo(mm2pt(24.5f), mm2pt(36));
        cb.moveTo(mm2pt(24.5f), mm2pt(48));
        cb.lineTo(mm2pt(24.5f), mm2pt(54));
        cb.stroke();

        // central part of the card
        PushbuttonField photo = new PushbuttonField(writer,
                new Rectangle(mm2pt(3), mm2pt(29), mm2pt(24), mm2pt(54)), "photo");
        PdfTemplate t1 = cb.createTemplate(mm2pt(21), mm2pt(25));
        t1.setColorFill(GRAY);
        t1.rectangle(0, 0, mm2pt(21), mm2pt(25));
        t1.fill();
        photo.setTemplate(t1);
        photo.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
        writer.addAnnotation(photo.getField());

        TextField type = new TextField(writer,
                new Rectangle(mm2pt(26), mm2pt(46), WIDTH - mm2pt(1.5f), mm2pt(54)), "type");
        type.setTextColor(GRAY);
        type.setText("TYPE");
        type.setFontSize(0);
        writer.addAnnotation(type.getTextField());

        TextField number = new TextField(writer,
                new Rectangle(mm2pt(26), mm2pt(44), WIDTH - mm2pt(1.5f), mm2pt(48)), "number");
        number.setText("N\u00b0 0000000");
        number.setFontSize(8);
        writer.addAnnotation(number.getTextField());

        TextField name = new TextField(writer,
                new Rectangle(mm2pt(26), mm2pt(28), WIDTH - mm2pt(1.5f), mm2pt(40)), "name");
        name.setText("Name");
        name.setFontSize(8);
        name.setOptions(TextField.MULTILINE);
        writer.addAnnotation(name.getTextField());

        PushbuttonField barcode = new PushbuttonField(writer,
                new Rectangle(mm2pt(3), mm2pt(23), WIDTH - mm2pt(3), mm2pt(28)), "barcode");
        PdfTemplate t2 = cb.createTemplate(WIDTH - mm2pt(6), mm2pt(5));
        t2.setColorFill(GRAY);
        t2.rectangle(0, 0, WIDTH - mm2pt(6), mm2pt(5));
        t2.fill();
        barcode.setTemplate(t2);
        barcode.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
        writer.addAnnotation(barcode.getField());

    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // step 4
    document.close();
}

From source file:classroom.filmfestival_c.Movies25.java

public static void createTemplate() {
    // our template will be 35 x 50 mm
    Document document = new Document(
            new Rectangle(Utilities.millimetersToPoints(35), Utilities.millimetersToPoints(50)));
    try {// w  ww  .  ja va  2 s  . c o  m
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(TEMPLATE));
        writer.setViewerPreferences(PdfWriter.PageLayoutSinglePage);
        // step 3
        document.open();
        // step 4

        // space reserved for the film poster
        PushbuttonField foto = new PushbuttonField(writer,
                new Rectangle(Utilities.millimetersToPoints(0), Utilities.millimetersToPoints(25),
                        Utilities.millimetersToPoints(35), Utilities.millimetersToPoints(50)),
                POSTER);
        foto.setBackgroundColor(Color.BLUE);
        writer.addAnnotation(foto.getField());

        // space reserved for the text
        TextField tekst = new TextField(writer,
                new Rectangle(Utilities.millimetersToPoints(0), Utilities.millimetersToPoints(7),
                        Utilities.millimetersToPoints(35), Utilities.millimetersToPoints(25)),
                TEXT);
        tekst.setOptions(TextField.MULTILINE);
        writer.addAnnotation(tekst.getTextField());

        // space reserved for the year
        TextField prijs = new TextField(writer,
                new Rectangle(Utilities.millimetersToPoints(0), Utilities.millimetersToPoints(0),
                        Utilities.millimetersToPoints(35), Utilities.millimetersToPoints(7)),
                YEAR);
        prijs.setAlignment(Element.ALIGN_CENTER);
        prijs.setBackgroundColor(Color.BLUE);
        prijs.setTextColor(Color.WHITE);
        writer.addAnnotation(prijs.getTextField());
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    // step 5
    document.close();
}

From source file:classroom.newspaper_b.Newspaper09.java

public static void main(String[] args) {
    try {/*ww w  .  j a  v a  2 s  . c o  m*/
        PdfReader reader = new PdfReader(NEWSPAPER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));

        PdfAnnotation annotation1 = PdfAnnotation.createSquareCircle(stamper.getWriter(),
                new Rectangle(LLX1, LLY1, URX1, URY1), MESSAGE, true);
        annotation1.put(PdfName.T, new PdfString("Advertisement 1"));
        annotation1.put(PdfName.C, new PdfArray(new float[] { 1, 0, 0 }));
        stamper.addAnnotation(annotation1, 1);

        PdfAnnotation annotation2 = PdfAnnotation.createText(stamper.getWriter(),
                new Rectangle(LLX2, LLY2, URX2, URY2), "Advertisement 2", MESSAGE, false, null);
        annotation2.put(PdfName.NM, new PdfString("ad2"));
        // the text must be read only, and the annotation set to NOVIEW
        annotation2.put(PdfName.F, new PdfNumber(PdfAnnotation.FLAGS_READONLY | PdfAnnotation.FLAGS_NOVIEW));

        // we create a popup annotation that will define where the rectangle will appear
        PdfAnnotation popup = PdfAnnotation.createPopup(stamper.getWriter(),
                new Rectangle(LLX2 + 50, LLY2 + 120, URX2 - 80, URY2 - 120), null, false);

        // we add a reference to the text annotation to the popup annotation
        popup.put(PdfName.PARENT, annotation2.getIndirectReference());
        // we add a reference to the popup annotation to the text annotation
        annotation2.put(PdfName.POPUP, popup.getIndirectReference());

        // we add both annotations to the writer
        stamper.addAnnotation(annotation2, 1);
        stamper.addAnnotation(popup, 1);

        // the text annotation can't be viewed (it's invisible)
        // we create a widget annotation named mywidget (it's a button field)
        PushbuttonField field = new PushbuttonField(stamper.getWriter(), new Rectangle(LLX2, LLY2, URX2, URY2),
                "button");
        PdfAnnotation widget = field.getField();
        PdfDictionary dict = new PdfDictionary();
        // we write some javascript that makes the popup of the text annotation visible/invisible on mouse enter/exit
        String js1 = "var t = this.getAnnot(this.pageNum, 'ad2'); t.popupOpen = true; var w = this.getField('button'); w.setFocus();";
        PdfAction enter = PdfAction.javaScript(js1, stamper.getWriter());
        dict.put(PdfName.E, enter);
        String js2 = "var t = this.getAnnot(this.pageNum, 'ad2'); t.popupOpen = false;";
        PdfAction exit = PdfAction.javaScript(js2, stamper.getWriter());
        dict.put(PdfName.X, exit);
        // we add the javascript as additional action
        widget.put(PdfName.AA, dict);
        // we add the button field
        stamper.addAnnotation(widget, 1);

        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:classroom.newspaper_b.Newspaper10.java

public static void main(String[] args) {
    try {//from   w  w w.ja v a  2s  . co  m
        PdfReader reader = new PdfReader(NEWSPAPER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        PushbuttonField button;
        Rectangle rect;

        rect = new Rectangle(100, 980, 700, 1000);
        button = new PushbuttonField(stamper.getWriter(), rect, "click");
        button.setBackgroundColor(Color.ORANGE);
        button.setText("Click here to close window");
        button.setLayout(PushbuttonField.LAYOUT_LABEL_ONLY);
        button.setAlignment(Element.ALIGN_RIGHT);
        PdfFormField menubar = button.getField();
        String js = "var f1 = getField('click'); f1.display = display.hidden;"
                + "var f2 = getField('advertisement'); f2.display = display.hidden;";
        menubar.setAction(PdfAction.javaScript(js, stamper.getWriter()));
        stamper.addAnnotation(menubar, 1);

        rect = new Rectangle(100, 500, 700, 980);
        button = new PushbuttonField(stamper.getWriter(), rect, "advertisement");
        button.setBackgroundColor(Color.WHITE);
        button.setBorderColor(Color.ORANGE);
        button.setImage(Image.getInstance(IMG));
        button.setText("Buy the book iText in Action");
        button.setLayout(PushbuttonField.LAYOUT_LABEL_TOP_ICON_BOTTOM);
        PdfFormField advertisement = button.getField();
        advertisement.setAction(new PdfAction("http://www.1t3xt.com/docs/book.php"));
        stamper.addAnnotation(advertisement, 1);
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:classroom.newspaper_b.Newspaper13.java

public static void addButton(PdfStamper stamper, Rectangle rect, String path, String name, int pagenumber)
        throws IOException, DocumentException {
    PushbuttonField field = new PushbuttonField(stamper.getWriter(), rect, name);
    if (path.endsWith(".pdf")) {
        PdfReader reader = new PdfReader(path);
        field.setTemplate(stamper.getImportedPage(reader, 1));
    } else {/*from   w w  w  .j av a2 s  .  c  o  m*/
        field.setImage(Image.getInstance(path));
    }
    field.setBackgroundColor(new Color(0xFF, 0xFF, 0xFF));
    field.setBorderColor(new Color(0xC0, 0xC0, 0xC0));
    field.setBorderWidth(0.5f);
    field.setScaleIcon(PushbuttonField.SCALE_ICON_ALWAYS);
    field.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
    stamper.addAnnotation(field.getField(), pagenumber);
}

From source file:com.geek.tutorial.itext.acroform.ListFieldForm.java

License:Open Source License

public ListFieldForm() throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ListFieldForm.pdf"));
    document.open();//from  w  ww.jav  a2 s .c om

    // Code 1
    String options[] = { "PS3", "XBOX 360", "Wii", "PSP", "NDS", "GBA" };

    // Code 2 create drop-down list
    PdfFormField dropDown = PdfFormField.createCombo(writer, true, options, 0);
    dropDown.setWidget(new Rectangle(50, 785, 120, 800), PdfAnnotation.HIGHLIGHT_INVERT);
    dropDown.setFieldName("dropDownList");
    dropDown.setValueAsString("PS3");
    dropDown.setMKBorderColor(Color.BLACK);
    writer.addAnnotation(dropDown);

    // Code 3 create scrollable list
    TextField scrollableList = new TextField(writer, new Rectangle(150, 740, 250, 800), "scrollableList");
    scrollableList.setBackgroundColor(Color.WHITE);
    scrollableList.setBorderColor(Color.BLUE);
    scrollableList.setBorderWidth(2);
    scrollableList.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
    scrollableList.setFontSize(10);
    scrollableList.setChoices(options);
    scrollableList.setChoiceSelection(0);
    writer.addAnnotation(scrollableList.getListField());

    // Code 4 add function and button for showing state 
    writer.addJavaScript(
            "function showState(){" + "app.alert('DropDown:'+ this.getField('dropDownList').value +'\\n'+"
                    + "'Scrollable List:'+this.getField('scrollableList').value);" + "}");
    PushbuttonField push = new PushbuttonField(writer, new Rectangle(70, 710, 140, 730), "pushAction");
    push.setBackgroundColor(Color.LIGHT_GRAY);
    push.setBorderColor(Color.GRAY);
    push.setText("Show State");
    push.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
    push.setTextColor(Color.BLACK);
    PdfFormField pushbutton = push.getField();
    pushbutton.setAction(PdfAction.javaScript("showState()", writer));
    writer.addAnnotation(pushbutton);

    document.close();
}

From source file:com.geek.tutorial.itext.acroform.RadioCheckBoxForm.java

License:Open Source License

public RadioCheckBoxForm() throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("RadioCheckBoxForm.pdf"));
    document.open();//from   ww w  .  j ava  2 s  . c  om
    PdfContentByte cb = writer.getDirectContent();

    BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    Rectangle rect;

    // Code 1 create radio button
    String[] radios = { "Radio1", "Radio2", "Radio3" };
    PdfFormField radioField = PdfFormField.createRadioButton(writer, true);
    radioField.setFieldName("radioField");
    radioField.setValueAsName(radios[0]);
    for (int i = 0; i < radios.length; i++) {
        rect = new Rectangle(40, 815 - i * 30, 60, 797 - i * 30);
        addRadioButton(writer, rect, radioField, radios[i], i == 0);
        cb.beginText();
        cb.setFontAndSize(bf, 12);
        cb.showTextAligned(Element.ALIGN_LEFT, radios[i], 70, 802 - i * 30, 0);
        cb.endText();
    }
    writer.addAnnotation(radioField);

    // Code 2 create checkbox button
    String[] options = { "Check1", "Check2", "Check3" };
    for (int i = 0; i < options.length; i++) {
        rect = new Rectangle(160, 815 - i * 30, 180, 797 - i * 30);
        addCheckbox(writer, rect, options[i]);
        cb.beginText();
        cb.setFontAndSize(bf, 12);
        cb.showTextAligned(Element.ALIGN_LEFT, options[i], 190, 802 - i * 30, 0);
        cb.endText();
    }

    // Code 3 add function and button for showing state 
    writer.addJavaScript(
            "function showState(){" + "app.alert('Radio:'+ this.getField('radioField').value +'\\n\\n'+"
                    + "'Check1:'+this.getField('Check1').value +'\\n'+"
                    + "'Check2:'+this.getField('Check2').value +'\\n'+"
                    + "'Check3:'+this.getField('Check3').value);" + "}");
    PushbuttonField push = new PushbuttonField(writer, new Rectangle(80, 710, 150, 730), "pushAction");
    push.setBackgroundColor(Color.LIGHT_GRAY);
    push.setBorderColor(Color.GRAY);
    push.setText("Show State");
    push.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
    push.setTextColor(Color.BLACK);
    PdfFormField pushbutton = push.getField();
    pushbutton.setAction(PdfAction.javaScript("showState()", writer));
    writer.addAnnotation(pushbutton);

    document.close();
}

From source file:com.geek.tutorial.itext.acroform.TextFieldForm.java

License:Open Source License

public TextFieldForm() throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TextFieldForm.pdf"));
    document.open();//from   w ww. j  a va2 s.  c om

    PdfPTable table = new PdfPTable(2);
    table.getDefaultCell().setPadding(5f); // Code 1, will only affect empty field
    table.setHorizontalAlignment(Element.ALIGN_LEFT);
    PdfPCell cell;

    // Code 2, add name TextField      
    table.addCell("Name");
    TextField nameField = new TextField(writer, new Rectangle(0, 0, 200, 10), "nameField");
    nameField.setBackgroundColor(Color.WHITE);
    nameField.setBorderColor(Color.BLACK);
    nameField.setBorderWidth(1);
    nameField.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
    nameField.setText("");
    nameField.setAlignment(Element.ALIGN_LEFT);
    nameField.setOptions(TextField.REQUIRED);
    cell = new PdfPCell();
    cell.setMinimumHeight(10);
    cell.setCellEvent(new FieldCell(nameField.getTextField(), 200, writer));
    table.addCell(cell);

    // force upper case javascript
    writer.addJavaScript("var nameField = this.getField('nameField');" + "nameField.setAction('Keystroke',"
            + "'forceUpperCase()');" + "" + "function forceUpperCase(){"
            + "if(!event.willCommit)event.change = " + "event.change.toUpperCase();" + "}");

    // Code 3, add empty row
    table.addCell("");
    table.addCell("");

    // Code 4, add age TextField
    table.addCell("Age");
    TextField ageComb = new TextField(writer, new Rectangle(0, 0, 30, 10), "ageField");
    ageComb.setBorderColor(Color.BLACK);
    ageComb.setBorderWidth(1);
    ageComb.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
    ageComb.setText("12");
    ageComb.setAlignment(Element.ALIGN_RIGHT);
    ageComb.setMaxCharacterLength(2);
    ageComb.setOptions(TextField.COMB | TextField.DO_NOT_SCROLL);
    cell = new PdfPCell();
    cell.setMinimumHeight(10);
    cell.setCellEvent(new FieldCell(ageComb.getTextField(), 30, writer));
    table.addCell(cell);

    // validate age javascript
    writer.addJavaScript("var ageField = this.getField('ageField');"
            + "ageField.setAction('Validate','checkAge()');" + "function checkAge(){" + "if(event.value < 12){"
            + "app.alert('Warning! Applicant\\'s age can not" + " be younger than 12.');" + "event.value = 12;"
            + "}}");

    // add empty row
    table.addCell("");
    table.addCell("");

    // Code 5, add age TextField
    table.addCell("Comment");
    TextField comment = new TextField(writer, new Rectangle(0, 0, 200, 100), "commentField");
    comment.setBorderColor(Color.BLACK);
    comment.setBorderWidth(1);
    comment.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
    comment.setText("");
    comment.setOptions(TextField.MULTILINE | TextField.DO_NOT_SCROLL);
    cell = new PdfPCell();
    cell.setMinimumHeight(100);
    cell.setCellEvent(new FieldCell(comment.getTextField(), 200, writer));
    table.addCell(cell);

    // check comment characters length javascript
    writer.addJavaScript("var commentField = " + "this.getField('commentField');" + "commentField"
            + ".setAction('Keystroke','checkLength()');" + "function checkLength(){"
            + "if(!event.willCommit && " + "event.value.length > 100){" + "app.alert('Warning! Comment can not "
            + "be more than 100 characters.');" + "event.change = '';" + "}}");

    // add empty row
    table.addCell("");
    table.addCell("");

    // Code 6, add submit button   
    PushbuttonField submitBtn = new PushbuttonField(writer, new Rectangle(0, 0, 35, 15), "submitPOST");
    submitBtn.setBackgroundColor(Color.GRAY);
    submitBtn.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
    submitBtn.setText("POST");
    submitBtn.setOptions(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);
    PdfFormField submitField = submitBtn.getField();
    submitField.setAction(PdfAction.createSubmitForm("http://www.geek-tutorials.com/java/itext/submit.php",
            null, PdfAction.SUBMIT_HTML_FORMAT));
    cell = new PdfPCell();
    cell.setMinimumHeight(15);
    cell.setCellEvent(new FieldCell(submitField, 35, writer));
    table.addCell(cell);

    // Code 7, add reset button
    PushbuttonField resetBtn = new PushbuttonField(writer, new Rectangle(0, 0, 35, 15), "reset");
    resetBtn.setBackgroundColor(Color.GRAY);
    resetBtn.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
    resetBtn.setText("RESET");
    resetBtn.setOptions(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);
    PdfFormField resetField = resetBtn.getField();
    resetField.setAction(PdfAction.createResetForm(null, 0));
    cell = new PdfPCell();
    cell.setMinimumHeight(15);
    cell.setCellEvent(new FieldCell(resetField, 35, writer));
    table.addCell(cell);

    document.add(table);
    document.close();
}

From source file:questions.javascript.AddJavaScriptToForm.java

public static void addJavaScript(String input, String output) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(input);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(output));
    stamper.getWriter()//from www.  j a va2 s .  c  om
            .addJavaScript("function setReadOnly(readonly) {" + "var partner = this.getField('partner');"
                    + "if(readonly) {" + "partner.value = '';" + "}" + "partner.readonly = readonly;" + " }"
                    + "function validate() {" + "var married = this.getField('married');"
                    + "var partner = this.getField('partner');"
                    + "if (married.value == 'yes' && partner.value == '') {"
                    + "app.alert('please enter the name of your partner');" + "}" + "else {"
                    + "this.submitForm({" + " cURL:\"http://1t3xt.info/examples/request.php\","
                    + " cSubmitAs: \"HTML\"" + "});" + "}" + " }");
    AcroFields form = stamper.getAcroFields();
    Item fd = form.getFieldItem("married");

    PdfDictionary dictYes = (PdfDictionary) PdfReader.getPdfObject((PdfObject) fd.getWidgetRef(0));
    PdfDictionary yesAction = dictYes.getAsDict(PdfName.AA);
    if (yesAction == null)
        yesAction = new PdfDictionary();
    yesAction.put(new PdfName("Fo"), PdfAction.javaScript("setReadOnly(false);", stamper.getWriter()));
    dictYes.put(PdfName.AA, yesAction);

    PdfDictionary dictNo = (PdfDictionary) PdfReader.getPdfObject((PdfObject) fd.getWidgetRef(1));
    PdfDictionary noAction = dictNo.getAsDict(PdfName.AA);
    if (noAction == null)
        noAction = new PdfDictionary();
    noAction.put(new PdfName("Fo"), PdfAction.javaScript("setReadOnly(true);", stamper.getWriter()));
    dictNo.put(PdfName.AA, noAction);

    PdfWriter writer = stamper.getWriter();
    PushbuttonField button = new PushbuttonField(writer, new Rectangle(40, 690, 200, 710), "submit");
    button.setText("validate and submit");
    button.setOptions(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);
    PdfFormField validateAndSubmit = button.getField();
    validateAndSubmit.setAction(PdfAction.javaScript("validate();", stamper.getWriter()));
    stamper.addAnnotation(validateAndSubmit, 1);

    stamper.close();
}

From source file:questions.javascript.TriggerMenuButtons.java

public static final void main(String[] args) {
    Document document = new Document();
    try {/*from w ww .  j  av  a  2 s  .  c  om*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        document.add(new Paragraph("Triggering Menu Items"));
        PushbuttonField saveAs = new PushbuttonField(writer, new Rectangle(40, 760, 100, 780), "Save");
        saveAs.setBorderColor(Color.BLACK);
        saveAs.setText("Save");
        saveAs.setTextColor(Color.RED);
        saveAs.setLayout(PushbuttonField.LAYOUT_LABEL_ONLY);
        PdfFormField saveAsButton = saveAs.getField();
        saveAsButton.setAction(PdfAction.javaScript("app.execMenuItem('SaveAs')", writer));
        writer.addAnnotation(saveAsButton);
        PushbuttonField mail = new PushbuttonField(writer, new Rectangle(120, 760, 180, 780), "Mail");
        mail.setBorderColor(Color.BLACK);
        mail.setText("Mail");
        mail.setTextColor(Color.RED);
        mail.setLayout(PushbuttonField.LAYOUT_LABEL_ONLY);
        PdfFormField mailButton = mail.getField();
        mailButton.setAction(PdfAction.javaScript("app.execMenuItem('AcroSendMail:SendMail')", writer));
        writer.addAnnotation(mailButton);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    document.close();
}