Licinio Gomez wrote:
> Hi list,
>
> I have one pdf with one acroform
> I don´t create the pdf with itext, it´s already created.
>
> Is there anyway to add one javascript action to one of those fields?
>
> I´ve tried with PdfStamper, but I can´t find the way of doing it
For this example, I take the HelloWorldForm.pdf from the book:
PdfReader reader = new PdfReader("HelloWorldForm.pdf");
It's a form with only one text field.
Suppose I want to add some JavaScript that shows the key that was
pushed in an alert box when entering something into the field from
the keyboard:
PdfDictionary js = new PdfDictionary();
js.put(PdfName.S, PdfName.JAVASCRIPT);
js.put(PdfName.JS, new PdfString("app.alert(event.change);"));
PdfDictionary aa = new PdfDictionary();
aa.put(PdfName.K, js);
This JavaScript is going to be added as an additional action.
(If you have the book, you'll find out more types of actions
that can be triggered by a field.)
Now we are going to find the field dictionary in the existing form:
PdfDictionary catalog = reader.getCatalog();
PdfDictionary form =
(PdfDictionary)PdfReader.getPdfObject(catalog.get(PdfName.ACROFORM));
PdfArray fields = (PdfArray)form.get(PdfName.FIELDS);
PdfDictionary field =
(PdfDictionary)PdfReader.getPdfObject((PdfObject)fields.getArrayList().get(0));
Note that the next iText version will have lots of convenience
methods to get the correct object. But for now, we have to to
it with casts and PdfReader.getPdfObject.
Let's add the JavaScript:
field.put(PdfName.AA, aa);
We're almost done, we just have to create and close a stamper:
PdfStamper stamper = new PdfStamper(reader, new
FileOutputStream("HelloWithJS.pdf"));
stamper.close();
It's as easy as that, that is: if you know your PDF.
br,
Bruno
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/