Highlighted

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

Explorer ,
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");
}

 

Adobe Community Professional
Correct answer by BarlaeDC | Adobe Community Professional

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

Topics

Acrobat SDK and JavaScript, PDF forms

Views

160

Likes

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

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

Explorer ,
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");
}

 

Adobe Community Professional
Correct answer by BarlaeDC | Adobe Community Professional

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

Topics

Acrobat SDK and JavaScript, PDF forms

Views

161

Likes

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
Adobe Community Professional ,
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

Likes

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
Reply
Loading...
Explorer ,
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.

 

Likes

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
Reply
Loading...
Adobe Community Professional ,
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

Likes

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
Reply
Loading...
Explorer ,
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 

Likes

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
Reply
Loading...
Adobe Community Professional ,
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

Likes

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

Copy link to clipboard

Copied

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

 

Likes

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
Reply
Loading...
try67 LATEST
Most Valuable Participant ,
Jun 18, 2020

Copy link to clipboard

Copied

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.

Likes

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
Reply
Loading...
Resources
One Stop Solution for Acrobat
Edit a PDF