Is there a way to access Javascript objects exist in a PDF file from an external code, I'm trying to access JS variables in a PDF file that is embedded in a web form as an IFrame, I would like to access these variables from the webform JS code.
Asked
Active
Viewed 321 times
1 Answers
1
It's possible if the PDF allows it. I've written a book about PDF and it contains a section about establishing Javascript communication between HTML and PDF. You can find the text of this section here: http://www.javabeat.net/2011/04/javascript-communication-between-html-and-pdf-in-itext/
Note that this doesn't work on all OSSs. The PDF needs to access its host container and you need to use a message container.
If your PDF doesn't have an onMessage()
function nor a message handler, you can't establish any communication.

Bruno Lowagie
- 75,994
- 9
- 109
- 165
-
Thanks Bruno, I'm actually creating the script in the PDF myself as I'm creating the PDF from scratch using ITextSharp, so in this case I have full control of both HTML and PDF contents, which I assume it should work if I create onMessage() function in the script using ITextSharp when creating the PDF? – Maya Dec 08 '12 at 17:41
-
In that case, you can indeed add the necessary JavaScript to the PDF. See http://itextpdf.com/examples/iia.php?id=175 or http://kuujinbo.info/iTextInAction2Ed/index.aspx?ch=Chapter09&ex=JSForm You can experiment with http://examples.itextpdf.com/resources/js/post_from_html.js and http://examples.itextpdf.com/resources/js/post_to_html.js – Bruno Lowagie Dec 09 '12 at 08:44
-
Thanks for this it did the job, I have to do the other way around as well which I created a bounty for on this other question in case you are interested http://stackoverflow.com/questions/13837002/pdf-hostcontainer-callback – Maya Dec 14 '12 at 21:59