javascript тройные переменные 'for-loop', коррелированные результаты из pdf - PullRequest
0 голосов
/ 27 сентября 2019

Я запускаю последовательность javascript внутри Adobe 9, чтобы извлечь некоторую финансовую информацию из PDF.Для этого я использую тройной вложенный цикл;однако данные возвращаются некоррелированными и повторяются.

Вот некоторые примеры данных, которые я извлекаю из PDF:

ProcDate: 2016/01/06
AccountNum: 132686101827
CheckAmt: 2,600.53
SerialNum: 3102
MflmSeqNum: 2638611584

ProcDate: 2016/01/08
AccountNum: 132686101827
CheckAmt: 35.24
SerialNum: 459
MflmSeqNum: 2638611585

ProcDate: 2016/02/01
AccountNum: 132686101827
CheckAmt: 1,754.28
SerialNum: 3104
MflmSeqNum: 2638611586

Ожидаемый результат будет, что я получу это в форматечто я могу экспортировать в текст, а затем импортировать в Excel:

2016/01/06 , 2,600.53 , 3102
2016/01/08 , 35.24 , 459
2016/02/01 , 1,754.28 , 3104

Однако я получаю следующее:

2016/01/06 , 2,600.53 , 3102
2016/01/06 , 2,600.53 , 3102
2016/01106 , 35.24 , 459
2016/01/06 , 35.24 , 3102
2016101/06 , 2,600.53 , 459
2016/01106 , 2,600.53 , 3102
2016/01/06 , 35.24 , 459
2016/01106 , 35.24 , 3102

Полный повторов, дубликатов и чисел не соотносятся скаждый блок данных.Код, который я использую для этого, приведен ниже - я в основном изменил код с этого сайта и добавил несколько циклов ( Образцы сценариев Acrobat Javascript ):

/* Extract ISBN numbers From the Document */
// This script will scan all pages of the input document
// and extract valid ISBN numbers into new PDF document.
// Output PDF document will be placed in the same folder
// as input. The name of the output document will be:
// Original filename + "_Extracted_ISBN"
// Visit www.evermap.com for more useful JavaScript samples.

// This is a combination of strict and relaxed versions of ISBN number format
var reCHKDT=/ProcDate:(.+? )/g;
var reCHKAMT=/CheckAmt:(.+? )/g;
var reCHKNUM=/SerialNum:(.+? )/g;

var strExt = "_AMDD_Check_Data.pdf";
var strIntro = "Check data numbers extracted from document: ";
var strFinal = "Total number of checks processed: " ;

ExtractFromDocument(reCHKDT,reCHKAMT,reCHKNUM,strExt,strIntro,strFinal);

function ExtractFromDocument(reMatch, reMatchAmt, reMatchNum, strFileExt, strMessage1, strMessage2)
{
var chWord, numWords;

// construct filename for output document
var filename = this.path.replace(/\.pdf$/, strFileExt);

// create a report document
try {
    var ReportDoc = new Report();
    var Out = new Object(); // array where we will collect all our emails before outputing them

    ReportDoc.writeText(strMessage1 + this.path);
    ReportDoc.divide(1);      // draw a horizontal divider
    ReportDoc.writeText(" "); // write a blank line to output

    for (var i = 0; i < this.numPages; i++)
    {
        numWords = this.getPageNumWords(i);
        var PageText = "";
        for (var j = 0; j < numWords; j++) {
            var word = this.getPageNthWord(i,j,false);
            PageText += word;
            }

        var strMatches = PageText.match(reMatch);
        if (strMatches == null) continue;

    for (var o = 0; o < this.numPages; o++)
    {
        numWordsAmt = this.getPageNumWords(o);
        var PageTextAmt = "";
        for (var k = 0; k < numWordsAmt; k++) {
            var wordAmt = this.getPageNthWord(o,k,false);
            PageTextAmt += wordAmt;
            }

        var strMatchesAmt = PageTextAmt.match(reMatchAmt);
        if (strMatches == null) continue;

    for (var p = 0; p < this.numPages; p++)
    {
        numWordsNum = this.getPageNumWords(p);
        var PageTextNum = "";
        for (var l = 0; l < numWordsNum; l++) {
            var wordNum = this.getPageNthWord(p,l,false);
            PageTextNum += wordNum;
            }

        var strMatchesNum = PageTextNum.match(reMatchNum);
        if (strMatchesAmt == null) continue;

        // now output matches into report document
        for (j = 0; j < strMatches.length; j++) {
                for (k = 0; k < strMatchesAmt.length; k++) {
                    for (l = 0; l < strMatchesNum.length; l++) {
            Out[strMatches[j].replace("ProcDate: ", "")+" , "+strMatchesAmt[k].replace("CheckAmt: ", "")+" , "+strMatchesNum[l].replace("SerialNum: ", "")] = true; // store email as a property name
                }
            }
        }
        }
    }
}
    var nTotal = 0;
    for (var prop in Out) 
    {
        ReportDoc.writeText(prop);
        nTotal++;
    }

    ReportDoc.writeText(" "); // output extra blank line
    ReportDoc.divide(1); // draw a horizontal divider
    ReportDoc.writeText(strMessage2 + nTotal);

    // save report to a document
    ReportDoc.save(
        {
        cDIPath: filename
        });

}
catch(e)
{
app.alert("Processing error: "+e)
}

} // end of the function
...