Skip to main content
jctremblay
Community Expert
Community Expert
Oct 5, 2019 2:33 PM UTC
Question

Injecting document script in batch using Action sequence

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):
- - - - -

  1. Create a new Action
  2. Select "More Tools"
  3. Select "Execute JavaScript"
  4. 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]")

This topic is closed to new replies. Start a new post to keep the conversation going.

12 replies

JR Boulay
Community Expert
Community Expert
Oct 6, 2019 4:15 PM UTC

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/

😉

Translate
Acrobate du PDF, InDesigner et Photoshopographe
jctremblay
Community Expert
Community Expert
Oct 6, 2019 5:33 PM UTC
That’s another option. A bit hard to read! 🙂
Translate
Legend
Oct 5, 2019 4:22 PM UTC

if ( x == "1" )
{
this.getfield("z") = "!"
}

becomes
var string ="\
if ( x == \"1\" ) \n\
{ \n\
this.getfield("z") = \"!\" \n\
} \n\
"

Translate
Legend
Oct 5, 2019 4:22 PM UTC

if ( x == "1" )
{
this.getfield("z") = "!"
}

becomes
var string ="\
if ( x == \"1\" ) \n\
{ \n\
this.getfield("z") = \"!\" \n\
} \n\
"

Translate
jctremblay
Community Expert
Community Expert
Oct 5, 2019 4:15 PM UTC

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);

 

Translate
Legend
Oct 5, 2019 4:24 PM UTC
This is a neat trick unless the code has comments (which I'd hope to see in 100 lines!) My approach would work with a simple script (not in Acrobat) to prepare code for insertion with suitable escapes.
Translate
jctremblay
Community Expert
Community Expert
Oct 5, 2019 4:28 PM UTC

If you use single line of comments // it will preserve them.

 

Translate
JR Boulay
Community Expert
Community Expert
Oct 5, 2019 3:16 PM UTC

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 :

https://www.pdfscripting.com/public/programs/downloadsearch.cfm?searchtype=simple&searchmode=cat&keywords=recent&sortby=name&cat=Automation%20Tools

Translate
Acrobate du PDF, InDesigner et Photoshopographe
Legend
Oct 5, 2019 4:17 PM UTC
One way to turn a script into something you can use in a string literal is to use \ escapes in three ways as in this example
Translate
Bernd Alheit
Community Expert
Community Expert
Oct 5, 2019 2:43 PM UTC

You can write your code in one line:

line1; line2; line3; and so on

Translate