How to create custom event when a user clicks on a hyperlink in a PDF file.
Copy link to clipboard
Copied
I'm new to Acrobat customizations. In our environment, we receive a lot of PDF files created externally that contain relative hyperlinks between documents. For a variety of reasons, we would like to perform a custom action when a user clicks on one of the hyerlinks. I've investigated creating a Javascript plugin but haven't gotten it to work. A code generator provided the below, which I placed into C:\Program Files\Adobe\Acrobat DC\Acrobat\Javascripts but it produces a "TypeError: this.addEvent is not a function" error.
Is the custom behavior on hyperlink click even possible to do in Javascript?
Thx.
// Filename: hyperlink_action.js
// Define the custom action to be performed when a hyperlink is clicked.
function customHyperlinkAction(event) {
// Get the URL of the clicked hyperlink.
var url = event.target.URL;
// Perform your custom action based on the URL.
// Examples:
// 1. Open the URL in a new window/tab (default behavior, but included for clarity):
// app.openURL(url);
// 2. Display an alert box with the URL:
app.alert("Clicked hyperlink: " + url);
// 3. Check if the URL matches a specific pattern and perform a different action:
if (url.startsWith("https://www.example.com")) {
app.alert("This link goes to Example.com!");
// You could perform a more complex action here, like opening a specific file
// or executing a different JavaScript function.
} else if (url.startsWith("internal://")) { // Example of a custom "internal" protocol
// Handle "internal" links differently. For example:
var internalPath = url.substring("internal://".length);
app.alert("Handling internal link: " + internalPath);
// You might have a way to process these, like looking up data based on the path.
} else {
// Default action: open the URL
app.alert("I am here!");
//app.openURL(url);
}
// 4. Modify form fields based on the URL (more advanced):
// if (url.includes("data_id=123")) {
// var field = this.getField("FieldName");
// if (field) {
// field.value = "New Value based on URL";
// }
// }
// 5. Log the click to the console (useful for debugging):
console.println("Hyperlink clicked: " + url);
}
// Add a document-level event listener for hyperlink clicks.
// This is the key part that makes the extension work. It intercepts
// hyperlink clicks before they are handled by the default behavior.
this.addEvent("HyperlinkClicked", customHyperlinkAction);
// Optional: Add a menu item to test the functionality (for development/testing).
// This is not necessary for the extension to work; the event listener above is what does the work.
var menuItem = {
cName: "Test Hyperlink Action", // Menu item name
cParent: "Tools", // Add it to the "Tools" menu
cExec: "console.println('Test menu item clicked.');" // Simple test action
};
app.addMenuItem(menuItem);
// Optional: Code to be executed when the extension is loaded.
console.println("Hyperlink Action Extension Loaded.");
Copy link to clipboard
Copied
addEvent is not a JavaScript function in Acrobat.
Copy link to clipboard
Copied
By investigating, do you mean that you read through the Acrobat JavaScript Reference and Programming Guide?
If not, then you should do so.
If the relative document URLs are inside existing links objects on the PDFs, then they are not accesible from a Script.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Not possible using JavaScript.

