Copy link to clipboard
Copied
Is this possible? I'm trying to do this using a setAction but am getting: General Error: Operation Failed. I pasted the function below in a hidden calculation field.
My setAction: linkField.setAction("SubmitForm", "URL=" + link);
Ideas?
function checkLinks() {
for (var i = 1; i <= 30; i++) {
var linkField = this.getField("Link" + i);
var link = linkField.valueAsString;
var supplierField = this.getField("Supp" + i);
if (link.contains("x")) {
supplierField.value = "X";
} else if (link.contains("y")) {
supplierField.value = "Y";
} else if (link.contains("z")) {
supplierField.value = "Z";
} else if (link === "") {
supplierField.value = "";
} else {
supplierField.value = "Unknown Supplier";
}
linkField.setAction("SubmitForm", "URL=" + link)
}
}
Copy link to clipboard
Copied
This code goes under the category of " you can't just make things up". I think you'll find it helpful to look through the Acrobat JavaScript Reference.
https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#getitemat
So, by "link field", do you mean a link annotation object? or a button field?
Either way, while they are acquired in different ways and are completely different animals, both do have a "setAction" function. This function applies a script to the Button or Link. And again, this script must conform to the rules of Acrobat JavaScript.
It is also a bad practice to apply form field scripts at runtime. A better strategy would be to place your script on the Link/Button. The script would then use the same test as you've shown above to navigate to the correct URL.
Copy link to clipboard
Copied
Just to add some more context.
It looks as if you are under the impression that "setAction" means to set one of the items that can be manually selected from the Actions pulldown on the properties dialog for a link or form field. This kind of makes sense, and it would be wonderful if this were the case. But alas, Adobe loves to confuse users by reusing the same terms for completely different things. For example batch sequences are also called Actions. When dealing with Acrobat it is best to forget everything you think you know, and read the documentation.
Copy link to clipboard
Copied
This is all good advice, thank you.
The "link fields", or "Link1" through "Link30" are textboxes, wherein users paste URLs (not my idea :)). The setAction solution was a bit of a hailmary. I turned to the Reference after my first ideas didn't work, such as: linkField.innerHTML = `<a href="${link}" target="_blank">Link</a>`. Surely there is a way to convert the 'pasted link text' to a hyperlink, though, no?
By applying formfield scripts at runtime, do you mean the hidden calculation field? If so, good to know that is bad practice.
Copy link to clipboard
Copied
The Acrobat DOM (Document Object Model) is completely different than the HTML DOM. This is exactly what I meant about "forget everything you think you know". There is litterally zero relationship between the two. A PDF's page content structure is not dynamic, and it's not visible to the JavaScript Model.
What you can do is use the "app.launchURL()" function to open the URL programatically from a script in the link/button.
Copy link to clipboard
Copied
What I meant by setting scripts at runtime is using the "setAction" function. In Acrobat, it is a good practice to set all event scripts at design time for a number of reasons that are mostly related to document usability. The functions like the setAction are intended for automation scripting at the application level. There are of course execptions.
Like this game:https://www.pdfscripting.com/public/FreeStuff/PDFSamples/TheFlyv3_EN4Rdr.pdf