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

Injecting document script in batch using Action sequence

Community Expert ,
Oct 05, 2019 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

Views

994

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
community guidelines
Community Expert ,
Oct 05, 2019 Oct 05, 2019

Copy link to clipboard

Copied

You can write your code in one line:

line1; line2; line3; and so on

Votes

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
community guidelines
Community Expert ,
Oct 05, 2019 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...

Votes

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
community guidelines
LEGEND ,
Oct 05, 2019 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

Votes

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
community guidelines
Community Expert ,
Oct 05, 2019 Oct 05, 2019

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Community Expert ,
Oct 05, 2019 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);

 

Votes

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
community guidelines
LEGEND ,
Oct 05, 2019 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.

Votes

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
community guidelines
Community Expert ,
Oct 05, 2019 Oct 05, 2019

Copy link to clipboard

Copied

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

 

Votes

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
community guidelines
LEGEND ,
Oct 06, 2019 Oct 06, 2019

Copy link to clipboard

Copied

Good solution

Votes

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
community guidelines
LEGEND ,
Oct 05, 2019 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\
"

Votes

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
community guidelines
LEGEND ,
Oct 05, 2019 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\
"

Votes

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
community guidelines
Community Expert ,
Oct 06, 2019 Oct 06, 2019

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/

😉

Votes

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
community guidelines
Community Expert ,
Oct 06, 2019 Oct 06, 2019

Copy link to clipboard

Copied

LATEST
That’s another option. A bit hard to read! 🙂

Votes

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
community guidelines