5 Replies Latest reply on Sep 15, 2011 5:46 PM by _jthompson

    "import as layer" with javascript?

    _jthompson Level 1



      I was wondering if there are any here that could give me some javascript batch processing in Acrobat advice. My problem is this... I have many (thousands) of pdf that have an object on a layer with incorrect data. In Illustrator I have written a script that rebuilds this object, populates it with correct data from a csv file then saves it as a pdf file with a name that corresponds to the master pdf it's to be inserted into.


      What I want to do with a javascript batchscript is for each file opened the file name is found and the newly created pdf is found (done) then do something equivalent to the "import as layer" command and import to an already existing layer in the master pdf so the new object masks the old. This I cannot do... well, really I cannot do much with js in acrobat for that matter. I cannot even get a var to return a value in the colsole for testing. It just returns "undefined".


      I can find the layer I need to manipulate with 'this.getOCGs()[0]', the layer I need to manipulate happens to be zero, and using the "Merging Layers" example in the Acrobat JavaScript Scripting Guide a layers properties are changed to match the taget layer but it still appears as a separate physical layer.


      What I just wrote may not make a whole lot os sense so in other words.... What I would like to do with javascript is mirror the action of the "Import as Layer" option selected from the Layers panel. The key function I want to perform is in the "Import Options" is to "Add to Existing Layer".


      I appreciate any help that can be offered.



        • 1. Re: "import as layer" with javascript?
          try67 MVP & Adobe Community Professional

          There's a lot that can't be done with JavaScript in Acrobat.
          As far as I know, you can't add or merge a layer directly, but you can add an OCG using the addWatermarkFromFile() method.


          Regarding the console, simply execute your code and then either select  the name of the variable you want to examine, or type it, and press  Ctrl+Enter. It will display its value. For example, if you execute this  code:
          var a = 2 + 5;

          The result will be "7".

          • 2. Re: "import as layer" with javascript?
            _jthompson Level 1

            Thanks for the reply try67!


            It's good to know there isn't a lot that can be done with js... Not knowing the range of possibilities is likely my biggest problem. Do you know if the Acrobat SDK would give deeper hooks into a document? I would be in heaven if I could just write something in Python and not have to run Acrobat or use any of it's built in features. I'll give the addWate3rMarkFromFile() method a try... I haven't in the past because I assumed, since it's a watermark, visibility could not be toggled like the other layers.


            Since my last message I played a bit more with the console and found my troubles were caused by not hitting ctrl+enter after each line. Instead I would write a few lines then ctrl+enter to test and it would fail.


            Thanks again.



            • 3. Re: "import as layer" with javascript?
              try67 MVP & Adobe Community Professional

              Don't get me wrong, you can achieve a lot with JS, but there's also a lot that can't be achieved, if that makes sense...

              After you create the watermark you can change its settings like any other OCG.


              To run multiple lines in the console you must select them all and then press Ctrl+Enter.


              Anyway, for questions about the Acrobat SDK you should try that forum.

              1 person found this helpful
              • 4. Re: "import as layer" with javascript?
                Dave Merchant MVP & Adobe Community Professional

                There are no tools in the JavaScript automation interface to merge, edit, replace or remove an OCG, or place page objects on them.


                Some things can be done via the SDK, by manipulating the PDOCG object, but it's all low-level stuff - you have to work with the object memberships themelves, we don't have nice wrapper functions like "move_to_layer_x" or "replace_layer_x", and membership of a PDOCG dictionary isn't directly related to an object's presence in the document (deleting the PDOCG doesn't delete the object). There's an example of using OCGs in Snippet Runner (a C++ plugin in the SDK) but nothing that directly replicates the "add to existing layer" command in the UI. It is possible to do that, but you'll have to write the code from scratch.

                1 person found this helpful
                • 5. Re: "import as layer" with javascript?
                  _jthompson Level 1

                  Can't edit, remove, replace, an OCG with js.... Too bad. I would really like to be able to deeply edit PDFs and after looking at the SDK it's way more effort than I want to put into this right now.


                  Regardless, I used addWatermarkFromFile() and though the result is not exaclty what I want it's adequate. Using js first in Illustrator to rebuild the data table I needed then again to insert the corrected data into the original PDF I was able to repair thousands of PDFs that would have taken perhaps three weeks to first re-generate then QC.


                  Thank you both for all your help!