6 Replies Latest reply on Aug 17, 2014 7:29 AM by Alexito

    import image

    Alexito

      Hello,

       

      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 the 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();


      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();

       

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

      The settings in Preferences->JavaScript-> JavaScript Security are disabled (Enable menu item JS execution privileges, enable global object security policy).

      Question 2: Is it not allowed or should I change some other settings or use the import on any other way?

       

      I tried all these possibilities with .jpg, .png, .pdf. with different sizes(big images and 20x20 pxls), 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, are there other possibilities to access images, maybe using xml or something else) (Question 3)?

       

      Thank you and kind regards,

      Alex

        • 1. Re: import image
          try67 MVP & Adobe Community Professional

          I tried the first code and it worked fine, in the sense that it generated a

          new file, but it couldn't import the image because the file path you

          specified is partial. You need to specify the full file path using the

          right syntax. Also, the third parameter of that method should only be used

          when importing a PDF file.

          • 2. Re: import image
            Alexito Level 1

            Thanks!
            It was no problem with the path, as I change it before posting. I printed out and checked every time.

            I installed Acrobat and I works now. Before I tried in Reader.

             

            Ok, when this code works, why the similar code doest not work on document level?

             

            function importImg() {

                var shortPath = "/Macintosh HD/tmpAcrobat/";

                console.println(shortPath + "icon.png");

                var img = this.importIcon("icon", shortPath + "ico.png",0); // I tried with different types of imgs: png, jpg, pdf and sizes : 16x16, 20x20, without the third parameter as well.

                oIcon = util.iconStreamFromIcon(this.getIcon("icon"));

                console.println(oIcon.width); // returns the icon's width

                console.println(oIcon.height); // return the icon's height

                return oIcon;

            };

            console.println("call importImg");

            var oIcon = importImg();

             

            Error:

            RangeError: Invalid argument value.

            Doc.importIcon:5:Document-Level:init

             

            Do you have any idea, how to get it work?

            • 3. Re: import image
              try67 MVP & Adobe Community Professional

              It seems I was wrong about the path. You can specify a relative one, but you can't specify it at all from a doc-level script, only from a trusted context.

              Also, you should remove the nPage parameter, as I've said earlier.

              • 4. Re: import image
                Alexito Level 1

                I removed the nPage parameter, thanks! So it looks now like this:

                 

                function importImg(targetImg) {

                    var shortPath = "/Macintosh HD/Users/tmpAcrobat/";

                    console.println(shortPath + "icon.jpeg");

                    var img = this.importIcon("icon", shortPath + "icon.png");

                    oIcon = util.iconStreamFromIcon(this.getIcon("icon"));

                    console.println(oIcon.width); // returns the icon's width

                    console.println(oIcon.height); // return the icon's height

                    return oIcon;

                };

                var oIcon = importImg("Button1");

                 

                >> You can specify a relative one, but you can't specify it at all from a doc-level script, only from a trusted context.


                What do you mean here, that it is not allowed to specify the path like this "/Macintosh HD/Users/tmpAcrobat/icon.png" ? Are there no possibilities to specify the path from a trusted content from a doc-level?

                • 5. Re: import image
                  try67 MVP & Adobe Community Professional

                  I already explained, you can't specify the cDIPath parameter from a

                  doc-level script.

                  • 6. Re: import image
                    Alexito Level 1

                    I've got a bit confused about "can", "cannot" and trusted content.

                    All right, thank you!