I've have a editable pdf with button fields in it. I'm processing this editable pdf in my vb.net application using iTextsharp. How want to know how to catch and handle the Click event of button field inside the pdf. **Sample Document can be found here Though, i'm using iTextSharp dll, i could not find any helpful resource to handle the button events. Please guide how to handle such pdf programmatically using vb.net.
1
votes
1 Answers
1
votes
You need to attach a PdfAction to the field's "mouse up" additional action dictionary.
To add an action to an existing field is a bit harder than doing so from scratch, but still quite possible.
(Appologies, but I don't know vb.net at all, you'll have to translate from 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);
In your particular case, you might be able to simply:
PdfDictionary aaDict = new PdfDictionary();
aaDict.put(PdfName.U, buttonAction);
item.getWidget(0).put(PdfName.AA, aaDict);
This will wipe out any existing actions. That may be okay for you, it may be Very Bad.