Quantcast
Channel: PDFCreator English - pdfforge forums
Viewing all articles
Browse latest Browse all 1241

Batch Find Text and Extract Pages to New PDF

$
0
0

Dear Forum,

I would like to find text and phrases within PDF files and extract pages containing those phrases to new PDF documents. It seems like I should be able to do that with PDF Creator. I have some JavaScript which should be able to perform the work, but I can't get it to function because I can't seem to initialize the correct variables. I was unable to find a knowledgebase on your site containing examples or a JavaScript language reference.

Can you point me to any articles you have, or help me work out my code to perform the required actions? Any help is appreciated! Thanks!

// Set the word to search for here
var sWord = "tillable";

// Source document = current document
var sd = this;
var nWords, currWord, fp, fpa = [], nd;
var fn = sd.documentFileName.replace(/\.pdf$/i, "");

// Loop through the pages
for (var i = 0; i < sd.numPages; i += 1) {

    // Get the number of words on the page
    nWords = sd.getPageNumWords(i);

    // Loop through the words on the page
    for (var j = 0; j < nWords; j += 1) {

        // Get the current word
        currWord = sd.getPageNthWord(i, j);

        if (currWord === sWord) {

            // Extract the current page to a new file
            fp = fn + "_" + i + ".pdf";
            fpa.push(fp);
            sd.extractPages({nStart: i, nEnd: i, cPath: fp});

            // Stop searching this page
            break;
        }
    }
}

// Combine the individual pages into one PDF
if (fpa.length) {

    // Open the document that's the first extracted page
    nd = app.openDoc({cPath: fpa[0], oDoc: sd});

    // Append any other pages that were extracted
    if (fpa.length > 1) {

        for (var i = 1; i < fpa.length; i += 1) {
            nd.insertPages({nPage: i - 1, cPath: fpa[i], nStart: 0, nEnd: 0});
        }
    }

    // Save to a new document and close this one
    nd.saveAs({cPath: fn + "_searched.pdf"});
    nd.closeDoc({bNoSave: true});

}

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 1241

Trending Articles