How do I insert JavaScript code into a PDF file?
I can code JavaScript, I just wish to know how to put it into a file to, for example, display the current date, use a combobox, etc.
JavaScript is typically tied to objects in a PDF document. For example, if you want the user to be able to print the PDF by clicking a Print button, follow the steps below:
Select Action:
choose Run a JavaScript
print();
in the code windowHere's a link to an old (Acrobat 7.0.5) Acrobat JavaScript Scripting Reference to get you started.
Fresh Links 22/06/2020
You didn't stipulate a constraint for language, so you can use Python to achieve this. The library PyPDF2 has a class called addJS
that works. Here is an example from the REPL:
mypdf.addJS("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
and here is a link to the documentation. Note, that you need to have python3 installed (2.7 is going away at the end of 2019) and then run pip3 install PyPDF2
or similar to install the library and then load it via import PyPDF2
. Hopefully that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With