javascriptadobeacrobatadobe-javascript

Import image in Acrobat using JavaScript (preferred on document-level)


I am going to implement a dynamic legend using JavaScript in Adobe Acrobat.

The document contains a lot of layers. Every layer has an own legend. The origin idea is to implement the legend so, that it contains the images in a dialog box for the visible layers.

I can only hide/show the layers by setting state to false or true (this.getOCGs()[i].state = false;) on document-level.

Question 1: Can I extract data from layer somehow for legend establishing? I think no, as we only have these function on layers: getIntent(), setIntent() and setAction(). Right? Therefore I decided to arrange it so, that all needed icons for every layer are saved in a folder with corresponding names. JavaScript should import the icons and I build the a dialog window with icons of visible Layers and place a text(description for this icon).

I tried all possibilities of image import described here: http://pubhelper.blogspot.com.au/2012/07/astuces-toolbar-icons-et-javascript.html. I got only one way (Convert the icons as hexadecimal strings). This way isn't good, as it is too much work to create with an other tool a hexadecimal string from a images and place it into a javascript code. Unfortunately, I cannot import image using other methods:(. Since the security settings in Adobe are changed after version 7 or so, it is not possible to use functions like app.newDoc, app.openDoc, even app.getPath On document-level. I decided to implement the import on a folder level using trusted functions like this:

Variant 1:

var importImg = app.trustedFunction(function() {
app.beginPriv();
var myDoc = app.newDoc({
    nWidth: 20,
    nHeight: 20
});

var img = myDoc.importIcon("icon", "/icon.png", 0);
app.endPriv();
return img;   });  
var oIcon = importImg();

The settings in Preferences->JavaScript-> JavaScript Security are disabled (Enable menu item JS execution privileges, enable global object security policy) NotAllowedError: Security settings prevent access to this property or method. App.newDoc:109:Folder-Level:User:acrobat.js

Variant 2:

var importImg = app.trustedFunction(function() {
var appPath = var phPath = app.getPath({
    cCategory: "user",
    cFolder: "javascript"
});

try {

    app.beginPriv();
    var doc = app.openDoc({
        cPath: phPath + "/icon.png",
        bHidden: true
    });
     app.endPriv();
} catch (e) {
    console.println("Could not open icon file: " + e);
    return;
}

    var oIcon = util.iconStreamFromIcon(doc.getIcon("icon"));
return oIcon;});
var oIcon = importImg();

Could not open icon file: NotAllowedError: Security settings prevent access to this property or method.

At least it allows the execution of all these functions like app.newDoc, but in the second variant it says, wrong range of content or so. Maybe is here the pdf from an image created false? I just took the image and printed it into a pdf.

I tried all these possibilities with .jpg, .png, .pdf. with different sizes(big images and 20x20), It doesn't work.

Could somebody help me, as I spent a lot of time with trying different possibilities. It would be actually better to implement the main goal described above on document level, is it possible?

Thank you and kind regards, Alex


Solution

  • Do you have the Console fully activated in Acrobat? If not, do so and look for error messages you get.

    The first variant does not work, because myDoc is not defined (unless you have done that before you call the importImg function).

    If you want to import the image into the newly created file, you will have to make a reference to the Document Object you create with newDoc(). Actually, that would make the link to myDoc, as in

    var myDoc = app.newDoc(1,1)
    

    (are you sure you want to create a document measuring 1x1 pt?)

    The next issue with the first variant is a bug in Acrobat, which discards "floating" Icon Objects when saving the document; you'd have to attach the Icon Object to a field to keep it; this field can be hidden, or even on a hidden Template page in the document.