6 Replies Latest reply on Dec 28, 2016 7:55 AM by JR_Boulay

    Find (Whole Words) Delete Page

    OHYEAHDANIEL

      I want to search a multi page pdf for specific text "87 Main" and delete those pages which have that text.

      Search needs to be Whole Words so as not to delete pages with "187 Main".

        • 1. Re: Find (Whole Words) Delete Page
          try67 MVP & Adobe Community Professional

          You'll need to use a loop to iterate over all of the words in all of the pages in the file, looking for this phrase.

          You can use the getPageNthWord method, in combination with the numPages property and getPageNumWords method, all of the Document object.
          Once you locate the phrase you can use the deletePages method to delete that page (again, a part of the Document object).

          I would recommend that you iterate over the pages from the last one to the first, since you're going to be deleting items of the array you're iterating over.

          • 2. Re: Find (Whole Words) Delete Page
            OHYEAHDANIEL Level 1

            Any chance you'd give me the script as a belated Christmas gift?

            I don't know what I'm doing.

             

            I tried a combination of these two scripts but failed to get it to work.

            This one extracts.

            // Iterates over all pages and find a given string and extracts all

            // pages on which that string is found to a new file.

             

            var pageArray = [];

             

            var stringToSearchFor = "Total";

             

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

            // iterate over all words

            for (var n = 0; n < this.getPageNumWords(p); n++) {

            if (this.getPageNthWord(p, n) == stringToSearchFor) {

            pageArray.push(p);

            break;

            }

            }

            }

             

            if (pageArray.length > 0) {

            // extract all pages that contain the string into a new document

            var d = app.newDoc(); // this will add a blank page - we need to remove that once we are done

            for (var n = 0; n < pageArray.length; n++) {

            d.insertPages( {

            nPage: d.numPages-1,

            cPath: this.path,

            nStart: pageArray[n],

            nEnd: pageArray[n],

            } );

            }

             

              // remove the first page

              d.deletePages(0);

              

            }

             

             

            This one deletes.

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

             

             

            • 3. Re: Find (Whole Words) Delete Page
              try67 MVP & Adobe Community Professional

              The last code is pretty close to the mark... Try this one instead:

               

              for (var p=this.numPages-1; p>=0; p--) {
                  if (this.numPages==1) break;
                  for (var n=0; n<this.getPageNumWords(p)-1; n++) {
                      if (this.getPageNthWord(p, n) == "87" && this.getPageNthWord(p, n+1) == "Main") {
                          this.deletePages(p);
                          break;
                      }
                  }
              }
              
              • 4. Re: Find (Whole Words) Delete Page
                OHYEAHDANIEL Level 1

                Thanks Try this works perfectly.

                Can you show me how to combine the two scripts to;

                1. extract all pages with 87 main to a new document and

                2. delete all pages with 87 main from the original pdf?

                • 5. Re: Find (Whole Words) Delete Page
                  try67 MVP & Adobe Community Professional

                  The script above already does #2. To do #1 you would need to study the insertPages and newDoc methods. They allow you to generate a new file and insert pages into it. Keep in mind the script works in reverse (ie, from the last page to the first) when you decide where to insert the pages into your new file.

                  • 6. Re: Find (Whole Words) Delete Page
                    JR_Boulay Adobe Community Professional

                    Hi.

                    I want to search a multi page pdf for specific text "87 Main" and delete those pages which have that text.

                    Search needs to be Whole Words so as not to delete pages with "187 Main".

                    No need to reinvent the wheel, the Acrobat's Redact feature was made for you, especially its Find Text feature which support GREP regex.