Skip to main content
Participant
December 26, 2016
Question

Find (Whole Words) Delete Page

  • December 26, 2016
  • 2 replies
  • 1115 views

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".

This topic has been closed for replies.

2 replies

JR Boulay
Community Expert
Community Expert
December 28, 2016

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.

Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
December 26, 2016

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.

Participant
December 26, 2016

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,

nEnd: pageArray,

} );

}

  // 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;
  
}
  
}
}

try67
Community Expert
Community Expert
December 26, 2016

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;

        }

    }

}