6 Replies Latest reply on Aug 19, 2013 6:46 PM by MikeIs2C

    Can't delete blank page of new doc after page insert

    MikeIs2C Level 1

      Hi,

       

      I am trying to come up with a solution that creates a copy of the first page of my document, then allow it to be saved as its own new document.  Thanks to the help of this forum, I have worked through the issues I was having to where I can get everything to work desired, except I can't get rid of the blank last page (created by the newDoc function).

       

      I believe the problem boils down to the fact that the copied (inserted) page uses a template.  If my understanding of using templates is correct, then I can't delete any pages.  As it stands, whenever I try to use the deletePages function, I am left with just the blank page (no matter which page is selected for deletion).  So if my suspicions are correct about the template, is there any other way I might be able to get rid of the blank page?

       

      Just to note, I also tried using replacePages to directly insert the copy as the first page.  However, when I do that it only copies over only the text descriptions: none of the form fields, buttons or anything else.  What might be causing that to happn?  I suppose that may qualify as a separate question where I could post it and it could be answered separately.  In any case, at the moment for me it is all part of trying to solve the same problem.

       

      I am including my code below to hopefully help anyone see what is going on:

       

      (Trusted functions needed for button activation:)

       

      myTrustedMenu = app.trustedFunction(function(name)

      {

          app.beginPriv();

          app.execMenuItem(name);

          app.endPriv();

      });

       

      myTrustedSave = app.trustedFunction(function(name, doc)

      {

          app.beginPriv();

          app.execMenuItem(name, doc);

          app.endPriv();

      });

       

      myTrustedSaveAs = app.trustedFunction(function(doc, namePath)

      {

          app.beginPriv();

          doc.saveAs(namePath);

          app.endPriv();

      });

       

      myTrustedNewDoc = app.trustedFunction( function ()

      {

          app.beginPriv();

          var doc = app.newDoc();

          app.endPriv();

          return doc;

      });

       

      myTrustedInsertFirstPage = app.trustedFunction(function(doc, namePath)

      {

          app.beginPriv();

          doc.insertPages ({

              nPage: -1,

              cPath: namePath,

              nStart: 0

          });

          app.endPriv();

      });

       

      myTrustedReplacePage = app.trustedFunction(function(doc, namePath)

      {

          app.beginPriv();

          doc.replacePages ({

              nPage: 0,

              cPath: namePath,

              nStart: 0

          });

          app.endPriv();

      });

       

      myTrustedDeleteLastPage = app.trustedFunction(function(doc)

      {

         

          app.beginPriv();

          doc.deletePages ({

              nSart: doc.numPages-1

          });

          app.endPriv();

      });

       

       

      (The button code used to activate new form document:)

       

      // Get path separate from name.

      // First split document path into an array

      var aPathComps = this.path.split("/");

      // Get File Name off end of array

      var myFileName = aPathComps.pop();

      // Remaining array is path minus name segments

      // Join as string containing path.

      var sPath = aPathComps.join("/");

       

      // Get filename from user; if not entered or cancelled selected, exit.

      var cFileName = app.response ({

          cQuestion:"Enter the name to save the new form document as (just the name, no extension):",

          cTitle:"Save New Form Document As...",

          bPassword: false,

          cDefault: "",

          cLabel:"Filename"

      });

       

      // If not null and has something entered

      if(cFileName && cFileName.length) {

         

          // Trusted function to create new document and return object.

          var oMyDoc = myTrustedNewDoc();

       

          // Trusted function to replace new doc page with first page of this doc.

          //myTrustedReplacePage(oMyDoc, this.path);

       

          // Trusted function to insert into new doc page first page of this doc.

          myTrustedInsertFirstPage(oMyDoc, this.path);

       

          oMyDoc.resetForm();

       

          // Trusted function to delete blank page of new doc.

          myTrustedDeleteLastPage(oMyDoc);

       

         // Trusted function to save new doc with entered filename and same path as this doc.

          myTrustedSaveAs(oMyDoc, sPath + "/" + cFileName + ".pdf");

       

          oMyDoc.closeDoc(true);

      } else {

          app.alert("User canceled save as operation");

      }

       

       

      Many thanks to anyone who might have some answers and/or solutions.

       

      Mike

        • 1. Re: Can't delete blank page of new doc after page insert
          Bernd Alheit Adobe Community Professional & MVP

          Why do you use so many unnessary trusted functions?

          1 person found this helpful
          • 2. Re: Can't delete blank page of new doc after page insert
            MikeIs2C Level 1

            Hi Bernd,

             

            I am using buttons to activate various features on my form document.  Being new at this, I found out that trusted functions had to be used to add privileges at the button level.  The following trusted functions that probably are not relevant for this button are:

             

            • myTrustedMenu is used for another button that currently uses it to save the document.
            • myTrustedSave was added while trying to troubleshoot.
            • myTrustedReplacePage was an attempt at using a different approach (as noted above).

             

            The above are all part of the same JS file so I included them -- in case they are relevant to the problem for some reason.  To my understanding so far, all of the other trusted functions (tf) were required in order to work from a button. 

             

            Given that, if there are still any tf that are unnecessary please let me know; especially if they are somehow part of the problem. 

             

            Thank you for your response,

             

            Mike

            • 3. Re: Can't delete blank page of new doc after page insert
              Bernd Alheit Adobe Community Professional & MVP

              doc.deletePages ({

                      nSart: doc.numPages-1

                  });

               

              You must use nStart, not nSart.

               

              doc.deletePages ({

                      nStart: doc.numPages-1

                  });

              1 person found this helpful
              • 4. Re: Can't delete blank page of new doc after page insert
                MikeIs2C Level 1

                Hi again,

                 

                Sorry to take so long to respond; I got pulled off in a different direction for a while.  Thank you for pointing out what should have been an obvious mistake to me.  Unfortunately fixing it didn't solve the problem.  After I fixed it I started getting an error about the nEnd not being available (unfortunately don't remember the exact error message).  Further attempts on my part to add the nEnd, and change every possible combination of values (with or without the nEnd) failed every time.

                 

                I eventually thought about trying this from a different angle.  What I did instead was set up a loop to check for blank pages on the new document; if found they would be deleted.  This worked to solve the problem I was having.  As you can see from the  code below, I was able to use the deletePages commanded directly -- without having to use a trusted function -- not what I expected originally.

                 

                So your question about why so many trusted functions had merit.  While most of my trusted functions were required, the one for deletePages was not (I had assumed it would be).  Anyway, both your question and showing me the error in my code were helpful and I will mark them as such.  Altough to solve the problem itself, I believe my modified code below does that.

                 

                Thanks again,

                 

                Mike

                 

                 

                // Get path separate from name.

                // First split document path into an array

                var aPathComps = this.path.split("/");

                // Get File Name off end of array

                var myFileName = aPathComps.pop();

                // Remaining array is path minus name segments

                // Join as string containing path.

                var sPath = aPathComps.join("/");

                 

                // Get filename from user; if not entered or cancelled selected, exit.

                var cFileName = app.response ({

                    cQuestion:"Enter the name to save the new form document as (just the name, no extension):",

                    cTitle:"Save New Form Document As...",

                    bPassword: false,

                    cDefault: "",

                    cLabel:"Filename"

                });

                 

                // If not null and has something entered

                if(cFileName && cFileName.length) {

                   

                    // Trusted function to create new document and return object.

                    var oMyDoc = myTrustedNewDoc();

                 

                    // Trusted function to insert into new doc page first page of this doc.

                    myTrustedInsertFirstPage(oMyDoc, this.path);

                  

                    // Get rid of any values possibly entered into the copied form page.

                    oMyDoc.resetForm();

                 

                    // Loop through pages and delete blank pages.

                    for (var i = 0; i <  oMyDoc.numPages; i++)

                    {

                        // Check for words on page, if none delete it.

                        numWords = oMyDoc.getPageNumWords(i);

                        if (numWords == 0)

                        {

                            // This page has no text, delete it.

                            oMyDoc.deletePages(i,i);

                        }

                    }

                 

                    // Trusted function to save new doc with entered filename and same path as this doc.

                    myTrustedSaveAs(oMyDoc, sPath + "/" + cFileName + ".pdf");

                 

                    // Close document or it will stay open!

                    oMyDoc.closeDoc(true);

                } else {

                    app.alert("User canceled save as operation");

                }

                • 5. Re: Can't delete blank page of new doc after page insert
                  Bernd Alheit Adobe Community Professional & MVP

                  // Loop through pages and delete blank pages.

                      for (var i = 0; i <  oMyDoc.numPages; i++)

                      {

                          // Check for words on page, if none delete it.

                          numWords = oMyDoc.getPageNumWords(i);

                          if (numWords == 0)

                          {

                              // This page has no text, delete it.

                              oMyDoc.deletePages(i,i);

                          }

                      }

                   

                   

                  This method will also delete pages with graphic (without text).

                  1 person found this helpful
                  • 6. Re: Can't delete blank page of new doc after page insert
                    MikeIs2C Level 1

                    That is not a problem for my needs on this, but it will probably help someone later on to whom it may apply.  Good job on you pointing that out!