0

I am using the script below to generate a random number using a button. I need assistance in coding said generated number to save and not change once saved, so the saved document can be opened anytime by any user and the number stays the same throughout.

this.getField("Violation Number").value = util.printf("%06d", Math.floor((Math.random() * 100000000) + 1));
Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • have you tried using local storage or app cache ? – Abrar Dec 02 '16 at 03:40
  • @AbrarShariarGalib - this JavaScript code is in a PDF file running in Acrobat, not in a web browser. – Michael Geary Dec 02 '16 at 04:10
  • Can you describe in more detail exactly what you want to have happen? Step by step, who does what and what happens next? – Michael Geary Dec 02 '16 at 04:15
  • You might find this helpful [link](https://acrobatusers.com/tutorials/how-save-pdf-acrobat-javascript) – Abrar Dec 02 '16 at 04:27
  • This js is running in a pdf. Ideally, I would like to have the js run when the document is opened generating the random number, then the number saves or becomes unchangeable after it was generated. Other users will be opening and closing the saved pdf, so once generated the number must stay the same. Thank you for all of your comments. I am an extreme novice, so excuse my lack of knowledge. – J. Wilkerson Dec 02 '16 at 05:04

2 Answers2

0

Maybe you can use the Global js variable and set your random number persistent, something like this:

global.x = 1
global.setPersistent("x", true);

For the exact use have a look in the js helpfile: http://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_AcroJS%2Fglobal_methods.htm&rhsearch=global%20persistent&rhhlterm=global%20persistent&rhsyns=%20

And a tutorial you find here: https://acrobatusers.com/tutorials/global-object-acrobat-javascript

ReFran
  • 897
  • 8
  • 14
0

I know this is an old question, but don't see anyone who marked it as answered.
My suggestion would be to run an if statement like this:

if (this.getField("Violation Number").value.length == 0) {
   this.getField("Violation Number").value = util.printf("%06d", Math.floor((Math.random() * 100000000) + 1));
}

Breaking it up you tell javascript to check the field value's lenght. If there is 0 characters in the field, run your script. If there is more than 0 characters in the field, do nothing.

Luis Paulo Pinto
  • 5,578
  • 4
  • 21
  • 35
THENTIN
  • 1
  • 1