Skip to main content
May 22, 2019
Répondu

How to get PDF to auto generate new number every time it is opened as long as the field is blank

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!

Ce sujet a été fermé aux réponses.
Meilleure réponse par Test Screen Name

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.

4 commentaires

Legend
May 22, 2019

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.

May 22, 2019

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

May 22, 2019

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

Legend
May 22, 2019

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.

May 22, 2019

What would be the code to generate a number starting at 00001 instead of a random number?

May 22, 2019

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!

Legend
May 22, 2019

Great, that's my third question answered. Please can you answer the other two?

Legend
May 22, 2019

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.

May 22, 2019

May 22, 2019

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