3 Replies Latest reply on Nov 9, 2016 3:16 AM by try67

    Help with Script to Delete page with "word" and the page before.

    jimmyk41283385

      Hi everyone

      I got a PDF generated from another software. It creates pages that i don´t want/or can't print.

      I need help with a script that can search for words (going to be about 5 different words) and delete the page containing one of that words and the page before.

       

      I found a script here but it only deletes the page that contains the word.

      I´m also not sure how to add multiple words without coping the whole script and type a new word.

       

      Using this script now

      for (var p=this.numPages-1; p>=0; p--) {
           for (var n=0; n<this.getPageNumWords(p); n++) {
              if (this.getPageNthWord(p, n) == "WORD") {
                  this.deletePages(p);
              }
          }
      }
      

       

      Anyone got any good ideas to help me?

        • 1. Re: Help with Script to Delete page with "word" and the page before.
          try67 MVP & Adobe Community Professional

          - Are the words that you're searching for just single words, or do you mean that you want to search for a phrase?

          If they are just single words then you can create a complex if-statement, something like this:

           

          var word = this.getPageNthWord(p, n);
          if (word == "WORD1" || word == "WORD2" || word == "WORD3" || word == "WORD4" || word == "WORD5") {
              // add current and previous pages to the list of pages to delete
          }
          

           

          - I recommend you use an array where you hold all the page numbers to be deleted and then use a separate loop (again from the last page to the first one!), where you delete all the pages from that array.

           

          Also, to make your code more efficient I recommend that when you encounter one of the words you're searching for you stop the internal loop and decrease the value of i by 1, to skip the next (ie, previous) page, because there's no point in searching it if you already know you're going to delete it, is there?

          • 2. Re: Help with Script to Delete page with "word" and the page before.
            jimmyk41283385 Level 1

            Thank you!

            Forgot to mention that it sometimes is phrases (or names like "John Smith").

             

            I got the "delete page containing 'word' and the page before" function to work but my problem now is how to make it look for phrases instead of single words.

            • 3. Re: Help with Script to Delete page with "word" and the page before.
              try67 MVP & Adobe Community Professional

              That's quite more complex. The options are either to search the page word by word, and when you encounter the first word in the phrase use a counter to see if the next words after that are a match as well, or to collect the entire page text into a single string and then search for the phrase within that string. The latter might be better in your case because you don't really care about the location of the search term in the page, you just want to know if it's there or not, as far as I can tell.