Injecting document script in batch using Action sequence
Copy link to clipboard
Copied
I have a bunch of PDF that I need to add a long document javascript in them. I want to use an action to inject it to a bucnh of PDF.
I have found this steps on StackOverflow (provided Joel Geraci):
- - - - -
- Create a new Action
- Select "More Tools"
- Select "Execute JavaScript"
- The script your action runs will need to inject the JavaScript that you want the PDF to run as a string. You'll need to give it a name ("init" in this case) so it appears in the "Document JavaScripts" list. So it will look like this...
this.addScript("init", "app.alert('Hello World');")
- - - - -
Works perfectly on a small code to inject. But I can't figure out how to us ethe same steps to includes a long multiline javascript. I have try a few things like defining a variable but it keep failing.
How can I write a long multiline code to be injected using the: this.addScript("init", "[code here]")
Copy link to clipboard
Copied
You can write your code in one line:
line1; line2; line3; and so on
Copy link to clipboard
Copied
Bonjour
En plus d'écrire le script en une seule ligne, il faut aussi échapper (escape) les caractères qui servent de balises à JavaScript, à commencer par les guillemets.
Par exemple ce script :
if (champPrefix == "2") {
this.getField("2N Fonds" + numRangee).value = datarr[event.changeEx][1];
this.getField("2CS" + numRangee).value = datarr[event.changeEx][2];
this.getField("2FondsComposition" + numRangee).value = datarr[event.changeEx][3];
this.getField("2FondsRisque" + numRangee).value = datarr[event.changeEx][4];
this.getField("2FondsDuree" + numRangee).value = datarr[event.changeEx][5];
}
else {
this.getField("N Fonds" + numRangee).value = datarr[event.changeEx][1];
this.getField("CS" + numRangee).value = datarr[event.changeEx][2];
this.getField("FondsComposition" + numRangee).value = datarr[event.changeEx][3];
this.getField("FondsRisque" + numRangee).value = datarr[event.changeEx][4];
this.getField("FondsDuree" + numRangee).value = datarr[event.changeEx][5];
}
Doit s'écrire ainsi pour tenir en une seule chaine de caractères (string) :
"if (champPrefix == \"2\") {this.getField(\"2N Fonds\" + numRangee).value = datarr[event.changeEx][1];this.getField(\"2CS\" + numRangee).value = datarr[event.changeEx][2];this.getField(\"2FondsComposition\" + numRangee).value = datarr[event.changeEx][3];this.getField(\"2FondsRisque\" + numRangee).value = datarr[event.changeEx][4];this.getField(\"2FondsDuree\" + numRangee).value = datarr[event.changeEx][5];}else {this.getField(\"N Fonds\" + numRangee).value = datarr[event.changeEx][1];this.getField(\"CS\" + numRangee).value = datarr[event.changeEx][2];this.getField(\"FondsComposition\" + numRangee).value = datarr[event.changeEx][3];this.getField(\"FondsRisque\" + numRangee).value = datarr[event.changeEx][4];this.getField(\"FondsDuree\" + numRangee).value = datarr[event.changeEx][5];}"
Il existe un utilitaire "JavaScript to Text Converter" dédié à cet usage, il est disponible sur cette page :
Acrobate du PDF, InDesigner et Photoshoptographe
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Thanks folks!
But with a javascript of more than 100 lines of code, that's not fun. 🙂
I have found a simple way of doing it and it's working really well. You can wrap your multiline code into a comment string like so...
var cScript = function(){/*
your multiline of code;
your multiline of code;
your multiline of code;
your multiline of code;
*/}.toString().slice(15,-3);
Copy link to clipboard
Copied
Copy link to clipboard
Copied
If you use single line of comments // it will preserve them.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
if ( x == "1" )
{
this.getfield("z") = "!"
}
becomes
var string ="\
if ( x == \"1\" ) \n\
{ \n\
this.getfield("z") = \"!\" \n\
} \n\
"
Copy link to clipboard
Copied
if ( x == "1" )
{
this.getfield("z") = "!"
}
becomes
var string ="\
if ( x == \"1\" ) \n\
{ \n\
this.getfield("z") = \"!\" \n\
} \n\
"
Copy link to clipboard
Copied
Maybe you should try to minify your script, this will reduce it as a single line.
I often use this site, just copy and paste: https://javascript-minifier.com/
😉
Acrobate du PDF, InDesigner et Photoshoptographe
Copy link to clipboard
Copied

