Example usage for com.lowagie.text.pdf TextField setChoiceSelection

List of usage examples for com.lowagie.text.pdf TextField setChoiceSelection

Introduction

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

Prototype

public void setChoiceSelection(int choiceSelection) 

Source Link

Document

Sets the zero based index of the selected item.

Usage

From source file:at.reppeitsolutions.formbuilder.components.pdf.itext.ITextSelect.java

License:Open Source License

@Override
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
    PdfWriter writer = canvases[0].getPdfWriter();
    TextField text = new TextField(writer, rectangle, String.format("choice_" + UUID.randomUUID().toString()));
    text.setBorderStyle(PdfBorderDictionary.STYLE_INSET);
    text.setChoices(values);//from   w  w  w . j  a v  a 2  s.  co m
    text.setFontSize(ITextInputText.FONTSIZE);
    text.setChoiceExports(text.getChoices());
    for (int i = 0; value != null && i < values.length; i++) {
        if (values[i].equals(value)) {
            text.setChoiceSelection(i);
            break;
        }
    }
    try {
        PdfFormField comboField = text.getComboField();
        if (locked) {
            comboField.setFieldFlags(BaseField.READ_ONLY);
        }
        writer.addAnnotation(comboField);
    } catch (IOException ex) {
        Logger.getLogger(ITextSelect.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(ITextSelect.class.getName()).log(Level.SEVERE, null, ex);
    }
}

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 w w.  j  av a  2 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();
}