Adding javascript to pdf pages in Java using Aspose.PDF

Can you point me to how to inject javascript on a pdf page using the java api? I want to execute some javascript when a page is viewed.

TIA,
An

Hi there,

Thanks for your inquiry. You can easily add JavaScript to you PDF document using Aspose.Pdf for Java. Please check following documentation link for details and code snippet. Hopefully it will help you to accomplish the task.

Please feel free to contact us for any further assistance.

Best Regards,

Hi,

Yes I’ve seen this mentioning of adding javascript to a pdf document, but is there a way to add a javascript at the page level, i.e. a different script on each page such that it will get executed when a page is viewed?
Thanks,
An

Hi there


Thanks for your feedback. You can set page open/close action as well. Please check following code snippet for the purpose. Hopefully it will help you to accomplish the task.

Document doc = new Document(myDir + “s2.pdf”);
//document level JavaScript
JavascriptAction javaScript=new JavascriptAction(“this.print({bUI:true,bSilent:false,bShrinkToFit:true});”);
doc.setOpenAction(javaScript);

//Page level JavaScript
doc.getPages().get_Item(2).getActions().setOnOpen(new JavascriptAction(“app.alert(‘page 2 is opened’)”));
doc.getPages().get_Item(2).getActions().setOnClose(new JavascriptAction(“app.alert(‘page 2 is closed’)”));
doc.save(myDir + “JavaScript-Added.pdf”);

Please feel free to contact us for any further assistance.

Best Regards,

Thanks for the info. We’re testing Aspose Cloud right now while setting up a server for Aspose.java. is it possible for us to test what you’ve suggested through the Aspose Cloud REST API?

Thanks,
An

Hi there,


Thanks for your feedback. I am afraid currently the subjected feature is not available in Aspose Cloud. However, we have a logged a new feature request, SAASPDF-122, in our issue tracking system for further investigation and implementation. We will notify you as soon as it is implemented.

We are sorry for the inconvenience caused.

Best Regards,
//document level JavaScript
JavascriptAction javaScript=new JavascriptAction("this.print({bUI:true,bSilent:false,bShrinkToFit:true});");
doc.setOpenAction(javaScript);

We want to set some javascript to be executed when the document is closed. We expect a doc.setCloseAction() but cannot find one. Is there one?
TIA,
An

Hi there,


Thanks for your query. You can use setBeforeClosing() property from Action collection of Document as following. Hopefully it will help you to accomplish the task.

doc.getActions().setBeforeClosing()


Please feel free to contact us for any further assistance.

Best Regards,

Hi ,

Are there any limitations for javascript to work on the pdf.
I added this action and it is not working.

JavascriptAction javaScript=new JavascriptAction(“this.print({bUI:true,bSilent:false,bShrinkToFit:false});”);
doc.setOpenAction(javaScript);

Thanks,
Mary.

@marybellapu,

We have tested your scenario in our environment, and it is working fine. This is the output PDF: JavaScript-Added.pdf (1.6 KB). If this does not help, then please send us your source PDF and code. We will investigate your scenario in our environment, and share our findings with you.