首页 > 解决方案 > 基于 Acrobat Pro DC Java Script 中的匹配字符串提取页面

问题描述

我需要从具有匹配字符串的 PDF 文档中提取页面,即 Acrobat 创建一个包含所有页面的新文件,在其中找到我在 CSV 或 xlsx 文件中的字符串

这是一个示例 PDF 文件,我只需要具有以下两个字符串的页面...

  1. 销售员

我在谷歌搜索时发现了以下代码,但它只搜索一个字符串并创建一个与该字符串匹配的新页面文件。虽然我需要搜索多个字符串并且只需要一个文件。有什么想法请...

// 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 = "Test";

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

我假设将添加一些代码来加载 CSV/XLSX 文件和一个 FOR/WHILE 循环来搜索该 PDF 文件中的所有字符串并存储它们的页码,然后使用所有这些页码创建一个新文件。

标签: javascriptpdfextractacrobat

解决方案


我在这里找到了这个问题的解决方案。在这个网站上,我下载了执行上述操作的 Adob​​e Action 文件。它做了一件额外的事情,即突出显示它在文件中找到的不打扰我的文本。


推荐阅读