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

JavaScript to combine Two or more files

Explorer ,
Oct 22, 2023 Oct 22, 2023

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

TOPICS
How to , JavaScript , Modern Acrobat , PDF , PDF forms
3.2K
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 ,
Oct 22, 2023 Oct 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...

 

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
Explorer ,
Oct 23, 2023 Oct 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...

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 ,
Oct 23, 2023 Oct 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.

 

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
Explorer ,
Oct 24, 2023 Oct 24, 2023

I did the changes you suggested but I obtain a different result. It just skips to another document open already

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 ,
Oct 24, 2023 Oct 24, 2023

When you open a file it will gain focus, yes, that is normal.

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
Explorer ,
Oct 24, 2023 Oct 24, 2023

But that is not the file supposed to  open, it's another random form that I had opened already for other purposes

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 ,
Oct 24, 2023 Oct 24, 2023

That doesn't make sense. Files don't just open of their own accord.

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
Explorer ,
Oct 24, 2023 Oct 24, 2023

It doesn't, I'm just saying the form isn't opening or merging with the form requested, it simply shift to another form open but I don't think it's because it's setting the focus to the other form, it just doesn't do what I'm hoping to do. While I do thank you for your patience, May I simply ask to provide with a draft of the Code as you'd write it ? 

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 ,
Oct 24, 2023 Oct 24, 2023

Try this:

Execute the script in the Javascript console.

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
Explorer ,
Oct 24, 2023 Oct 24, 2023

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

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 ,
Oct 24, 2023 Oct 24, 2023

Yes

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
Explorer ,
Oct 31, 2023 Oct 31, 2023

I've not been able to do it. However I have this Code now, which is not working either. 

RaiseError: Si è verificato un errore in un file.


var shipsName = this.getField("SHIPSNAME").value;


var path = "G:\\@@@@\\@@@\\" +shipsName;
var files = {
"Check Box1": path+ "\\COR.pdf",
"Check Box2": path+ "\\CREW.pdf",
"Check Box3": path+ "\\GUEST.pdf"
};


for (var checkbox in files) {
if (this.getField(checkbox).value === "Yes") {
this.insertPages({
nPage: this.numPages - 1,

cPath: files[checkbox]
});
}

It gives me this kind of mistake

Doc.insertPages:19:Field Button4:Mouse Up
===> Si è verificato un errore in un file.

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 ,
Oct 31, 2023 Oct 31, 2023

- You did not adjust the file paths as I've suggested.

- You've declared a variable which has the same name as a built-in property of the Document object (path). That will cause a conflict. Rename it to something else.

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
Explorer ,
Oct 31, 2023 Oct 31, 2023

Like this? 
var shipsName = this.getField("SHIPSNAME").value;

var pat = "G:/________/_________/" +shipsName;
var files = {
"Check Box1": pat+ "/COR.pdf",
"Check Box2": pat+ "/CREW.pdf",
"Check Box3": pat+ "/GUEST.pdf"
};

for (var checkbox in files) {
if (this.getField(checkbox).value === "Yes") {
this.insertPages({
cPath: files[checkbox], nPage: -1});

}
}

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 ,
Oct 31, 2023 Oct 31, 2023

Not quite. See the example in my first reply.

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
Explorer ,
Oct 31, 2023 Oct 31, 2023

Anyaway I'm still having issue due to "Raised Error etc.etc.". I used a simple Code this.insertPages with other forms from the Console and it worked. SO I tried to make my code a trusted function, but it didn't work either

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 ,
Oct 31, 2023 Oct 31, 2023

Your code doesn't include any references to a trusted function. You can't use it in its current form from a button. It has to go through a trusted function, located in a folder-level script file.

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
Explorer ,
Nov 04, 2023 Nov 04, 2023
LATEST

At last I found the way: 1. Create the trusted function and put the entire JS into the Javascript Folder 2. change settings in Acrobat both regarding advanced security and Javascript 3. Recall the trusted function through a simple button that I needed to have into my Doc.

 

 

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