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

Hidden variables in JavaScript

Community Beginner ,
Jan 12, 2022 Jan 12, 2022

I'm trying to make a PDF form where the user inputs various measurements and the PDF calculates some results using JavaScript.

 

However, some of the intermediate values I don't want the user to see.

 

For example, they input a floor width and length in mm. It then calculates the area in m². Based on that, it calculates the number of tins of adhesive required and the number of rolls of vinyl.

 

I don't want them to see the area in m² though. Is there a way to keep that variable in the background in JavaScript. Or is the best way to add a text field with it in, but make it hidden?

 

If I was coding it for a website, I could declare a variable for area and never output it, only use it in calculattions. But I don't know how to do that in a PDF?

 

Thanks.

TOPICS
JavaScript
1.1K
Translate
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 12, 2022 Jan 12, 2022

Any local variable in JavaScript would be "local", and that does not show up in any of the fields visible by the user. So just declare a JavaScript variable and use that. This is standard JavaScript, and nothing specific to Adobe Acrobat or PDF forms. 

Translate
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 12, 2022 Jan 12, 2022

> If I was coding it for a website, I could declare a variable for area and never output it, only use it in calculattions. But I don't know how to do that in a PDF?

 

Exactly the same. The user only sees what you show them. The rest is "hidden" within the code.

For example:

 

var a = 5;
var b = 10;
var c = a*b;
app.alert(c);

 

You don't have to reveal the values of a and b in order to show c.

Translate
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 Beginner ,
Jan 12, 2022 Jan 12, 2022

Thanks. Where would I put that code though? I've tried putting it in the field properties Custom calculation script, but those variables don't seem to be global, and other fields can't access them.

Translate
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 12, 2022 Jan 12, 2022

No, they're not global. They are local to the location where you created them.

If you want variables that can be accessed from any script in the file you need to define them at the doc-level, via Tools - JavaScript - Document JavaScripts, or as true global variables, using the Global object (but that might require a change to the Preferences, as it also means they will be accessible to other files and scripts).

Translate
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 12, 2022 Jan 12, 2022
LATEST

If you want to maintian the intermediate value, then a good place to put it is a hidden form field. This makes the value persistent with the form. But if you don't need to be presistent, but globally avaiable, then add it to the document object. 

For example:

 

this.floorArea = ... calculation ...

Now "floorArea" can be seen and use by all scripts in the document. 

 

Another way to do this is to declare a variable in a document level script. Any variable in a doc script is automatically at document scope.   However, this data is lost when the document is closed. 

 

Here's an article on document scripts:

https://www.pdfscripting.com/public/Document-Level-Scripts.cfm

 

 

 

 

 

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

Translate
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