is there any way to add Document level Javascript? #2157
-
Hi everyone! In Adobe, Document level Javascript can be added to the PDF file, can this Javascript be added to the PDF file using PyMuPDF
When user opens the PDF file, Javascript code (Document level Javascript code added by PyMuPDF) should get executed which adds a mouse enter and mouse exit event on all the buttons present in the PDF file to show or hide the OCG layers when these events occur. Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You have to set
Define the JS action object: xref = doc.get_new_xref()
js = "the javascript text"
objsource = f"<</Type/Action/S/JavaScript/JS({js})>>"
doc.update_object(xref, objsource) Update the catalog: cat = doc.pdf_catalog()
doc.xref_set_key(cat, "OpenAction", f"{xref} 0 R") Then save. Fingers crossed that everything works ... 😎 |
Beta Was this translation helpful? Give feedback.
You have to set
OpenAction
to a JS action dictionary object.This is currently not implemented as such in PyMuPDF.
But it should be possible to achieve your goal using PyMuPDF low-level interface calls. Approach could be along the lines:
OpenAction
key pointing to the JS action dictionary objectDefine the JS action object:
Update the catalog:
Then save. Fingers crossed that everything works ... 😎