Skip to main content
LisaB1311
Participating Frequently
March 28, 2017
Question

Java Script app.doc.save()

  • March 28, 2017
  • 3 replies
  • 2092 views

I put the following java script in my PDF, but it doesn't work.

There is a mistake in the marked line..

Can anybody help me?

allgood();

function allgood()

{

var mel;

mel=""

for (var i=0;i<this.numFields;i++)

{

  var fName=this.getNthFieldName(i);

  var f=this.getField(fName);

if ((f.type != "button") && f.required && (f.value.length<1))

  {

   mel=mel + fName + ", ";

  }

}

if (mel==""){

app.doc.save();

}

else

{app.alert("Please fill all required fields before saving!");

}

}

This topic has been closed for replies.

3 replies

Legend
March 28, 2017

There's a slightly bigger question here, which is: what made you think that app.doc.save was the right thing to do? This is worth pursuing. I'm guessing one of two things: you found this in a sample for some other flavour of JavaScript; or you guessed. Some languages will give in easily to this approach, but for Acrobat JavaScript you need the documentation. Not only because of finding the method names but for the many complex notes and security warnings which will make this sort of task more painful than you can imagine.

Specific things to watch out for.


* app doesn't have a "doc" property, because you can have multiple documents open. There is app.activeDocs, but note the complex limitations. In some methods, not others, you can use "this" for the current document. Read up on "this"
* document doesn't have a "save" method.
* writing files from JavaScript is a security nightmare, so there are lot of complex restrictions on the use of saveAs.

try67
Community Expert
Community Expert
March 29, 2017

If you want to save the file silently you need to run the code from a privileged context, such as a folder-level script.

If you just want to prompt the user to save it then you can use this command instead of saveAs:

app.execMenuItem("SaveAs");

LisaB1311
LisaB1311Author
Participating Frequently
March 28, 2017

with your idea the following mistake is shown....

Bernd Alheit
Community Expert
Community Expert
March 28, 2017

What do you use?

Bernd Alheit
Community Expert
Community Expert
March 28, 2017

You can use this.saveAs( ... ),

Read the documentation in the Acrobat JavaScript Reference.