Skip to main content
Known Participant
January 22, 2025
Answered

Can an HTML event trigger a PDF JavaScript event with HostContainer?

  • January 22, 2025
  • 1 reply
  • 762 views

I have a PDF in an HTML OBJECT container and I want to have an HTML onmouseover event send a string to the PDF's JavaScript that will show the layer specified by the string and then hide that layer on an HTML onmouseout event. So, my question is is HostContainer the object to use to do this, accept an HTML message and then pass the string to the PDF's JavaScript?

Correct answer Thom Parker

I do have one more question in order to get started. I've read your Where does JavaScript Go article (https://acrobatusers.com/tutorials/so-where-does-javascript-go-in-acrobat/) and I see where to add a JavaScript routine in tht text field properties window, but can't figure out how I'd add a JavaScript function that is used by all scripts, such as:

function find(ocgname){
    var layers = this.getOCGs();
    for(var i=0; i < layers.length; i++) { 
        if(layers[i].name == ocgname) return layers[i];
    }
    return null;
}

that would be used by all scripts entered in a text field's properties window, each text field's script would pass its specific argument to find(). How would function scripts be added to a PDF to be called by each function? Or, is the intent of Adobe to NOT have universally available functions, treat each text field as a stand alone field?

 

Can you point me to an article about that? I'll keep searching in the meantime.


To define a document global function, you need a document level script.

https://www.pdfscripting.com/public/Document-Level-Scripts.cfm

 

 

1 reply

Thom Parker
Community Expert
Community Expert
January 22, 2025

Communication between the browser host container and the PDF is through the HostContainer Object.  This object posts and receives messages to message handlers that are setup on both sides. See the reference:

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#hostcontainer-properties

 

However, as far as I know, this functionality hasn't been supported in many years, i.e., the brower side host container isn't supported. 

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
jeff_bissAuthor
Known Participant
January 22, 2025

So, the brower side host container is no longer supported. Great. 

 

It seems that I need to use a form. I assume that I can add text fields to my map PDF to which I can attach a:

  • mouse enter event to show a layer
  • mouse exit event to hide a layer
  • mouse click event to go to a detail page

I've read:

While neither discusses show/hide layers, JavaScript should provide that function with Text fields, correct? Or, do I need to use another form field?

Thom Parker
Community Expert
Community Expert
January 22, 2025

The HostContainer support was very brief. It was introduce in Acrobat 7. Shortly after this the internet started getting dangerous, and all kinds of things were getting hacked, and then support dissappeared. 

 

So, here's an article on scripting with layers:

https://acrobatusers.com/tutorials/create_use_layers/

 

And here's more info on scripting form fields:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often