How do I delete certain pages

Hello,

In my form, I would like to be able to delete specific pages based on check boxes. I used "this.deletePages({nStart: 4, nEnd: 7});" but in fact I would like to look for a word in the page and then delete the page.

Any help will be grateful


Alain TUCCILLO


8 Answers

Voted Best Answer

You need to update the script and replace "TheWord" in the script with the word you are looking for.

Do you get any errors in the JavaScript console?

This approach requires that you can actually extract text from the document. If you have a scanned document, the document needs to be OCRed first. Sometimes you have a file that just does not allow you to extract text because information in the document is missing.

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


By Karl Heinz Kremer   

Deleting pages will only work in Adobe Acrobat, not the free Reader. If that is not a problem, you can use the "word finder" to find a certain word on a page. You would do this by iterating over all pages in your document, and then over all words on that page. You would use code like this:

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

This will loop over all pages, starting at the end of the document (so that when you remove a certain page, the page numbers before that page will not change).

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


Karl Heinz Kremer   

Karl, thank for your prompt response.

I just tried you script and it seems that it is running throught the document but nothing happen. Should the word to be in a textField or not, in order for the script to locate it and then to delete the page?

Thank


Alain TUCCILLO   

I did a small mistake on copying the script and now it is ok.

I would like to be able to delete several pages at the same time using this method i.e to delete page X contains the word 3A and the page X+1 contains the word 3B.

Also instead of to look through all the document and find the specific word(s), is it possible to point a section on it and then to look for the word(s)?

Thx


Alain TUCCILLO   

I would like to be able to delete several pages at the same time using this method i.e to delete page X contains the word 3A and the page X+1 contains the word 3B.

You need to add more cases to the part of the script that checks if the word exists:

if (this.getPageNthWord(p, n) == "3A") {
        this.deletePages(p);
        break;
}
else if (this.getPageNthWord(p, n) == "3B") {
        this.deletePages(p);
        break;
}

Because there is some overhead in actually getting the word, you may want to get it only once and then use a variable in your actual comparison:

var theCurrentWord = this.getPageNthWord(p, n);
if (theCurrentWord == "3A") {
    this.deletePages(p);
    break;
}
else if (theCurrentWord == "3B") {
    this.deletePages(p);
    break;
}

Also instead of to look through all the document and find the specific word(s), is it possible to point a section on it and then to look for the word(s)?

Not with JavaScript. You can select a word, but you cannot get the word that is currently selected.

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


Karl Heinz Kremer   

I got an error message regarding the script
"unlabeled break must be inside loop or switch
4: at line 5"

I don't know how to solve it.


Alain TUCCILLO   

From a different question posted by Alain:

Is it possible to move to a page using "Go to a page view" and delete the 4 following pages after the current page?

How would you move to a different page? Would the user select the page, or would this be done by e.g. clicking on a bookmark or a link?

In general, you can find out what the current page is, and based on that, you can select the next four pages.

Let's assume you have a menu item that the user triggers when the pages should be deleted, you can then use the following script to delete the next four pages.

// delete the four pages following the current page

for (var p=this.pageNum+4; p>this.pageNum; p--) {
    if (p < this.numPages) 
        this.deletePages(p);
}

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


Karl Heinz Kremer   

Hello Karl,

Your last script do exactly what I want.

Thx a lot


Alain TUCCILLO   


Please specify a reason: