Author Topic: Delete pages marked by keyword  (Read 165 times)

0 Members and 1 Guest are viewing this topic.

Nick Riviera

  • Newbie
  • *
  • Posts: 14
Delete pages marked by keyword
« on: May 05, 2021, 10:27:06 AM »
I have a  acrobat script that I use through document javascript/pdfshell tools
The script works but the pdfs is not save, otherwise I have to open each pdf and save...
I want to run this script on multiple files and then save it automatically
Can be done this ?

acrobat javascript

Quote
var Count1 = this.numPages;


for (var p=this.numPages-1; p>=0; p--) {
    for (var n=0; n<this.getPageNumWords(p); n++) {
        if (getPageNthWord(p, n) == "DELETE") {
            this.deletePages(p);
            break;
           
            app.execMenuItem("Save");
        }
    }
}

RTT

  • Administrator
  • *****
  • Posts: 886
Re: Delete pages marked by keyword
« Reply #1 on: May 07, 2021, 04:51:11 PM »
Try with this mixed Acrobat and PDF-ShellTools script.
Code: [Select]
var app = pdfe.CreateObject("AcroExch.App");
var baseDoc = pdfe.CreateObject("AcroExch.PDDoc");

var ProgressBar = pdfe.ProgressBar;
ProgressBar.max = pdfe.SelectedFiles.Count;

for (var i = 0; i < pdfe.SelectedFiles.Count; i++) {
    try {
        ProgressBar.position = i + 1;
        var FileObj = pdfe.SelectedFiles(i);
        var FullPathFilename = FileObj.Filename;
        FileObj.Close();

        pdfe.echo('Deleting keyword marked pages: ' + FullPathFilename);

        baseDoc.Open(FullPathFilename);

        try {
            var JSObject = baseDoc.getJSObject();
            var PagesGotDeleted = false;
            for (var p = JSObject.numPages - 1; p >= 0; p--) {
                var PageNumWords = JSObject.getPageNumWords(p);
                for (var n = 0; n < PageNumWords; n++) {
                    if (JSObject.getPageNthWord(p, n) == "DELETE") {
                        JSObject.DeletePages(p);
                        PagesGotDeleted = true;
                        break;
                    }
                }
            }
            if (PagesGotDeleted) {
                if (baseDoc.Save(0, FullPathFilename)) {
                    pdfe.echo(" [OK]", 0, 1);
                } else {
                    pdfe.echo(" [failed to save]", 0xFF0000, 1);
                }
            } else {
                pdfe.echo(" [Keyword not found]", 0xFF, 1);
            }
        } catch (err) {
            pdfe.echo(err, 0xFF0000);
            pdfe.echo("");
        }
        baseDoc.Close();
    } catch (err) {
        pdfe.echo(err, 0xFF0000);
        pdfe.echo("");
    }
}
app.Exit();
pdfe.echo('\nDone');
Use the manager to import the attached script. You will get a "Delete pages marked by keyword [DELETE]" named script in the PDF-ShellTools context menu, My Scripts submenu.

Nick Riviera

  • Newbie
  • *
  • Posts: 14
Re: Delete pages marked by keyword
« Reply #2 on: May 10, 2021, 07:05:40 AM »
Works, thanks!


Try with this mixed Acrobat and PDF-ShellTools script.
Code: [Select]
var app = pdfe.CreateObject("AcroExch.App");
var baseDoc = pdfe.CreateObject("AcroExch.PDDoc");
[/quote]