Skip to main content
Known Participant
November 1, 2022
Question

Help adding custom toolbar icons to add-in tools

  • November 1, 2022
  • 2 replies
  • 5381 views

I have three simple javascript add-ins that I have used successfully for years. I would like to add custom icons to them, but am having problems. When I use this code, I get an error saying "this.importIcon is not a function". I am a cut-and-paste Javascripter so likely I'm making a simple mistake. Could anyone tell me what it is and how to fix it? Thank you!

 


this.importIcon("myIconCW", "/D/J/CAD Standards/Stamps/Acrobat tools/clockwise.jpg", 0);
this.importIcon("myIconCCW", "/D/J/CAD Standards/Stamps/Acrobat tools/counterclockwise.jpg", 0);
this.importIcon("myIconFlat", "/D/J/CAD Standards/Stamps/Acrobat tools/flatten.jpg", 0);

var cwIcon = util.iconStreamFromIcon(this.getIcon("myIconCW"));
var ccwIcon = util.iconStreamFromIcon(this.getIcon("myIconCCW"));
var flatIcon = util.iconStreamFromIcon(this.getIcon("myIconFlat"));

app.addToolButton({cName: "MyFlattenButton",
cLabel: "Flatten",
oIcon: flatIcon,
cEnable: "event.rc = (app.doc != null);",
cExec: "flattenPages();"
});


app.addToolButton({cName: "MyCWButton",
cLabel: "CW",
oIcon: cwIcon,
cEnable: "event.rc = (app.doc != null);",
cExec: "rotatepage(this.pageNum,90);"
});

app.addToolButton({cName: "MyCCWButton",
cLabel: "CCW",
oIcon: ccwIcon,
cEnable: "event.rc = (app.doc != null);",
cExec: "rotatepage(this.pageNum,270);"
});


function rotatepage(pageno,degrees) {
var rotateangle = this.getPageRotation(pageno);
var newangle = (rotateangle + degrees) % 360;
this.setPageRotations(pageno,pageno,newangle);
return 0;
}

This topic has been closed for replies.

2 replies

jteselleAuthor
Known Participant
November 2, 2022

Ok, I have changed the code to the following, which opens a blank doc and then tries to import the icons. But I get an error "GeneralError: Operation failed.
App.addToolButton:26:Folder-Level:App:newbuttons2.js"

when I open up a doc in Acrobat.

 

Here is the revised code:

 

var cwIcon;
var ccwIcon;
var flatIcon;

// Create a document
app.beginPriv();
var myDoc = app.newDoc();
// import icon (20x20 pixels) from the file specified
myDoc.importIcon("mycwIcon", "/D/J/CAD Standards/Stamps/Acrobat tools/2clockwise.jpg", 0);
myDoc.importIcon("myccwIcon", "/D/J/CAD Standards/Stamps/Acrobat tools/2counterclockwise.jpg", 0);
myDoc.importIcon("myflatIcon", "/D/J/CAD Standards/Stamps/Acrobat tools/2flatten.jpg", 0);
// convert the icon to a stream.
cwIcon = util.iconStreamFromIcon(myDoc.getIcon("mycwIcon"));
ccwIcon = util.iconStreamFromIcon(myDoc.getIcon("myccwIcon"));
flatIcon = util.iconStreamFromIcon(myDoc.getIcon("myflatIcon"));
// close the doc now that we have grabbed the icon stream
myDoc.closeDoc(true);
app.endPriv();

app.addToolButton({
cName: "MyFlattenButton",
cLabel: "Flatten",
oIcon: flatIcon,
cEnable: "event.rc = (app.doc != null);",
cExec: "flattenPages();"
});


app.addToolButton({
cName: "MyCWButton",
cLabel: "CW",
oIcon: cwIcon,
cEnable: "event.rc = (app.doc != null);",
cExec: "rotatepage(this.pageNum,90);"
});

app.addToolButton({
cName: "MyCCWButton",
cLabel: "CCW",
oIcon: ccwIcon,
cEnable: "event.rc = (app.doc != null);",
cExec: "rotatepage(this.pageNum,270);"
});


function rotatepage(pageno,degrees) {
var rotateangle = this.getPageRotation(pageno);
var newangle = (rotateangle + degrees) % 360;
this.setPageRotations(pageno,pageno,newangle);
return 0;
}

 

 

try67
Adobe Expert
November 2, 2022

Works fine for me... Did you make sure the file paths are correct?

jteselleAuthor
Known Participant
November 2, 2022

Line 26 is the last line of the "app.addToolButton" call. It means it failed on this function. Acrobat JS doen't report errors inside a single function call. It's the whole thing. 

 

Have you checked the actual icon size being returned from "util.iconStreamFromIcon"?  Although an excessive size shouldn't cause an error, it just doesn't draw the icon. I always hard code the icon data rather than relying on loading images, especially with Acrobat DC which destroys image quality. 

Here's your clockwise icon hard coded. 

 

 

var str2CWImgData = "fffffffffffffffffffffffffffefefefffdfffefffdfefefffefdfcfffbfafaffe7e7edfff8f9fefffffffffffdfdfcfffdfcf8fffffffbfffffffefffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffffefffffffdfefffffeffffff848792ff585c68ffc4c7d1fffcfefffffefffffffffffefffffffffffffffffffffffffffffffffffffffffffffffffffefefefffffffffffffffefffffffefffefffffffdfffffffcffffffe3e8f6ff687086ff0e1832ff1f2944ff6b738affd1d5e2fffdfffffffefffffffffffffffffffffffffffffffffefffffffefffffefefefffefefefffffffefffcfdfdfffcffffffe5e9f2ff7d8393ff30384fff17223fff182547ff172447ff131e3cff252b3dff7c7f88ffedeef1fffefefefffffffffffffffffffffefffffffefffffefefefffefefefffefffffffdffffffcbd0dcff42495cff141b37ff141d3dff172244ff182548ff182548ff111c38ff2d3444ff91949bfff1f2f4fffbfbfbfffffffffffffffffffffffffffffffffffefefefffdfefffffcffffffdae0e8ff384053ff141d36ff161d39ff303754ff464e6bff121c38ff19243eff626b80ffd3d8e3fffdfffffff9fafcfff9f9f9fffffffffffffffffffffffffffffffffffdfcfbfffcfdfffff3faffff606a79ff121d37ff16203aff4f5565ffd9dbe8ffa3a5b4ff3a3d4bffb1b5c0fff9fdfffffdfffffffcfdfffffefdfefffffefefffffffffffffffffffffffffffffffffffdfbfbfffdffffffc2cbd6ff1b283aff121f39ff374058ffdde1eafffdfeffffdfdee3ffe6e5eafffefffffffdfefffffdfdfdfffefefefffffffffffffffffffffffffffffffffffffffefffffffefffdfbfdfffdffffff848e9eff121f35ff131f37ff969eb1fffcfffffffdfdfcfffffffffffffffffffffefefffefdfcfffcfcfbfffefefdfffffffefffffffefffffffffffffffefffffffdfffffffcfffcfcfdfffbfeffff5b6578ff0f1c34ff222c43ffd3daeafffcfffffffffffcfffefefefffffffffffffffefffffefcfffffffefffdfdfcfffefefdfffdfdfdfffbfbfcfffefefefffdfdfbfffffffcfffcfffefff8fdffff4d586cff0f1b34ff2f374effe4e9f8fffcfffffffffffcfffefffffffdfefffffefffefffefffefffffffefffefefefffdfdfffffffefffffffefffffffdfffffefefdfffffffdfffcfffdfff9ffffff5a6579ff121d38ff242c44ffd4d9e9fffdfffffffefffdfffdfefefffefffffffefffefffdfffcfffffffefffcfcfefffcfdffffa8a9b2ffc1c1cbfff5f5fdfffdfdfefffffffefffafcf7fffcffffff808a9dff101b37ff151f3aff98a0b4fffcfffffffbfdfbfffcfefbfffcfefafffcfefafffcfefafffdfefbfffcfdffffe4e8f4ff303447ff1a1f35ff5f6274fff3f3f9fffffefefffcfbf8fffcfeffffc4cbdbff1d273fff15203bff39435bffd9deebfffcfffffffafcfbfffcfefcfffafdfbfffafdfdfffcfffffffafeffff7e8598ff172039ff131c35ff6e7387fffcfcfffffffcfcfffefbfdfffcfbfffff5f8ffff636b79ff121e37ff13203bff4f576cffd2d8e7fffcfffffffdfffffffcfffffffbfffffff1f8ffff8b94a5ff18253eff14213cff253045ffcad0ddfffffffffffefcf8fffdf9fefffbf9fdfffcfdffffdbe0e7ff353f52ff131f38ff151f38ff2b344aff767d8fffa9afbfffb2b9c9ff8e97a7ff495366ff142036ff16233dff152239ff8f98a7fffbfffffffefefefffffffbfffefefffffefefffffffffefffdffffffc8cfdaff3d4759ff121e36ff12203bff101d39ff13203cff12203cff111f3aff111f39ff121f36ff1d283bff8b93a0fff9fefffffefffffffefefdfffffefbfffbfefefffcfefdfffffffefffcfcfcfffcffffffdfe6f0ff778193ff2b374eff14213bff121e3bff101c39ff111e38ff1e2a40ff4f596affbac1ccfffbfffffffefffffffffffefffefefdfffdfdfcfff9fffcfff9fdfbfffefffffffefefffffcfcfdfffdfefffffcffffffe3e7f1ffb4b9c7ff9599a9ff9195a5ffa3a8b6ffcfd3ddfff5f8fefffefffffffbfcfcfffcfefdfffefffffffdfffefffefffffffbfffdfffbfffdfffdfefffffffffffffffffefffffffefffffffffffffffffffdfdfffffefefffffefefffffefefffffffefffffffffffffffffdfffffffcfffefffefffefffffffefffffffeffff";
var oImg2CWStream = {count:0, width:20, height:20, read:function(nBytes){return str2CWImgData.slice(this.count,this.count+=2*nBytes)}};

 

 

I have had the "addToolButton" function throw an error when a button name already exists. To fix this issue add this code before the call

 

app.removeToolButton("MyCWButton"); 

 

I would also suggest cutting the code down to only one button until you have it debugged. 

 

 


Good idea about cutting the code down. After trying that and messing about some, it turns out that the flatten icon is the one causing a problem for some reason. If I substitute one of the rotate icons for the flatten icon, all loads up ok and works. 

 

Now a different question, though -- is there any way to get the icons larger? The other toolbar icons are a usable size but I have a 4K monitor and these new 20x20 are very small. Perhaps after all this it will turn out that I'm better off with the generic "folder" icon that appears for the tools and just remembering which one goes where. Am I stuck with this small size?

 

Bernd Alheit
Adobe Expert
November 1, 2022

Have you opened a document?

jteselleAuthor
Known Participant
November 1, 2022

Yes, the error message occurs when I open a document.

try67
Adobe Expert
November 1, 2022

That's because the code executes before the document is opened, so the "this" keyword doesn't point to it at that point. You can't do it like that. Instead, you should either create a new blank document and use the reference to it to import the icons, or convert the icons to strings and embed that directly in your code.