Injecting document script in batch using Action sequence

Oct 05, 2019

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

  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]")

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms

Views

508

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Injecting document script in batch using Action sequence

Oct 05, 2019

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

  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]")

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms

Views

509

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 05, 2019 0
Oct 05, 2019

Copy link to clipboard

Copied

You can write your code in one line:

line1; line2; line3; and so on

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 05, 2019 0
Oct 05, 2019

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 :

https://www.pdfscripting.com/public/programs/downloadsearch.cfm?searchtype=simple&searchmode=cat&key...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 05, 2019 1
Oct 05, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 05, 2019 0
Oct 05, 2019

Copy link to clipboard

Copied

Merci pour le lien vers "JavaScript to Text Converter"... très utile aussi!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 05, 2019 0
Oct 05, 2019

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

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 05, 2019 0
Oct 05, 2019

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 05, 2019 0
Oct 05, 2019

Copy link to clipboard

Copied

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

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 05, 2019 0
Oct 06, 2019

Copy link to clipboard

Copied

Good solution

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 06, 2019 0
Oct 05, 2019

Copy link to clipboard

Copied

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

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 05, 2019 0
Oct 05, 2019

Copy link to clipboard

Copied

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

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 05, 2019 0