Skip to main content
Known Participant
November 1, 2022
Question

Help adding custom toolbar icons to add-in tools

  • November 1, 2022
  • 2 replies
  • 5391 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
Community Expert
Community Expert
November 2, 2022

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

jteselleAuthor
Known Participant
November 2, 2022

Yes, paths are correct.

 

I saw in a post somewhere that Acrobat can be picky about the format of the icon file. They are 20x20 JPG files, downsampled in Corel PhotoPaint from 50x50 and saved at 100% quality. Is there anything else I should check? I have attached the files here in case anyone has something to check them.

 

It seems from the error message, though, that the error is occurring when the "App.addToolButton" function is called. Is that correct? That would be after the icon files have loaded, right? What could be the problem there?

Bernd Alheit
Community Expert
Community 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
Community Expert
Community 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.