Skip to main content
tim-b
Participating Frequently
September 11, 2018
Answered

Loaded image in Image field makes Text field visible + required

  • September 11, 2018
  • 1 reply
  • 1321 views

Hi,

I am creating form in Acrobat Pro to allows users to insert images via the Image Field function. Once an image is inserted, a text field below is required to be filled in.

I have created a javascript code for a similar scenario involving checkboxes. However, I have been unable to locate the correct terminology to reference the Image Field. Below is the Javascript code for the checkbox scenario.

event.target.buttonImportIcon();

var showHide = event.target.isBoxChecked(0)?display.visible:display.hidden;

this.getField("Photo1_Caption").display = showHide;

if(event.target.value == "Yes") {

// console.println("Checked " + event.value);

this.getField("Photo1_Caption").required = true;

}

else {

// console.println("Not " + event.value);

this.getField("Photo1_Caption").required = false;

}

Using the above code as a guide, is it possible for some assistance to achieve the following end outcome please:

  • If image is inserted into Image Field, the text field is made visible and required
  • If no image is inserted into Image Field, the text field remains hidden
  • If image is cleared from the Image Field, the text field becomes hidden and not required

Thank you kindly in advance for your help.

Tim

This topic has been closed for replies.
Correct answer George_Johnson

Here's what I had in mind for the Mouse Up script of the image field:

// Prompt the user to select an icon source

event.target.buttonImportIcon();

var oIcon;

// Get a reference to the image caption field

var f_Caption = getField("Photo1_Caption");

// Attempt to determine if there's a non-blank icon

try {

    // The next line will generate an error if there's no icon (image)

    oIcon = util.iconStreamFromIcon(event.target.buttonGetIcon());

 

    // Set caption field to visible and required

    f_Caption.display = display.visible;

    f_Caption.required = true;

 

} catch (e) {

    // It's a blank icon

    // so set caption field to hidden and not required

    // and clear the value

    f_Caption.display = display.hidden;

    f_Caption.required = false;

    f.value = "";

 

}

1 reply

Inspiring
September 11, 2018

One way to attempt to determine if an icon is blank is something like:

// Prompt the user to select an icon source

event.target.buttonImportIcon();

var oIcon;

try {

    oIcon = util.iconStreamFromIcon(event.target.buttonGetIcon());

} catch (e) {

    app.alert("Blank icon!");

}

The buttonImportIcon method has a return code that you can check as well. Once you think you know whether the icon is blank or not, it's a simple matter to control the text field.

tim-b
tim-bAuthor
Participating Frequently
September 11, 2018

Thanks George_Johnson​ for the reply. My modified code thanks to your input is below.

// Prompt the user to select an icon source

event.target.buttonImportIcon();

var oIcon;

try {

    oIcon = util.iconStreamFromIcon(event.target.buttonGetIcon());

} catch (e) {

    app.alert("Blank icon!");

}this.getField("Photo1_Caption").display = showHide;

if(event.target.value == "Yes") {

// console.println("Checked " + event.value);

this.getField("Photo1_Caption").required = true;

}

else {

// console.println("Not " + event.value);

this.getField("Photo1_Caption").required = false;

}

However, I am unsure of the correct values for the items shown in bold blue in the above code as they are for checkboxes. I have a very basic understanding of Javascript so apologies if I have missed an obvious step.

Thanks.

George_JohnsonCorrect answer
Inspiring
September 11, 2018

Here's what I had in mind for the Mouse Up script of the image field:

// Prompt the user to select an icon source

event.target.buttonImportIcon();

var oIcon;

// Get a reference to the image caption field

var f_Caption = getField("Photo1_Caption");

// Attempt to determine if there's a non-blank icon

try {

    // The next line will generate an error if there's no icon (image)

    oIcon = util.iconStreamFromIcon(event.target.buttonGetIcon());

 

    // Set caption field to visible and required

    f_Caption.display = display.visible;

    f_Caption.required = true;

 

} catch (e) {

    // It's a blank icon

    // so set caption field to hidden and not required

    // and clear the value

    f_Caption.display = display.hidden;

    f_Caption.required = false;

    f.value = "";

 

}