您需要将PdfAction 附加到该字段的“鼠标向上”附加操作字典。
向现有字段添加操作比从头开始要困难一些,但仍然很有可能。
(请教,但我根本不懂vb.net,你得从Java翻译过来)
// Create your action.
// There are quite a few static functions in PdfAction to choose from.
PdfAction buttonAction = PdfAction.javaScript(writer, scriptStr);
AcroFields.Item buttonItem = myAcroFields.getFieldItem(buttonFldName);
// "AA" stands for "additional actions"
PdfDictionary aaDict = buttomItem.getMerged(0).getAsDict(PdfName.AA);
if (aaDict == null) {
aaDict = new PdfDictionary();
} else { // this button already has an AA dictionary.
// if there's an existing up action, preserve it, but run ours first.
PdfDictionary upAction = aaDict.getAsDict(PdfName.U);
if (upAction != null) {
PdfIndirectReference ref = aaDict.getAsIndirect(PdfName.U);
if (ref != null) {
buttonAction.put(PdfName.NEXT, ref);
} else {
buttonAction.put(PdfName.NEXT, upAction);
}
}
}
aaDict.put(PdfName.U, buttonAction);
int writeFlags = AcroFields.Item.WRITE_WIDGET | AcroFields.Item.WRITE_MERGED;
buttomItem.writeToAll(PdfName.AA, aaDict, writeFlags);
在您的特定情况下,您可以简单地:
PdfDictionary aaDict = new PdfDictionary();
aaDict.put(PdfName.U, buttonAction);
item.getWidget(0).put(PdfName.AA, aaDict);
这将清除所有现有操作。这对你来说可能没问题,但可能非常糟糕。