Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Find (Whole Words) Delete Page

New Here ,
Dec 26, 2016 Dec 26, 2016

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

TOPICS
Acrobat SDK and JavaScript
987
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 26, 2016 Dec 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 26, 2016 Dec 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;
  
}
  
}
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 26, 2016 Dec 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;

        }

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 27, 2016 Dec 27, 2016

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 27, 2016 Dec 27, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 28, 2016 Dec 28, 2016
LATEST

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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines