• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Participant ,
Jan 03, 2023 Jan 03, 2023

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

 

 

TOPICS
JavaScript , PDF forms

Views

541

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2023 Jan 03, 2023

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. 

 

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2023 Jan 03, 2023

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. 

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 03, 2023 Jan 03, 2023

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2023 Jan 03, 2023

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. 

 

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2023 Jan 03, 2023

Copy link to clipboard

Copied

LATEST

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

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines