Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

run JS code with PDF form button

Community Beginner ,
May 04, 2025 May 04, 2025

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:

 

Screenshot 2025-05-04 183702.pngexpand imageScreenshot 2025-05-04 183726.pngexpand image

TOPICS
Edit and convert PDFs , How to , JavaScript , PDF forms
263
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
May 04, 2025 May 04, 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();
});

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 04, 2025 May 04, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 04, 2025 May 04, 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();
});
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 04, 2025 May 04, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 05, 2025 May 05, 2025

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

appendPDF(this);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 05, 2025 May 05, 2025

Thanks, it works. Two things came to my mind. Is the inserted content revertable? With edit-undo it s not possible cause a script modified the content in the background. The other concern is that script cannot insert content from an e-signed, certified PDF document (This operation is not permitted). Is there a workaround for that?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 05, 2025 May 05, 2025

- It's reversible in the sense that you can delete the pages you inserted using a script, but not via Undo.

You have to use the deletePages command, or close the file without saving it.

- No. You can't insert pages from a signed file, not via script nor manually.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 06, 2025 May 06, 2025

I tried to modify the code to insert the selected file's content before the actual document's first page.

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

    try {
        var oResult = app.browseForDoc().cPath;

        oDoc.insertPages(0, oResult);

    } catch (e) {
        app.alert("User cancelled or error occurred:\n" + e.toString(), 1);
    }

    app.endPriv();
});

However, it appends selected file's content after the last page, instead of appending them/it before the first one of the actual document

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 06, 2025 May 06, 2025
LATEST

Change the zero in this line to -1 (negative 1)

oDoc.insertPages(0, oResult);

  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines