Adobe Acrobat JavaScript PDF Script Issues: File Matching and Dynamic Retrieval

Joined
Nov 29, 2024
Messages
1
Reaction score
0
I am developing a JavaScript-based PDF script in Adobe Acrobat to insert referenced PDF files into a main document. The script identifies references by searching for "File Reference" text on specific pages and attempts to match these to files in a designated folder. While I have successfully inserted some PDFs, I am facing two key issues:

Certain files are not matched or inserted, even though their names appear to align with the references.
The script is unable to directly retrieve file names from the designated folder, which complicates the matching process.
What I Tried:
I designed a script to parse the main document for "File Reference" text, normalize the extracted references, and compare them with filenames in a predefined array. The script then inserts the matching PDF files at the appropriate locations in the main document.

Expected Outcome:
I expected the script to successfully match all references to the corresponding files in the folder and insert them without issues.

Actual Outcome:
Some files are inserted correctly, but others are not matched despite their filenames appearing to align with the references.
Dynamic retrieval of file names directly from the folder could not be implemented due to limitations in accessing the file system within Adobe Acrobat's JavaScript environment.

Code:
function findMatchingFile(refFileName, folderPath) {
    var normalizedRefName = normalizeFileName(refFileName);

    var fileList = ["notapdf.pdf"];

    for (var i = 0; i < fileList.length; i++) {
        var fileName = fileList[i];
        var normalizedFileName = normalizeFileName(fileName);

        if (normalizedFileName.indexOf(normalizedRefName) !== -1) {
            return folderPath + fileName; 
        }
    }
    return null;
}

function normalizeFileName(fileName) {
    return fileName
        .toLowerCase()
        .replace(/[\[\]\(\)\-]/g, "")
        .replace(/\s+/g, "");
}

function insertReferencedDocs(mainDoc, refFolder) {
    console.println("Starting document insertion from page 19...");

    var numPages = mainDoc.numPages;
    var references = [];

    for (var i = 18; i < numPages; i++) {
        var pageText = getFullPageText(mainDoc, i);
        var fileReference = findFileReference(pageText);

        if (fileReference) {
            references.push({ page: i, file: fileReference });
            console.println("Found 'File Reference' on page " + (i + 1) + ": " + fileReference);
        }
    }

    console.println("Total references found: " + references.length);

    references.forEach(function (ref) {
        var matchedFilePath = findMatchingFile(ref.file, refFolder);
        if (matchedFilePath) {
            try {
                var sourceDoc = app.openDoc(matchedFilePath);
                console.println("Inserting: " + matchedFilePath + " on page " + (ref.page + 1));

                mainDoc.insertPages({
                    nPage: ref.page,
                    cPath: matchedFilePath,
                    nStart: 0,
                    nEnd: sourceDoc.numPages - 1
                });

                sourceDoc.closeDoc();
            } catch (e) {
                console.println("Error inserting: " + matchedFilePath + ". Error message: " + e.message);
            }
        } else {
            console.println("No match found for reference: " + ref.file);
        }
    });

    console.println("Document insertion completed.");
}

function getFullPageText(doc, pageNum) {
    var numWords = doc.getPageNumWords(pageNum);
    var pageText = "";

    for (var i = 0; i < numWords; i++) {
        var word = doc.getPageNthWord(pageNum, i, true);
        if (word) {
            pageText += word + " ";
        }
    }

    return pageText.trim();
}

function findFileReference(text) {
    var words = text.split(" ");
    for (var i = 0; i < words.length - 2; i++) {
        if (words[i] === "File" && words[i + 1] === "Reference") {
            var fileName = words.slice(i + 2).join(" ").split("pdf")[0].trim();
            return fileName;
        }
    }
    return null;
}

insertReferencedDocs(this, "C:/filepath/bla/blabla/");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,056
Messages
2,570,440
Members
47,101
Latest member
DoloresHol

Latest Threads

Top