Skip to main content
Participating Frequently
November 14, 2016
Question

I would like to delete all pages that contain a certain phrase utilizing a Javascript Action

  • November 14, 2016
  • 1 reply
  • 1452 views

I generate several large PDF files and would like to delete any page within these files that contain a certain phrase and then save the new file. 

This topic has been closed for replies.

1 reply

Bernd Alheit
Community Expert
November 14, 2016

With JavaScript you can look for words in the document and you can delete pages.

Participating Frequently
November 14, 2016

I've been playing around with a couple different variations of code, but nothing has worked.  I've used the following to pull the pages with a certain word, but I think it would be better to delete pages with a phrase.  Any suggestion?

var pageArray = [];

var stringToSearchFor = "Weight";

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);

 

}

Joel Geraci
Community Expert
November 14, 2016

When you're deleting pages, it's far easier to work backwards. Iterate through the pages from the back to the front and just delete the page that contains the word or phrase you're looking for.