Skip to main content
Inspiring
December 26, 2017
Answered

Creating add-ons toolbar button with custom icon

  • December 26, 2017
  • 4 replies
  • 9913 views

I have a folder level JavaScript that is running when Acrobat starts up. I'm able to add a button to the add-ons toolbar without a custom icon but once I try to specify an icon I get an error that doesn't tell me much.

GeneralError: Operation failed.

App.addToolButton:1824:Folder-Level:App:config.js

Here is my code:

var myIcon;

var importIcons = app.trustedFunction(function() {

     app.beginPriv();

     // Create a document

     var myDoc = app.newDoc();

     // import icon (20x20 pixels) from the file specified

     myDoc.importIcon("myIcon", "/some/path/plus.jpg", 0);

   

     // convert the icon to a stream.

     myIcon = util.iconStreamFromIcon(myDoc.getIcon("myIcon"));

     // close the doc now that we have grabbed the icon stream

     myDoc.closeDoc(true);

     app.endPriv();

});

importIcons();

// add buttons to add-ons toolbar

app.addToolButton({

     cLabel: "Test1",

     cName: "testButton1",

     oIcon: myIcon,

     cExec: "app.alert('hello');",

     cTooltext: "Test1",

     cEnable: true,

     cMarked: true,

     nPos: 0

});

My image is 20x20 px and a .jpg. I'm also wondering if there's a way to accomplish this without having to create a dummy empty document just in order to call importIcon() and getIcon(). Any help is appreciated.

This topic has been closed for replies.
Correct answer cschmelter

I warned you, didn't I?

Actually, I don't think the issue is with the image size. It's with the format.

I had encountered similar issues when I messed around with these icons and finally I had to write a Java application that converts PNG images to an ARGB hex-string. But they come out rotated and flipped, so I have to rotate and flip them the other way in advance for them to come out correctly. It's a total nightmare...

Here's the hex-string I generated for your image:

"FFFFFFFFFFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFFFFFFFFFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFFFFFFFFFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFA30000FFFFFFFF"


Your hex string worked fine for me. I'm able to generate valid hex strings that display the icon correctly on Windows but not on Mac. I'm just going to assume this is a Mac bug and generate my hex strings on Windows and paste into Mac. Thanks for the help.

4 replies

JR Boulay
Community Expert
Community Expert
December 30, 2017

Here is the source JS file: https://adobe.ly/2q1y84S

of this free tool: https://www.abracadabrapdf.net/?p=964

which use one of above icons.

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
Community Expert
December 29, 2017

Here is a free PDF file that convert GIF, PNG, JPG… images to a usable format to embed into JavaScripts : Fichiers partagés - Acrobat.com

Acrobate du PDF, InDesigner et Photoshopographe
Inspiring
December 29, 2017

I tried it out on a couple different .png files and a .jpg file and the icon still doesn't display correctly. I'm assuming there's something wrong with the image format I'm starting with but I'm not much of an image expert. Any way you can send me an image you've used with your tool that displays correctly so I can compare the specs to my images?

JR Boulay
Community Expert
Community Expert
December 29, 2017
Acrobate du PDF, InDesigner et Photoshopographe
Inspiring
December 26, 2017

It doesn't seem like it's a file path issue because when I intentionally try a file path that doesn't exist I get an error back from the iconStreamFromIcon() function:

Util.iconStreamFromIcon:1801:Folder-Level:App:config.js

and when I put in a valid path I don't get an error back from iconStreamFromIcon().

Thom Parker
Community Expert
Community Expert
December 27, 2017

You'll find a tutorial and a tool for creating toolbar Icons here:

Alerts, Popups, and Other Devices for Interacting with the User

You'll also find lots of folder level automation tools that use toolbar buttons with icons.  If you look at them you'll notice not a single one depends on an external file. Loading files at startup is iffy, and create dependencies that add difficulty to maintaining and transferring scripts.

Here are some free examples of tools with toolbar buttons:

Free Acrobat Automation Tools

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
December 27, 2017

Thanks for the reply Thom, but unfortunately I don't have a subscription so can't see the examples.

try67
Community Expert
Community Expert
December 26, 2017

Are you adding your code to the built-in config.js file? If so, that's a bad idea. You should create your own js file.

Beside that I'm not seeing any major issues. I tried your code on my machine and it worked fine...

try67
Community Expert
Community Expert
December 26, 2017

PS. Short of converting your image to a Hex string, which is super-complicated and annoying, your solution is the easiest one I've came across so far.