Search code examples
javascriptpdfhyperlinkdirectoryacrobat

Acrobat JavaScript: Directory Contents and Hyperlinking Files


I've recently been looking into automating a few things in Acrobat through JavaScript and had a couple questions that came up. I'm wondering if anyone might have thoughts or tips on these, it would be appreciated!

  1. Is it possible to either get the contents of a directory (so the list of documents can be stored as an array), or check for the existence of a file? From what I've gathered, this may not be something that JavaScript in Acrobat is able to accomplish due to lack of access to the file system.

Two tricks that I've seen suggested for checking to see if a file exists are to either import it as an attachment, or attempt to open the file and see what the result is. e.g. if (this.importDataObject("myFile", "/testfile.pdf/")) { This doesn't seem to work well for my situation as the files in question can be quite large, and importing or opening them can be slow.

  1. Is there a best way to use JavaScript to set a hyperlink in a pdf to open a file? I've tried l.setAction("app.openDoc(\"testfile.pdf\")") which adds the quoted JavaScript to the hyperlink and uses it to open the file in a new window, but I'm wondering how this compares with the way Acrobat normally links to a file, or if there's another more optimal way to set this programmatically.

Solution

  • For question #1 - Short answer is "No". As you said, Acrobat JavaScript doesn't have access to the file system. Long answer is "Yes - depending on what you want to do". Acrobat has a way to run "Actions" that can process all the files in a given directory, a list of files, or all the files that are currently open. You can run JavaScript on each file in a directory using an action without that script needing to actually know the file name.

    For Question #2. The only option you have to set a link action via JavaScript is to set the JavaScript action. If you create a link, you only get to set the mouse up action. However, you can add a button instead of a link where the background and border are transparent and there's no caption and you get something that visually looks like a link but you can set more actions like mouse up, down, and rollover (enter/exit).