Copy link to clipboard
Copied
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".
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
}
}
}
Copy link to clipboard
Copied
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;
}
}
}
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now