21 Replies Latest reply on Mar 18, 2013 3:57 AM by MadMich

    Extract spawned page to folder

    MadMich Level 1

      Is it possible to Spawn a template page  and then extract it to another folder?

      This is what i have so far but I get a range error…

       

       

       

          var t = this.getTemplate("MyTemplate");   

          //var T = t[0];

       

          var XO = t.spawn(this.numPages, true, true);

       

          for (var i=0; i<0; i++) t.spawn(this.numPages, true, true, XO);

       

       

       

      this.pageNum++;

       

       

          /* Extract pages to folder */

       

          // Regular expression used to acquire the base name of file

       

          var re = /\.pdf$/i;

       

          // filename is the base name of the file Acrobat is working on

       

          var filename = this.documentFileName.replace(re,"");

       

          try {for (var j = 0; j < this.numPages; j++)

       

                  this.extractPages({

       

                      nStart: j,

       

                      cPath: "/MyComputer/Desktop/MyFolder/"+filename+"_" + j +".pdf"

       

                  });        

       

          } catch (e) { console.println("Aborted: " + e) }

        • 1. Re: Extract spawned page to folder
          gkaiseril MVP & Adobe Community Professional

          At what line?

           

          Not very clear what you are doing. Could you provide some comments in your code?

           

          What is the total error message?

           

          The 'e' in "catch(e)" is an array from version 6 on. You might want to print the entire array

           

          console.println("Aborted: " + e.join(", "));

          • 2. Re: Extract spawned page to folder
            try67 MVP & Adobe Community Professional

            The path you're using doesn't seem correct. The name of the drive can't be

            "MyComputer"...

            • 3. Re: Extract spawned page to folder
              Bernd Alheit Adobe Community Professional & MVP

              Is this path not allowed on a Mac?

              • 4. Re: Extract spawned page to folder
                try67 MVP & Adobe Community Professional

                I just don't think it's a valid one, but I might be wrong (not a Mac guy)...

                • 5. Re: Extract spawned page to folder
                  MadMich Level 1

                  Thanks for the comments.

                  The path is mac based the name is just fiction for security reasons. I also take on board the comments about the catch(e).

                   

                  The error is as follows:

                  Aborted: RangeError: Invalid argument value.

                   

                  Here are some comments for you as well

                   

                  //Add a second page from a hidden template when  the first page is signed...

                   

                    var t = this.getTemplate("MyTemplate");   

                      //var T = t[0];

                   

                      var XO = t.spawn(this.numPages, true, true);

                   

                      for (var i=0; i<0; i++) t.spawn(this.numPages, true, true, XO);

                   

                  this.pageNum++;

                   

                  // The next stage is to extract the second page and save it into a folder where the process can start all over again.

                   

                      /* Extract pages to folder */

                   

                      // Regular expression used to acquire the base name of file

                   

                      var re = /\.pdf$/i;

                   

                      // filename is the base name of the file Acrobat is working on

                   

                      var filename = this.documentFileName.replace(re,"");

                   

                      try {for (var j = 0; j < this.numPages; j++)

                   

                              this.extractPages({

                   

                                  nStart: j,

                   

                                  cPath: "/MyComputer/Desktop/MyFolder/"+filename+"_" + j +".pdf"

                   

                              });        

                   

                      } catch (e) { console.println("Aborted: " + e) }

                   

                  //Ideally i would like the first pdf to delete the temporary second page and save.

                   

                   

                  I'm open to any better suggestions, I have looked at exporting attachments as well but didn't feel it was right.

                  I basically have 6 folders and only the first one starts with a pdf, the second, third and so on depend on the previous one having a signed pdf.

                  • 6. Re: Extract spawned page to folder
                    try67 MVP & Adobe Community Professional

                    The error message should contain a line number as well...

                     

                    And this line of code will not execute because your stop condition is met before you even enter into it... cause an infinite loop.

                     

                    for (var i=0; i<0; i++) t.spawn(this.numPages, true, true, XO);

                     

                    Edited

                    • 7. Re: Extract spawned page to folder
                      MadMich Level 1

                      The console shows no line number when i initiate the script!

                      All i see is 'Aborted: RangeError: Invalid argument value.'

                      I think the fact that i am creating a new page and then instantly trying to export is the problem and i don't know how to code it.

                       

                      I'm not sure if i'm on the right track, your constructive criticism is appreciated but at the end of the day do you think i am wasting my time going down this route?

                      Do you have any other suggestions for adding a pdf to a folder based on another page or pdf being signed?

                      • 8. Re: Extract spawned page to folder
                        MadMich Level 1

                        I've decided to try something else to achieve the same outcome.

                        Instead of spawning a page i copy/move and rename a template pdf to a new folder called A02 or A03 (depending on wether the active file is in a folder A01 or A02).

                         

                        When I run the script from my console it works a treat! but when i add it to a button action it fails with the following message:

                        'otherDoc is undefined

                        25:AcroForm:Button2:Annot1:MouseUp:Action1

                        TypeError: otherDoc is undefined'

                         

                        I'm stumped again! Can anyone tell me why it does not work from a button action? Here is my script:

                         

                         

                        // first bit sets up the version number for the new file

                        var phase = this.getField("phase").value;

                        var nphase = this.getField("nphase").value;

                        if (nphase == "A01"){

                        phase = "A02";

                        }else{

                        if (nphase == "A02"){

                        phase = "A03"

                        }}

                        var r = (nphase = phase);

                         

                        // break into an array of path components

                        var aPathComps = myDocPath.split("/");

                        var pathRoot = aPathComps[5]; // select foldername

                        var workOrder = aPathComps[aPathComps.length-4]; // Last element

                         

                        // this is the master template file

                        var otherDoc = app.openDoc("/server/account/"+workOrder+"/templateFolder/myfile.pdf")

                         

                        /* Extract pages to folder */

                         

                            // Regular expression used to acquire the base name of file

                         

                            var re = /\.pdf$/i;

                         

                            // filename is the base name of the file Acrobat is working on myfile.pdf

                            var filename = otherDoc.documentFileName.replace(re,"");

                         

                        try {for (var i = 0; i < this.numPages; i++)

                         

                                otherDoc.saveAs({

                         

                                        nStart: i,

                        // path were template will be copied/renamed to

                                        cPath: "/server/account/"+workOrder+"/subfolder/"+r+"/"+filename+"_" +r+".pdf"

                        });  

                            } catch (e) { console.println("Aborted: " + e) }

                        this.closeDoc();

                        • 9. Re: Extract spawned page to folder
                          MadMich Level 1

                          I kind of need to get this working. Has anyone got any suggestions how to fix it?

                          • 10. Re: Extract spawned page to folder
                            try67 MVP & Adobe Community Professional

                            The documents you're opening with openDoc have to be disclosed for it to work.

                            Also, there's a bug in your last loop. You need to access the numPages of otherDoc, not of the "this" object.

                            • 11. Re: Extract spawned page to folder
                              MadMich Level 1

                              Thanks for the fast reply,

                               

                              I've add the following 'this.disclosed = true;' to the document level of the file i am trying to open.

                               

                              I have renamed the last loop to:

                               

                              // filename is the base name of the file Acrobat is working on myfile.pdf

                                  var filename = otherDoc.documentFileName.replace(re,"");

                               

                              try {for (var i = 0; i <otherDoc.numPages; i++)

                               

                                      otherDoc.saveAs({

                               

                              I now get a different error in the console: 'Aborted: NotAllowedError: Security settings prevent access to this property or method.'

                              Again no line error? As before if i run it from the console it works!

                               

                              Cheers again for the feedback

                              • 12. Re: Extract spawned page to folder
                                try67 MVP & Adobe Community Professional

                                There's always a number of the line where the error took place in the error

                                message.

                                Post the entire code and the complete error message.

                                • 13. Re: Extract spawned page to folder
                                  MadMich Level 1

                                  This is the complete message in the console window, honest!

                                  Aborted: NotAllowedError: Security settings prevent access to this property or method.

                                   

                                  This is my code:

                                  // first bit sets up the version number for the new file

                                  var phase = this.getField("phase").value;

                                  var nphase = this.getField("nphase").value;

                                  if (nphase == "A01"){

                                  phase = "A02";

                                  }else{

                                  if (nphase == "A02"){

                                  phase = "A03"

                                  }}

                                  var r = (nphase = phase);

                                   

                                  // break into an array of path components

                                  var aPathComps = myDocPath.split("/");

                                  var pathRoot = aPathComps[5]; // select foldername

                                  var workOrder = aPathComps[aPathComps.length-4]; // Last element

                                   

                                  // this is the master template file

                                  var otherDoc = app.openDoc("/server/account/"+workOrder+"/templateFolder/myfile.pdf" )

                                   

                                  /* Extract pages to folder */

                                   

                                      // Regular expression used to acquire the base name of file

                                   

                                      var re = /\.pdf$/i;

                                   

                                      // filename is the base name of the file Acrobat is working on myfile.pdf

                                      var filename = otherDoc.documentFileName.replace(re,"");

                                   

                                  try {for (var i = 0; i < otherDoc.numPages; i++)

                                   

                                          otherDoc.saveAs({

                                   

                                                  nStart: i,

                                  // path were template will be copied/renamed to

                                                  cPath: "/server/account/"+workOrder+"/subfolder/"+r+"/"+filename+"_" +r+".pdf"

                                  });  

                                      } catch (e) { console.println("Aborted: " + e) }

                                  this.closeDoc();

                                  • 14. Re: Extract spawned page to folder
                                    try67 MVP & Adobe Community Professional

                                    What the heck is this supposed to be?

                                     

                                    var r = (nphase = phase);

                                     

                                    Also, are you sure that the path you're trying to open has the correct

                                    syntax? To know the right syntax you should open the file in Acrobat and

                                    then execute this code in the console:

                                    this.path

                                    • 15. Re: Extract spawned page to folder
                                      MadMich Level 1

                                      var r = (nphase = phase); this gets the field value of the open document (eg A01) and adds it to the path required for the new pdf.

                                      So if the open document is in a folder named A01, a reference pdf will be copied & renamed to folder A02,  if the open document is in a folder named A02, the reference pdf file will be copied to folder A03 and so on (A04, A05...etc).

                                       

                                      When creating this script i had to consider that there will be thousands of folders all with unique work numbers.

                                      The idea is that every time a pdf is signed a reference pdf is copied to the next folder and so on.

                                       

                                      The path works, i did use this.path. As mentioned when i use the console window it all seems to work well?

                                      • 16. Re: Extract spawned page to folder
                                        try67 MVP & Adobe Community Professional

                                        This line doesn't do that. It assigns the value of phase to nphase and then to r...

                                        Also, from which context are you running this code? saveAs needs to run from a trusted context.

                                        • 17. Re: Extract spawned page to folder
                                          MadMich Level 1

                                          The tests I have been running using a mouse down button field script, eventually i want to be able to run it from a signature field script.

                                          The Trusted context i'm afraid i would need help with.

                                          • 18. Re: Extract spawned page to folder
                                            try67 MVP & Adobe Community Professional

                                            This would requires placing a script in a specific folder of each user who

                                            is going to be using this script... Is that possible for you?

                                            • 19. Re: Extract spawned page to folder
                                              MadMich Level 1

                                              I'm not sure that's an option. I have around 250 users globally logging in to a server.

                                              Like most place we upgrade all of the time and i'm concerned it could be a logistical nightmare.

                                              • 20. Re: Extract spawned page to folder
                                                try67 MVP & Adobe Community Professional

                                                Then you're out of luck, I'm afraid...

                                                • 21. Re: Extract spawned page to folder
                                                  MadMich Level 1

                                                  Right thanks for looking at it. I can at least go back and say it's not possible now.