I do not find anything related in the documentation: https://www.ghostscript.com/doc/current/Use.htm
I did find a somewhat related example for inserting watermarks on each page, so it seems like it may be possible.
Is it possible in Ghostscript to add watermark to every page in PDF
However, it's not clear from that example how to insert Javascript.
If you want to execute JavaScript on document load you can do it like this (thanks to this useful PDF primer).
[ /_objdef {MyAlert} /type /dict /OBJ pdfmark
[ {MyAlert} << /JavaScript << /Names [ (MyAlertCode) << /S /JavaScript
/JS (app.alert\("Hello world 1."\);app.alert\("Hello world 2."\);) >> ]
>> >>
/PUT pdfmark
[ {Catalog} << /Names {MyAlert} >> /PUT pdfmark
Full steps to reproduce would be:
gs
command).ps
) file that contains the code from above.cd
into that foldergs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=newpdf.pdf myps.ps mypdf.pdf
(obviously replace myps.ps
and mypdf.pdf
with your actual file names).newpdf.pdf
to verify that it worked!Other tips:
app.alert
in the example rather than the traditional alert()
.) Don't just assume the function you're trying to use exists.app.alert\("Hello World"\)
Resources: