title: iText and JavaScript - show confirm() and disable textfields
body: <p>I have a short question regarding iText (2.1.7 for java) and Javascript. I have a form with several textfields and added a button. When I click the button I want to show a hint with "ok" and "cancel" and if its confirmed, I want the textfields to be disabled. Sounds easy, but somehow I have several problems.
  1. iText does not execute the app.confirm("Text") from my JavScript. I tried with app.alert("Test") and it works - so I know that my Javascript is found and used at all. But alert is not what I want here.
  2. Setting the fields to disabled or readOnly does not work at all.</p>
  
  <p>Right now these are my code-parts:</p>
  
  <pre><code>String myJavaScript =
  
              "function lock(){" +
                      "var confirmed = app.confirm('Test');" +
                      "if(confirmed){" +
                      "this.getField('abnehmer').disabled = true;}}";
  
      writer.addJavaScript(myJavaScript);
  </code></pre>
  
  <p>Instead of getField I also tried document.getElementById and this.getElementById and it also didn't work.
  As the fieldname I use the name of my PdfFormField. I create them like this:</p>
  
  <pre><code>private void insertTextFieldAndBox (PdfWriter writer, PdfContentByte contentByte, String fieldNumber, Rectangle box,
                                      String fieldName, String defaultText, boolean multiLined) throws IOException, DocumentException {
  
      ColumnText ct = new ColumnText(contentByte);
      ct.setSimpleColumn(box.getLeft(),box.getBottom(), box.getRight(), box.getTop());
      ct.addElement(createFieldnumber(fieldNumber));
      ct.go();
  
      TextField kidField = new TextField(writer, box ,null);
      kidField.setText("This is a text");
      kidField.setFont(inside_textbox.getBaseFont());
      kidField.setFontSize(inside_textbox.getSize());
      kidField.setExtraMargin(5,0);
      if(multiLined){
          kidField.setAlignment(Element.ALIGN_TOP);
          kidField.setOptions(TextField.MULTILINE);
      }
      PdfFormField textfield = kidField.getTextField();
      textfield.setFieldName(fieldName);
      writer.addAnnotation(textfield);
  
  }
  </code></pre>
  
  <p>And then for the Button (its inside a table) I use this PdfCellEvent:</p>
  
  <pre><code> class ButtonCell implements PdfPCellEvent {
          protected String fieldname;
          protected String caption;
  
          public ButtonCell(String fieldname, String caption) {
              this.fieldname = fieldname;
              this.caption = caption;
          }
  
  
          public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
  
              PdfWriter writer = canvases[PdfPTable.BASECANVAS].getPdfWriter();
              PushbuttonField button = new PushbuttonField(writer, rectangle, "MyButton");
              button.setRotation(90);
              button.setBackgroundColor(new GrayColor(0.75f));
              button.setBorderColor(GrayColor.GRAYBLACK);
              button.setBorderWidth(1);
              button.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
              button.setTextColor(GrayColor.GRAYBLACK);
              button.setFontSize(12);
              button.setText("Lock document");
  
              PdfFormField field;
              try {
                  field = button.getField();
                  field.setAction(PdfAction.javaScript("lock()", writer));
                  writer.addAnnotation(field);
              } catch (Exception e) {
                  e.printStackTrace();
              }
  
          }
  
      }
  </code></pre>