Skip to main content
Known Participant
October 22, 2023
Question

JavaScript to combine Two or more files

  • October 22, 2023
  • 2 replies
  • 3080 views

Yes I know this has been a major discussion in the past, but I couldn't find a proper solution, mostrly because in the replies I've read there were links which point to nowhere or expired.

Whatever, I'm trying to figure out a JS to add as action to a button that, basically merge  the document from which I'm working to another document. The document is variable, in fact it depends by a value reported into a specific text field in my document.  I tried to accomplish this using the following Code, but it never worked, it's just opening the other document in a different window:

 

var textValue = this.getField("TEXTFIELD").value;
var filePath = "G:\\Il mio Drive\\@@@@@@@@\\PASSPORT " + textValue + ".pdf";


var externalDoc = app.openDoc(filePath);

if (externalDoc != null) {

this.insertPages({
nPage: this.numPages,
cPath: filePath,
nStart: 0,
nEnd: externalDoc.numPages - 1
});


externalDoc.closeDoc(true);
} else {

app.alert("Impossibile aprire il documento esterno.");
}

 

Any help will be much appreciated

This topic has been closed for replies.

2 replies

Bernd Alheit
Community Expert
Community Expert
October 24, 2023

Try this:

Execute the script in the Javascript console.

Known Participant
October 24, 2023

You mean like when I declare the forms disclosed = true ?

Bernd Alheit
Community Expert
Community Expert
October 24, 2023

Yes

try67
Community Expert
Community Expert
October 22, 2023

Several issues there:

- The file paths needs to be in the following format:

"/G/Il mio Drive/@@@@@@@@/PASSPORT " + textValue + ".pdf";

- You must use the reference to the file you opened, ie. externalDoc, instead of the "this" keyword.

- The file you open must be disclosed for openDoc to return a reference to it.

- You must run both openDoc and insertPages from a privileged context, such as a trusted function in a folder-level script.

- You must save externalDoc before closing it if you want anything you did to it to not go away...

 

Known Participant
October 23, 2023

Thank you for your comment.

Please help me a little bit further here:

- The file paths needs to be in the following format:

"/G/Il mio Drive/@@@@@@@@/PASSPORT " + textValue + ".pdf";

Yes that is the path when I copied from the file, but when it comes to re-writing it in Javascript I had always to use \\ rather than /

- You must use the reference to the file you opened, ie. externalDoc, instead of the "this" keyword.

Please clarify

- The file you open must be disclosed for openDoc to return a reference to it.

The file from which I'm operating is disclosed, but the files I call with openDoc are not and it worked for the simple function to just open them in another window

- You must run both openDoc and insertPages from a privileged context, such as a trusted function in a folder-level script. Please clarify. I had just put both the folders with the files I'm working on it as Trusted paths in Acrobat

- You must save externalDoc before closing it if you want anything you did to it to not go away...

try67
Community Expert
Community Expert
October 23, 2023

- No, you must use the path as I wrote it. With forward slashes, not escaped back-slashes.

- For example, replace this:

this.insertPages({ ...

With:

externalDoc.insertPages({ ...

- If you open a file with openDoc and want to do anything with it, beyond just displaying it on the screen, it must be disclosed.

- Not sure if placing the folders as trusted will be enough, but we can check that later. First fix the other issues and then try it again.