Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
With JavaScript you can look for words in the document and you can delete pages.
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I would love to accomplish that, but can't get the code correct. Any suggestions?
Copy link to clipboard
Copied
You can delete pages and save the document with a new name.
Copy link to clipboard
Copied
Code suggestions?
Copy link to clipboard
Copied
Look for saveAs in the Acrobat JavaScript Reference.
Copy link to clipboard
Copied
This code extracts the pages that contain the word "losses" and then prompts to save those pages. I'm looking to save all the pages that don't contain that word. Any ideas?
var pageArray = [];
var stringToSearchFor = "losses";
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) {
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++) {
nPage: d.numPages-1,
cPath: this.path,
nStart: pageArray
nEnd: pageArray
} );
}
// remove the first page
d.deletePages(0);
}
Copy link to clipboard
Copied
Delete the pages which are stored in the pageArray.
Copy link to clipboard
Copied
Exactly how would I do that?
Copy link to clipboard
Copied
Use the method deletePages. Make sure that you go from highest to lowest page number.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now