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

Looking for some Javascript help with buttons

Community Beginner ,
May 10, 2020 May 10, 2020

Using Adobe Acrobat X Pro, I would like the user to be able to click repeatedly on a single button  in order to turn on and off the visibility of different objects or buttons, and the following cycle to happen:

 

  • With the first click, button one becomes visible.
  • With the second click, button two also becomes visible.
  • With the third click, both buttons are hidden.
  • With the fourth click, button one becomes visible.
  • With the fifth click, button two also becomes visible.
  • With the sixth click, both buttons are hidden…
  • …and the cycle continues.

 

I have the code (below) that nearly does the trick, but I don’t want colours changing and would like to amend the code to replace the colour coding with a variable.  However I cannot get this to work, as I don’t really know the syntax for setting global variables.  E.g. I want to set a “behind the scenes” variable such as A, B, C that changes to the next letter on each click (with C going to A).  This variable can then be used to go through the above cycle accordingly.

 

[background:  there are more states than 3, but simplified for purposes of posting.  Also this will need to be done in around 100 places, which is why I want a neater “variable” solution than changing colours!]

 

if (color.equal(event.target.fillColor, color.red))

{button1.display = display.visible;

event.target.fillColor = color.yellow;}

 

else if (color.equal(event.target.fillColor, color.yellow))

{button2.display = display.visible;

event.target.fillColor = color.green;}

 

else {event.target.fillColor = color.red;

button1.display = display.hidden;

button2.display = display.hidden;}

TOPICS
PDF forms
1.4K
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
1 ACCEPTED SOLUTION
Community Expert ,
May 13, 2020 May 13, 2020

The trick to this type of scripting is to name buttons with a scheme that allows a scirpt to automatically identify all the related fields from the field where it is called. So the exact same script will work everywhere.

You can see a tutorial that explains the importance of field naming for writting scripts here:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

One way to do this is with group naming, like this. 

 

"Group1.Button1" "Group1.Button2" etc. 

 

Now when a button is pressed, it can get the names of all the related buttons from it's own name. 

 

var prefix = event.targetName.split(".").shift();

var button1 = this.getField(prefix + ".Button1");

 

Next, it is possibe to write a script to automatically create all the fields with "doc.addField()"

Here's the reference entry.

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_Acro...

 

 

 

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

View solution in original post

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 ,
May 10, 2020 May 10, 2020

On every click check the state of the two buttons. When both buttons are hidden show button one and so on.

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 ,
May 10, 2020 May 10, 2020

Can you provide some example code? Bit of a javascript novice here!

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 ,
May 10, 2020 May 10, 2020

Thanks for your help so far, I am trying to check the state of the button but can't find the right code.  This is an example of the type of code I'm trying (simplified to cycle Button7 on and off):

var button7 = getField("Button7");

if (button7.presence = "visible")
button7.display = display.hidden;

else if (button7.presence = "invisible")
button7.display = display.visible;

 

Also tried other variations, such as:

...if(button7.visible = "visible")...

...if(button7.visible = "true")...

...if(button7.visible=TRUE)...

...etc.

 

I just don't know the syntax - help appreciated!

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 ,
May 11, 2020 May 11, 2020

Use something like this:

var button7 = getField("Button7");
if (button7.display == display.visible)
  button7.display = display.hidden;
else if (button7.display == display.hidden)
  button7.display = display.visible;
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 ,
May 12, 2020 May 12, 2020

Thanks for your help – this works great now! 

 

I’ve used the code below in one “control” button, which effectively turns visible a set of 5 other buttons, in a continuous cycle.

 

My next problem is that I need this repeated for around 100 “control” buttons, so I need to set up 600 buttons, and repeat (a variation of) the code in the 100 “control” buttons.

 

So I envisage the steps required are that I need to uniquely name all 600 buttons, and adapt the code in each of the 100 “control” buttons in order to refer to these unique names.  Do you know of a quick or automated way of doing this?  Or will I need to manually name all of these buttons, and manually adapt the code 100 times?

 

[PS. This is my first piece of javascript coding, so if you have any tips to streamline or tidy the code please let me know!]

 

var button1 = getField("Button1");

var button2 = getField("Button2");

var button3 = getField("Button3");

var button4 = getField("Button4");

var button5 = getField("Button5");

 

if (button5.display == display.visible)

    {button1.display = display.hidden;

    button2.display = display.hidden;

    button3.display = display.hidden;

    button4.display = display.hidden;

    button5.display = display.hidden;}

 

else if (button4.display == display.visible)

    {button5.display = display.visible;}

 

else if (button3.display == display.visible)

    {button4.display = display.visible;}

 

else if (button2.display == display.visible)

    {button3.display = display.visible;}

 

else if (button1.display == display.visible)

    {button2.display = display.visible;}

 

else

    {button1.display = display.visible;}

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 ,
May 13, 2020 May 13, 2020

The trick to this type of scripting is to name buttons with a scheme that allows a scirpt to automatically identify all the related fields from the field where it is called. So the exact same script will work everywhere.

You can see a tutorial that explains the importance of field naming for writting scripts here:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

One way to do this is with group naming, like this. 

 

"Group1.Button1" "Group1.Button2" etc. 

 

Now when a button is pressed, it can get the names of all the related buttons from it's own name. 

 

var prefix = event.targetName.split(".").shift();

var button1 = this.getField(prefix + ".Button1");

 

Next, it is possibe to write a script to automatically create all the fields with "doc.addField()"

Here's the reference entry.

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_Acro...

 

 

 

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
Community Beginner ,
May 17, 2020 May 17, 2020
LATEST

Amazing!! Thank you so much for your help!! You've saved me hours of manually amending names!!

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