Skip to main content
Known Participant
January 3, 2023
Question

Convert pasted url into: 1. clickable link; that 2. displays as "Link"

  • January 3, 2023
  • 2 replies
  • 1069 views

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)
}
  }

 

 

This topic has been closed for replies.

2 replies

Thom Parker
Community Expert
Community Expert
January 3, 2023

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. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
dankpoochAuthor
Known Participant
January 3, 2023

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.

Thom Parker
Community Expert
Community Expert
January 3, 2023

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. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Thom Parker
Community Expert
Community Expert
January 3, 2023

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. 

 

 

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