我有一个关于iText(java为2.1.7)和Javascript的简短问题。我有一个包含多个文本字段的表单并添加了一个按钮。当我单击按钮时,我想显示“ok”和“cancel”的提示,如果确认,我希望禁用文本字段。听起来很简单,但不知怎的,我有几个问题。 1. iText不会从我的JavScript执行app.confirm(“Text”)。我尝试使用app.alert(“测试”)并且它有效 - 所以我知道我的Javascript被找到并被使用了。但警报不是我想要的。 2.将字段设置为disabled或readOnly根本不起作用。
现在这些是我的代码部分:
String myJavaScript =
"function lock(){" +
"var confirmed = app.confirm('Test');" +
"if(confirmed){" +
"this.getField('abnehmer').disabled = true;}}";
writer.addJavaScript(myJavaScript);
我没有使用getField,而是尝试了document.getElementById和this.getElementById,但它也没有用。 作为字段名,我使用我的PdfFormField的名称。我像这样创建它们:
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);
}
然后对于Button(它在表格内)我使用这个PdfCellEvent:
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();
}
}
}