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

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

New Here ,
Nov 14, 2016 Nov 14, 2016

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. 

TOPICS
Acrobat SDK and JavaScript , Windows
1.3K
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 ,
Nov 14, 2016 Nov 14, 2016

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

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 ,
Nov 14, 2016 Nov 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);

 

}

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 ,
Nov 14, 2016 Nov 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.

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 ,
Nov 14, 2016 Nov 14, 2016

I would love to accomplish that, but can't get the code correct.  Any suggestions?

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 ,
Nov 14, 2016 Nov 14, 2016

You can delete pages and save the document with a new name.

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 ,
Nov 14, 2016 Nov 14, 2016

Code suggestions?

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 ,
Nov 14, 2016 Nov 14, 2016

Look for saveAs in the Acrobat JavaScript Reference.

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 ,
Nov 14, 2016 Nov 14, 2016

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

  1. 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++) {

  1. d.insertPages( {

nPage: d.numPages-1,

cPath: this.path,

nStart: pageArray,

nEnd: pageArray,

} );

}

  // remove the first page

  d.deletePages(0);

 

}

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 ,
Nov 14, 2016 Nov 14, 2016

Delete the pages which are stored in the pageArray.

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 ,
Nov 14, 2016 Nov 14, 2016

Exactly how would I do that? 

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 ,
Nov 14, 2016 Nov 14, 2016
LATEST

Use the method deletePages. Make sure that you go from highest to lowest page number.

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