简体   繁体   中英

Adobe PDF Javascript to Extract Name, save file

Looking for some help with Javascript in Adobe Acrobat Pro XI.

Here's what I have so far:

   /* Extract pages to file */
   // Regular expression used to acquire the base name of file

   var re = /\.pdf$/i;
   // filename is the base name of the file Acrobat is working on

   var filename = this.documentFileName.replace(re,"");

   try {for (var i = 0; i < this.numPages; i+=4)
         this.extractPages({
            nStart: i,
            nEnd: i+3,
         });         
   } catch (e) { console.println("Aborted: " + e) }

What happens now is that it extracts the page range and creates a series of open files that I can then rename.

What I'd like to do is add a loop to extract a name from the bill, then use that as the file name to create a new file in a folder.

I found this:

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

which I think will get me part of the way there, but I'm not sure how to include that in my loop.

I think I need to search the document a static string (in this case "Student Name") and then return the next two words.

Worked up a file to do this sort of thing using copy-pasted code (and some help from Thom Parker on the Adobe Forums, see: https://community.adobe.com/t5/acrobat/step-by-step-instructions-for-saving-a-pdf-in-acrobat-dc-using-a-javascript/mp/10893916?page=1#M238905 )

 // https://community.adobe.com/t5/acrobat/how-to-set-up-a-default-value-in-execdialog/td-p/9343585?page=1 var dialogTitle = "Please specify "; var defaultAnswer = ""; var stringToSearchFor = app.response("Client ID", dialogTitle, defaultAnswer); //https://acrobatusers.com/tutorials/print/how-save-pdf-acrobat-javascript/ /* Put script title here */ // 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 = "64718"; 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[n], nEnd: pageArray[n], } ); } // remove the first page d.deletePages(0); // https://community.adobe.com/t5/acrobat/how-to-save-as-pdf-form-using-script/td-p/9848947?page=1 // Split Path into an array so it is easy to work with var aMyPath = this.path.split("/"); // Remove old file name aMyPath.pop(); // Add new file name aMyPath.push(stringToSearchFor); // Put path back together and save d.saveAs(aMyPath.join("/")+".pdf"); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM