• Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
    Dedicated community for Japanese speakers
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
    Dedicated community for Korean speakers
Exit
1

(If statement) to insert image based on a value.

Explorer ,
Jun 18, 2020 Jun 18, 2020

Copy link to clipboard

Copied

I am using AutoMailMerge and I am trying to add a JavaScript to check if a field value is "1"  then insert image in the image field. It's working with this code, but it doesn't do the else if conditions, all exported/processed PDFs contains the first image only (Circle1.png).

 

This is the code:

var f = this.getField("Image1_af_image");
if (f.value = 1)
{
   f.buttonImportIcon("C:/Users/EslamSamy/Desktop/Circle1.png");
} else if (f.value = 2) 
{
  f.buttonImportIcon("C:/Users/EslamSamy/Desktop/Circle2.png");
} else if (f.value = 3) 
{
  f.buttonImportIcon("C:/Users/EslamSamy/Desktop/Circle3.png");
}

 

TOPICS
Acrobat SDK and JavaScript

Views

639

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

correct answers 1 Correct answer

Community Expert , Jun 18, 2020 Jun 18, 2020

Hi,

 

I am confused as to how f which points to a field which contains the numbers 1,2, or 3, and then f is also being used for the buttonImportIcon.

 

In the forms you have posted the number is contianed in a field called "Number", therefore shouldn't the code be something like :

 

var f = this.getField("Image1_af_image");
var n = this.getField("Number");
if (n.value == "1")
{
f.buttonImportIcon("C:/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle1.png");
} else if (n.value == "2") 
{
...

Votes

Translate

Translate
Community Expert ,
Jun 18, 2020 Jun 18, 2020

Copy link to clipboard

Copied

Hi,

 

You need to add a second '=' to your if statements

= - means set the value to

== - means compare

 

so the if becomes

var f = this.getField("Image1_af_image");
if (f.value == 1)
{
   f.buttonImportIcon("C:/Users/EslamSamy/Desktop/Circle1.png");
} else if (f.value == 2) 
{
  f.buttonImportIcon("C:/Users/EslamSamy/Desktop/Circle2.png");
} else if (f.value == 3) 
{
  f.buttonImportIcon("C:/Users/EslamSamy/Desktop/Circle3.png");
}

 

Hope this helps

 

Malcolm

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
Explorer ,
Jun 18, 2020 Jun 18, 2020

Copy link to clipboard

Copied

I am sorry! but it didn't work, also it didn't change the first image like before.

 

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 ,
Jun 18, 2020 Jun 18, 2020

Copy link to clipboard

Copied

Hi,

 

I won't change anything unless the value of "Image1_af_image" is the number 1, 2, or 3.  Is that field a drop down or list which has these values specified, or is the user typing them in?

 

Have you checked the console for an error?

 

If you can share the document or post it so we can see then we can help more, all I did was correct you JavaScript syntax.

 

Regards

 

Malcolm

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
Explorer ,
Jun 18, 2020 Jun 18, 2020

Copy link to clipboard

Copied

Okay I chagned the code to this: But it deosn't insert any image to the filed.
For your queastion I get the filed number from a excel sheet using AutoMailMerge 

var f = this.getField("Image1_af_image");
if (f.value == "1")
{
f.buttonImportIcon("C:/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle1.png");
} else if (f.value == "2") 
{
f.buttonImportIcon("C:/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle2.png");
}


This link contains 3 processed files with this above code with 3 diffrent values for "Image1_af_image".
https://gofile.io/d/qMvnfu 

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 ,
Jun 18, 2020 Jun 18, 2020

Copy link to clipboard

Copied

Hi,

 

I am confused as to how f which points to a field which contains the numbers 1,2, or 3, and then f is also being used for the buttonImportIcon.

 

In the forms you have posted the number is contianed in a field called "Number", therefore shouldn't the code be something like :

 

var f = this.getField("Image1_af_image");
var n = this.getField("Number");
if (n.value == "1")
{
f.buttonImportIcon("C:/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle1.png");
} else if (n.value == "2") 
{
f.buttonImportIcon("C:/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle2.png");
}

 

Hope this helps

 

Malcolm

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
Explorer ,
Jun 18, 2020 Jun 18, 2020

Copy link to clipboard

Copied

You are the Best : ) Thank you so so much!

 

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 ,
Jun 18, 2020 Jun 18, 2020

Copy link to clipboard

Copied

LATEST

You have to use the correct file-syntax. So instead of this:

f.buttonImportIcon("C:/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle2.png");

Use:

f.buttonImportIcon("/C/Users/EslamSamy/Desktop/MEWA Project/Files/Circles/Circle2.png");

 

Be aware that using this method with the file-path requires a trusted context, though.

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