Skip to main content
Participating Frequently
May 4, 2025
Answered

run JS code with PDF form button

  • May 4, 2025
  • 2 replies
  • 1020 views

I try to create a PDF document in Adobe Acrobat Pro DC with a form button that calls a JS function. The purpose of this function is to open a file picker dialog, and append the selected file's content at the end of the current file (that calls the JS function).

 

I placed following JS code into C:\Program Files\Adobe\Acrobat DC\Acrobat\Javascripts (file is: appendPDF.js):

 

var appendPDF = app.trustedFunction(function () {
    app.beginPriv();
    try {
        var oResult = app.browseForDoc({
            cFilter: "Adobe PDF Files: *.pdf"
        });

        if (oResult && typeof oResult.cPath === "string") {
            // Convert from /C/Users/... to C:\\Users\\... format
            var rawPath = oResult.cPath;
            var windowsPath = rawPath
                .replace(/^\/([A-Z])\//i, "$1:\\")  // Replace leading /C/ with C:\
                .replace(/\//g, "\\");             // Replace remaining / with \

            app.alert("Selected file: " + windowsPath);

            var thisDoc = app.activeDocs[0];
            if (!thisDoc) {
                throw new Error("No active document.");
            }

            thisDoc.insertPages({
                nPage: thisDoc.numPages - 1,
                cPath: windowsPath,
                nStart: 0,
                nEnd: -1
            });

            app.alert("Pages inserted successfully.");
        } else {
            app.alert("No file selected or cPath missing.");
        }
    } catch (e) {
        app.alert("Error: " + e.message);
    }
    app.endPriv();
});

 

Form button is placed in the upper left corner, select trigger is mouseup and action is run a javascript.

Called JS function: appendPDF();

 

File picker dialog box opens, but two error message boxes come up right after selecting a file:

 

Correct answer PDF Automation Station

There's no cFilter parameter for the browseForDoc method.  That's why you're getting Invalid argument type.  IMO you have a lot of unnecessary checking and path splicing.  Try this:

appendPDF = app.trustedFunction(function (oDoc) 
{
app.beginPriv();

var count=0;
try{var oResult=app.browseForDoc().cPath}catch(e)
{app.alert("User cancelled.",1);count++}
if(count==0)
{oDoc.insertPages(oDoc.numPages-1,oResult);}

app.endPriv();
});

2 replies

PDF Automation Station
Community Expert
Community Expert
May 5, 2025

There's no cFilter parameter for the browseForDoc method.  That's why you're getting Invalid argument type.  IMO you have a lot of unnecessary checking and path splicing.  Try this:

appendPDF = app.trustedFunction(function (oDoc) 
{
app.beginPriv();

var count=0;
try{var oResult=app.browseForDoc().cPath}catch(e)
{app.alert("User cancelled.",1);count++}
if(count==0)
{oDoc.insertPages(oDoc.numPages-1,oResult);}

app.endPriv();
});
adam_2770Author
Participating Frequently
May 5, 2025

The dialog box opes but oDoc is not defined and it throws error (TypeError: oDoc is undefined).

I m dummy to JS. Where should oDoc be declared?

try67
Community Expert
Community Expert
May 5, 2025

You need to pass it as an input parameter when you call the function, like this:

appendPDF(this);

try67
Community Expert
Community Expert
May 4, 2025

- The first message is not an error message. It's the alert you created yourself in this line:

app.alert("Selected file: " + windowsPath);

- I recommend you remove the try-catch clause surrounding your code (or at least modify the alert to include more information from the error object) so that you could see which line is causing the error, exactly. That would help you debug the problem.

- Instead of using the activeDocs array (which is not reliable, since you can't know if the file you're working on is the first one in it), just pass a reference to that Document object when calling the function, using the "this" keyword.