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

buttonGetIcon return value

New Here ,
Sep 11, 2020 Sep 11, 2020

Copy link to clipboard

Copied

I have sifted thru posts, but can't find a solution ...

I want to execute a script from a button that will evaluate many image fields to determine if an image is assigned to the field.  The purpose is to clear a hidden text field of the original path of the imported icon in each. I have tried:

var isIcon = this.getField("Image17_af_image").value;

which returns "null"

var isIcon = this.getField("Image17_af_image").buttonGetIcon();

which returns [object Icon]

var isIcon = util.iconStreamFromIcon(this.getField("Image17_af_image").buttonGetIcon());

which returns [object Stream]

Is it possible to get a "Yes"/"No" response?

 

TOPICS
Acrobat SDK and JavaScript

Views

817

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
LEGEND ,
Sep 11, 2020 Sep 11, 2020

Copy link to clipboard

Copied

Maybe the width and height of the icon object will give interesting info. Maybe not; perhaps there is always some kind of image assigned, even if it is blank.

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

Copy link to clipboard

Copied

You can do it like this:

 

var buttonIcon = this.getField("Button3").buttonGetIcon();
try {
	var buttonIconStream = util.iconStreamFromIcon(buttonIcon);
	var streamLength = buttonIconStream.width*buttonIconStream.height;
	app.alert("Icon size: " + streamLength);
} catch (e) {
	app.alert("No icon found.");
}

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
New Here ,
Sep 11, 2020 Sep 11, 2020

Copy link to clipboard

Copied

Thanks try67! I'll be looking at "try" functionality.  Expect it will be very useful.

Code performing as needed:

var i = 0
for (var i=16;i<=112;i++)
if(i<70 || i>71){
{
s=i+"";
}
var buttonIcon = this.getField("Image"+s+"_af_image").buttonGetIcon();
try {
var buttonIconStream = util.iconStreamFromIcon(buttonIcon);
var streamLength = buttonIconStream.width*buttonIconStream.height;
// app.alert(streamLength);
}
catch (e) {
getField("FilePost"+s).value="";
getField("Text"+s).value="";
// app.alert("No icon found.");
}
}

 

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

Copy link to clipboard

Copied

You can comment out the streamLength variable, too.

The try-catch clause is used to catch run-time errors and handle them properly (instead of just existing the code immediately). The iconStreamFromIcon method throws such an error when you use it on an empty Icon object.

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
New Here ,
Sep 11, 2020 Sep 11, 2020

Copy link to clipboard

Copied

THX!  You're an awesome resource.  The streamLength components are good to know anyway.  Added to my personal toolkit.

Can you send me a link to try-catch doc and the return codes for catch?

 

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

Copy link to clipboard

Copied

try-catch is explained here: https://www.w3schools.com/js/js_errors.asp

 

The documentation of the iconStreamFromIcon method doesn't actually mention that it throws an error when the Icon object is empty. I found that out myself and then used that information to catch the error and handle it. This is the error message you get without the try-catch clause:

 

RaiseError: Expected a dict object.
Util.iconStreamFromIcon:2:Console undefined:Exec
===> Expected a dict object.

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
New Here ,
Sep 11, 2020 Sep 11, 2020

Copy link to clipboard

Copied

Thanks again try67.  Will be reviewing the doc.

Nothing beats the significant hands-on you have.

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
New Here ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

I'm using the following script to set any one of over 100 images I have on a form. 

** Thanks try67! **

 

I want to conditionally "clear image" instead of assigning an image, without using a blank file.  Is this do-able?

 

s = event.target.name;
s1 = s.substr(5,s.length-14);
FP = "FilePost"+s1
//app.alert(FP);

var cRtn = app.response (
{
cQuestion:"Paste the Image File Name",
cTitle:"Assign Image",
//bPassword:true,
//cDefault: global.cLastPswd,
cLabel:"File Name:"});
if(cRtn && cRtn.length) {
}
else app.alert("File name invalid or not present");

var strDIPath = "/" + cRtn.replace(/\:?\\/g,"/");
var strDIPath = strDIPath.replace(/"/g,"");

var cRtn = strDIPath;

event.target.buttonImportIcon(cRtn);
getField(FP).value=cRtn;

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 ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

No, a script can't clear an field's image, unfortunately.

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
New Here ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

LATEST

Sounds to me like an easy hole to plug.  Oh well.  Thanks!

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