Copy link to clipboard
Copied
I am brand new to JavaScripts. I have never done one before. I am familiar with VB in excel and modifying macro's but that is about it.
I have a PDF document that I need a new number generated every time the document is opened as long as the field is blank.
I have this script but have no idea if it is correct or if I am missing something:
if (this.getField("IA-").value == this.getField("IA-").defaultValue) {
this.getField("IA-").value = util.printf("%06d", Math.floor((Math.random() * 1000000) + 1));
}
I want the number to generate IA-001 or IA-002 etc etc in a certain area of the document
I have opened Document JavaScripts and pasted the above as a new script but that doesn't seem to do anything. A screen shot of what I have to do or some assistance with the document would be much appreciated.
Thank you very much!
People use random numbers because having numbers that start at 1 and go up can be difficult or impossible. You have to keep count, somewhere. That's the hard part.
If you keep it in the file, then two copies of the file will have the same series of numbers (you get duplicates).
If you keep it in the file, and the file is NOT SAVED, it just uses the same number again.
If you want to keep it somewhere else there is a world of pain.
Copy link to clipboard
Copied
This code looks as if it updates a field called IA- (including dash). Did you make the field and call it that?
And, please let us know if there are errors in the console.
Finally, where did you paste it? There are lots of places to put scripts.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I know that script is not right. I want it to always have a IA- in front of the generating number.
Example:
IA-001
IA-002 etc.
I also do not want it to generate a number if there is something already in the field to avoid overwriting a number if a form is being modified.
Hope this helps.
Thank you
Copy link to clipboard
Copied
Ok I was referencing the wrong field.
Should be:
if (this.getField("File Reference Number").value == this.getField("File Reference Number").defaultValue) {
this.getField("File Reference Number").value = util.printf("%06d", Math.floor((Math.random() * 1000000) + 1));
}
However how do I get it to start at 001?
It generated a number: 495272?
Thank you
Copy link to clipboard
Copied
I also do not know what you mean by errors in the console, sorry.
Very new to this. I did not create the field but I figured out the field name and got a number to generate. Now I just need to know how to get it to start at sequence 001 or something to that effect.
Thank you
Copy link to clipboard
Copied
Great, that's my third question answered. Please can you answer the other two?
Copy link to clipboard
Copied
Every problem writes messages to the JavaScript console. So it's the first place to look if anything isn't working. But it should also be checked if things seem OK, as an early warning. How to get the console? In Windows, Ctrl+J is the quick way (J for JavaScript so it's easy to remember).
What do you mean by "start at 1"? Your code is generating random numbers of six digits, between 1 and a million.
Copy link to clipboard
Copied
What would be the code to generate a number starting at 00001 instead of a random number?
Copy link to clipboard
Copied
Ok I think I am understanding this now. It is similar to VB in some ways.
So here is my new script:
if (this.getField("File Reference Number").value == this.getField("File Reference Number").defaultValue) {
this.getField("File Reference Number").value = util.printf("%04d", Math.floor((Math.random() * 1000) + 1));
}
4 Decimals is better for what I am using it for and I see the Math.random is what determines the random number however is there a way to start it at 0001? Would it be getRangeValue(01,9999) or something to that effect?
Sorry I am such a noob
Thanks!
Copy link to clipboard
Copied
People use random numbers because having numbers that start at 1 and go up can be difficult or impossible. You have to keep count, somewhere. That's the hard part.
If you keep it in the file, then two copies of the file will have the same series of numbers (you get duplicates).
If you keep it in the file, and the file is NOT SAVED, it just uses the same number again.
If you want to keep it somewhere else there is a world of pain.
Copy link to clipboard
Copied
I kind of figured I was hoping for the impossible to keep it in sequence and if so a reference would be required I am sure and you are right it would be far from "poka yoke" so to speak and there would be a high risk of duplicates. I will keep it to the random generator. I know I kind of answered my own question but you did give me a light bulb moment when you said the code updates a field called IA-. That is when I realized I was referencing the wrong field.
Thank you very much for responding so quickly and helping me to figure this out
Take care
Copy link to clipboard
Copied
One additional Question. What would be the code/script to save the document using the reference as the document name? I also always need it to start with an IA-. Currently the reference is only the number.
Example:
Thank you
Copy link to clipboard
Copied
That requires installing a script on the local machine, or another method of running the code from a "privileged context".
See: https://acrobatusers.com/tutorials/how-save-pdf-acrobat-javascript
Copy link to clipboard
Copied
Sorry, like I said I am still new at this. So there is no function that can run when you select save as it will automatically populate the file name as IA-"the reference"?
If there is could you provide an example of the script?
Copy link to clipboard
Copied
No, it's not possible. What you can do is use a separate prompt window to suggest which name to save it under, but you can't actually populate it into the Save As dialog.
Copy link to clipboard
Copied
So Something like this but modified would not work?:
// Split Path into an array so it is easy to work with
var aMyPath = this.path.split("/");
// Remove old file name
aMyPath.pop();
// Add new file name
aMyPaty.push("NewFileName.pdf");
// Put path back together and save
this.saveAs(aMyPath.join("/"));
Copy link to clipboard
Copied
No. You're welcome to try it for yourself, though...
Copy link to clipboard
Copied
Thank you