Copy link to clipboard
Copied
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:
Copy link to clipboard
Copied
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();
});
Copy link to clipboard
Copied
- 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.
Copy link to clipboard
Copied
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();
});
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
You need to pass it as an input parameter when you call the function, like this:
appendPDF(this);
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
- 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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Change the zero in this line to -1 (negative 1)
oDoc.insertPages(0, oResult);

