22 Replies Latest reply on Apr 7, 2015 1:01 AM by itzrandy

    Page Spawning - Prompting For Number Of Pages

    itzrandy Level 1

      I asked a similar question a few years back and at the time, the project went by the wayside.  Well, the project is back on track and now I'm in a jam and need to use some javascript for some easy automation.  Here's the situation.  I have a form with a hidden button that allows persons who know where the button is, to spawn the current semplate into additional pages, while renaming the approzimately 400+ form fields on each page.  As it currently stands, the user needs to click the hidden button each time to spawn a new page.  This is kind of cumbersome considering, I need to generate approximately 200 pages at a time.  Here is the current code (culled from any of a number of web pages) that works for the single click page addition:

       

      // Get a template object from an existing template

      var t = getTemplate("temp1");

       

       

      // Spawn the template on the page following this one, and rename the fields

      t.spawn({nPage: pageNum + 1, bRename: true, bOverlay: false});

       

       

       

      What I'd like to do is have the button "click" (mouse up) produce a dialog box that asks the user to input the number of pages needed to be spawned.  I'm not concerned about page numbering as I have a separate means for doing that, once all the pages have been spawned.  Clearly, I am not a javascript programmer, so, if someone is willing to help, it's going to have to be usable code that will require only minor tinkering, if any.  Any help or ideas would be greatly appreciated.  I'm using Acrobat Pro X.  There was an MVP named George Johnson who tried to help me back them, but quite honestly, Is imply didn't know how to tie it all together.  He suggested using the following code in generating the dialog box (obviously, text is not geared toward my document).  Can someone show me how to tie all of this together?

       

      var cResponse = app.response({

                 cQuestion: "How are you today?",

                     cTitle: "Your Health Status",

                     cDefault: "Fine",

                     cLabel: "Response:"

      });

      if (cResponse == null)

                 app.alert("Thanks for trying anyway.");

      else

                 app.alert("You responded, \""+cResponse+"\", to the health "

                           + "question.",3);

       

       

      Thank you in advance,

       

      Randy

        • 1. Re: Page Spawning - Prompting For Number Of Pages
          George_Johnson MVP & Adobe Community Professional

          The code could be something along the lines of:

           

          var resp = app.response({

              cQuestion: "How many pages to you want to add?",

              cTitle: "Enter number of pages to add",

              cDefault: "1",

              cLabel: "Pages:"

          });

           

          // Get a reference to the template to spawn

          var t = getTemplate("temp1");

           

          // Only proceed if user enters a value,

          // that can be converted to a number,

          // and the number is at least 1 and no more than 200

          if (resp && !isNaN(+resp) && +resp >= 1 && +resp <= 200) {

           

              // Round number of pages to nearest integer

              var num = Math.round(+resp);

           

              // Spawn the specified number of pages

              for (var i = 0; i < num; i += 1) {

                  if (i === 0) {

                      var oXO = t.spawn({nPage: numPages, bRename: true, bOverlay: false});

                  } else {

                      t.spawn({nPage: numPages, bRename: true, bOverlay: false, oXObject: oXO});

                  }

              }

           

          } else {

              app.alert("You entered an invalid number: " + resp);

          }

           

           

          I didn't test this code, so it could have bugs, but it should get you started. I usually add a check to make sure the user doesn't attempt to add too many pages. The code above limits the user to adding 200 pages max. at a time, but it doesn't prevent them from doing it multiple times.

          • 2. Re: Page Spawning - Prompting For Number Of Pages
            itzrandy Level 1

            George,

             

            Once again, you are a lifesaver.  Your code did exactly what I was looking to accomplish.  If you're up to another challenge, what can I do to accomplish one additional task?  Once, I've generated all 200 pages, I'm saving the document as a read only template for others to use for data fill in.  They will then save the project with another name, to retain the data.  Since the form will eventually leave our little realm, I'd like to know if there is a way to go through all 200 pages and delete the hidden spawn button?  For instance, put a temporary button on page one that's programmed to go through and delete the spawn buttons on all additional pages, which would follow the naming convention of "P#.temp1.button1".  I would then simply manually delete the buttons from page one (the new "delete" button and the spawn button).  I'd hate to have to go through 200 pages, one by one.

             

            Again, thank you so very much!  if there's such a thing as karma points, you've just banked a boat load!

             

            Randy

            • 3. Re: Page Spawning - Prompting For Number Of Pages
              itzrandy Level 1

              Greetings George!

               

              A while back, you assisted me with the following code (see below), which was used to spawn a multi-page document.  By the way, the code was just what the doctor ordered, thank you!  I am now faced with another issue while spawning this document.  I have one field (show_num) which I need to prevent from renaming, when the document is spawned.  All other fields, rename perfectly, as desired.  I now will be utilizing this show_num field, which was previously just a place holder and not used.  Is there a way to have the field remain the same name as the pages are spawned?  In other words, I'm looking to have the user type a "show number" on the first page and have it immediately populate in the same field on all pages (i.e. 1026-022).  This number would then populate on all pages in the "show_num" field.  Here's the code I'm using to spawn the pages:

               

              var resp = app.response({

               

               

                  cQuestion: "How many pages to you want to add?",

               

               

                  cTitle: "Enter number of pages to add",

               

               

                  cDefault: "1",

               

               

                  cLabel: "Pages:"

               

               

              });

               

               

               

               

               

              // Get a reference to the template to spawn

               

               

              var t = getTemplate("temp1");

               

               

               

               

               

              // Only proceed if user enteres a value,

               

               

              // that can be converted to a number,

               

               

              // and the number is at least 1 and no more than 600

               

               

              if (resp && !isNaN(+resp) && +resp >= 1 && +resp <= 600) {

               

               

               

               

               

                  // Round number of pages to nearest integer

               

               

                  var num = Math.round(+resp);

               

               

               

               

               

                  // Spawn the specified number of pages

               

               

              for (var i = 0; i < num; i += 1) {

               

               

                  if (i === 0) {

               

               

                      var oXO = t.spawn({nPage: numPages, bRename: true, bOverlay: false});

               

               

                  } else {

               

               

                      t.spawn({nPage: numPages, bRename: true, bOverlay: false, oXObject: oXO});

               

               

                  }

               

               

               

               

               

                  // Delete the button

               

               

                  removeField("P" + (numPages - 1) + ".temp1.spawnPage");

               

               

              }

               

               

              } else {

               

               

                  app.alert("You entered an invalid number: " + resp);

               

               

              }

              • 4. Re: Page Spawning - Prompting For Number Of Pages
                George_Johnson MVP & Adobe Community Professional

                You can do that, but you have to create two separate template pages. One would contain the fields that you want to rename when spawned, and the other would contain the fields that you don't want renamed. Spawn the page with the renamed fields first, then spawn the other on top of it (set bOverlay to true, bRename to false, and set the correct page.)

                • 5. Re: Page Spawning - Prompting For Number Of Pages
                  itzrandy Level 1

                  Okay, I can understand the concept of using the second template, but I'm confused as to how you'd spawn the 2nd template over the top of the newly created form from the 1st template?  How do you "set the correct page", when in practice I have just created 400 new pages?

                   

                  Once again, thank you for your help, George!

                   

                  Randy

                  • 6. Re: Page Spawning - Prompting For Number Of Pages
                    gkaiseril MVP & Adobe Community Professional

                    One create the new page with the fields that had to be renames. The second overlay is to add the field that do not need to be renamed.

                    • 7. Re: Page Spawning - Prompting For Number Of Pages
                      itzrandy Level 1

                      I understand the need for the 2nd template, I'm just a bit baffled as to how you open a 2nd template and have it's actions applied to the document you've just spawned from the 1st template?

                      • 8. Re: Page Spawning - Prompting For Number Of Pages
                        George_Johnson MVP & Adobe Community Professional

                        To clarify, you spawn the second template on the page you just created by spawning the first template. So if you start out with a one page document, you end up with a two page document after spawning the first template. You then spawn the second template on the second page (now page 1). This second template should be a blank page with just the form fields that you don't want to be renamed when the template is spawned on top of another.

                        • 9. Re: Page Spawning - Prompting For Number Of Pages
                          itzrandy Level 1

                          Okay, now I'm completely baffled ... I'm going to chalk it up to being up too many hours, working.  The first template, if I were to spawn it, as I normally would, it prompts for how many pages I need.  Usually that is 400.  Then I'm supposed to spawn another template (the 2nd one with single field that shouldn't be renamed), on top of the 400 pages I just spawned?  How, do I get the 2nd template to in any way interact with the previous 400 pages I just created?

                           

                          Sorry if I'm being a bit thick on this one, but it's just not making any sense to me as to how this would work.

                          • 10. Re: Page Spawning - Prompting For Number Of Pages
                            George_Johnson MVP & Adobe Community Professional

                            To begin, you create 400 new pages by spawning the template 400 times. You already have this set up. You then need to spawn the second template on top of each of those 400 new pages. In this case, spawning does not create a new page, just new content on an existing page. So when you spawn a template, you can either create a new page in the document or add content to an existing page.

                            • 11. Re: Page Spawning - Prompting For Number Of Pages
                              itzrandy Level 1

                              I'll give it a shot and see what I can come up with.  I guess I'm just confused as to how you can have two templates at work within one document.  Can I use the same prompt coding to spawn the "new" template 400 times on top of the previously spawned document?

                               

                              Thanks again for trying to spell this out for me!

                              • 12. Re: Page Spawning - Prompting For Number Of Pages
                                George_Johnson MVP & Adobe Community Professional

                                Normally, templates are hidden, as opposed to the initial page of a single page document. So what you'd do is add a blank page to your existing document, add the field in the location you want it, make it a template, and then hide it. You will then be able to spawn this hidden template on an existing page.

                                 

                                To create a blank page (letter sized) in the document, open the document in Acrobat, open the JavaScript console (Ctrl+J), and enter: newPage()

                                 

                                If you need a different page size, you can specify a width and height parameter like:

                                 

                                newPage({nWidth: 6 * 72, nHeight: 4 * 72})    // Add a 6 inch by 4 inch page (72 points/inch)

                                 

                                With the cursor on that line of code, press Ctrl+Enter, or Enter on the numeric keypad. This will add a blank page to the end of the document. All you need to do now is make it a template, add the field(s), and hide it. Make note of the name you gave it so you can use it in the additional code you'll add to the script. If you get stuck with the code, post again.

                                • 13. Re: Page Spawning - Prompting For Number Of Pages
                                  itzrandy Level 1

                                  Thanks George!  I'll give it a shot when I can look at it through a fresh pair of eyes.  Just an FYI, it'll be an 11" x 17" sheet.  I'm assuming I can delete the hidden page once the full document has been spawned?

                                  • 14. Re: Page Spawning - Prompting For Number Of Pages
                                    George_Johnson MVP & Adobe Community Professional

                                    There's no need to delete it since it will be hidden and any space it take up will be negligible.

                                    • 15. Re: Page Spawning - Prompting For Number Of Pages
                                      itzrandy Level 1

                                      Normally I'd agree, but there's a huge effort afoot to steal the concept I'm working on ... Lawyers involved and all, so there's no way I'm leaving a footprint for these folks to try and dissect.

                                      • 16. Re: Page Spawning - Prompting For Number Of Pages
                                        itzrandy Level 1

                                        George ... Are you hiding out there?  Help!!!

                                         

                                        Okay, the topic that I visited above, back in 2014 has officially reared its ugly head.  Here's the scenario:  I have an existing 400 page document that I now need to put a field into.  The field needs to be named the same on all 400 pages, for arguments sake we'll call it "show".  When I type into "show" on page 1, the info will repeat on each page.  Is there a template or other means I can use to place the field named "show" on each of these 400 pages.  The pages already have half a gazillion other fields from originally being spawned and they all work properly.

                                         

                                        Any ideas, coding help you may be able to offer?  This one has far exceeded my abilities to comprehend.

                                         

                                        Thanks,

                                         

                                        Randy

                                        • 17. Re: Page Spawning - Prompting For Number Of Pages
                                          George_Johnson MVP & Adobe Community Professional

                                          Just create the field on the first page in the range and copy it to the others. In Acrobat 11 you'd select the select object tool (small black arrow) or go into form editing mode (Tools > Forms > Edit), right-click the field, and select "Duplicate Across Pages" from the popup context menu.

                                          • 18. Re: Page Spawning - Prompting For Number Of Pages
                                            itzrandy Level 1

                                            The problem is, I have hundreds of fields per page.  Trying to go into edit mode with that many fields across 400 pages, simply doesn't work.  So, is there a way, utilizing the code you prepared for me to spawn all those pages, add a line that will rename all the fields that contain the word "show" in the field name (e.g. "p4.temp1.show.1") back to the name "show".  In other words, utilize the duplicate field function towards the end of the code string?  Here's the code I'm using, which works awesome.

                                             

                                            var resp = app.response({

                                             

                                             

                                                cQuestion: "How many pages to you want to add?",

                                             

                                             

                                                cTitle: "Enter number of pages to add",

                                             

                                             

                                                cDefault: "1",

                                             

                                             

                                                cLabel: "Pages:"

                                             

                                             

                                            });

                                             

                                             

                                             

                                             

                                             

                                            // Get a reference to the template to spawn

                                             

                                             

                                            var t = getTemplate("temp1");

                                             

                                             

                                             

                                             

                                             

                                            // Only proceed if user enteres a value,

                                             

                                             

                                            // that can be converted to a number,

                                             

                                             

                                            // and the number is at least 1 and no more than 600

                                             

                                             

                                            if (resp && !isNaN(+resp) && +resp >= 1 && +resp <= 600) {

                                             

                                             

                                             

                                             

                                             

                                                // Round number of pages to nearest integer

                                             

                                             

                                                var num = Math.round(+resp);

                                             

                                             

                                             

                                             

                                             

                                                // Spawn the specified number of pages

                                             

                                             

                                            for (var i = 0; i < num; i += 1) {

                                             

                                             

                                                if (i === 0) {

                                             

                                             

                                                    var oXO = t.spawn({nPage: numPages, bRename: true, bOverlay: false});

                                             

                                             

                                                } else {

                                             

                                             

                                                    t.spawn({nPage: numPages, bRename: true, bOverlay: false, oXObject: oXO});

                                             

                                             

                                                }

                                             

                                             

                                             

                                             

                                             

                                                // Delete the button

                                             

                                             

                                                removeField("P" + (numPages - 1) + ".temp1.spawnPage");

                                             

                                             

                                            }

                                             

                                             

                                            } else {

                                             

                                             

                                                app.alert("You entered an invalid number: " + resp);

                                             

                                             

                                            }

                                            • 19. Re: Page Spawning - Prompting For Number Of Pages
                                              George_Johnson MVP & Adobe Community Professional

                                              You can either do it by spawning a template that contains just that single field as discussed in detail above, or do it manually like I mentioned most recently. It's not clear to me why this doesn't work for you, but you don't really need to be in form editing mode when duplicating the field.

                                              • 20. Re: Page Spawning - Prompting For Number Of Pages
                                                itzrandy Level 1

                                                George,

                                                 

                                                I truly hope I'm not wearing on your patience, not my intention.  I tried creating a template with just the single field but I have no idea how to get it to spawn into the 400 existing pages.  Yes, that would probably be easiest.  As for duplicating the field, the only way I know of doing it is via clicking Tools - Forms - Edit (Acrobat X Pro), which in turn puts you into a mode where are fields are then shown and editable.  Am I missing something here?  Is there another way to duplicate fields without going into edit mode?

                                                 

                                                Thanks again,

                                                 

                                                Randy

                                                • 21. Re: Page Spawning - Prompting For Number Of Pages
                                                  itzrandy Level 1

                                                  Disregard George,

                                                   

                                                  Just found the select object tool!  When I create the field on the first page, spawn the doc, will the duplicate across all pages eliminate the fields spawned as p1.temp.show, p2. ... etc.?

                                                  • 22. Re: Page Spawning - Prompting For Number Of Pages
                                                    itzrandy Level 1

                                                    It all makes sense now George! 

                                                     

                                                    I created a blank page, before the page with spawn code and put the field on that page.  I then spawned the document from the 2nd page (originally Pg 1) and all the pages were spawned subsequent to page 2.  Go back to page 1 with the single field and ran duplicate from there.  Seems a bit convoluted but it works!

                                                     

                                                    THANK YOU for all of your help!

                                                     

                                                    Randy