Skip to main content
jctremblay
Community Expert
Community Expert
October 5, 2019
Question

Injecting document script in batch using Action sequence

  • October 5, 2019
  • 6 replies
  • 1747 views

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 has been closed for replies.

6 replies

JR Boulay
Community Expert
Community Expert
October 6, 2019

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 Photoshopographe
jctremblay
Community Expert
Community Expert
October 6, 2019
That’s another option. A bit hard to read! 🙂
Legend
October 5, 2019

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

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

Legend
October 5, 2019

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

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

jctremblay
Community Expert
Community Expert
October 5, 2019

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

 

Legend
October 5, 2019
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.
jctremblay
Community Expert
Community Expert
October 5, 2019

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

 

JR Boulay
Community Expert
Community Expert
October 5, 2019

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

Acrobate du PDF, InDesigner et Photoshopographe
Legend
October 5, 2019
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
Bernd Alheit
Community Expert
Community Expert
October 5, 2019

You can write your code in one line:

line1; line2; line3; and so on