Copy link to clipboard
Copied
I need a unique invoice number to show in the corner of a form. I was thinking of the best way (maybe the easiest?) to generate a number.. I want the form when opened to automatically create this number. In addition, one the PDF has been filled out and so on, then saved, I dont want that invoice number to regenerate once its opened again for whatever the reason. Does this sound possible?
Copy link to clipboard
Copied
My bad... The code should be:
this.getField("Invoice No").value = util.printf("%06d", Math.floor((Math.random() * 1000000) + 1));
Copy link to clipboard
Copied
Can you share the file, either via a file-sharing website (dropbox, acrobat.com, etc.) or via email?
Copy link to clipboard
Copied
Sure what is your email?
Copy link to clipboard
Copied
try6767 at gmail
Copy link to clipboard
Copied
Hello try67.
How do I get the form to keep the generated number without first saving the file?
Copy link to clipboard
Copied
To save the data in a file (any file), it must be saved.
Copy link to clipboard
Copied
Hmmm. This may have seemed like a strange request but I had an older PDF version with a script which functioned that way. I would close the PDF without saving. On launching the file again it would advance sequentially to the next number.
Sent from my iPhone
Copy link to clipboard
Copied
Then either the file saved itself (using a script), or the data was saved to a global (and persistent) variable and then used when the file got re-opened.
Copy link to clipboard
Copied
I understand what you are saying but respectfully, I was trying to pick your brain for a solution. I'm by no means savvy with script so I need your help.
I could share with you the script that I got from a post a few years back. Hopefully you can determine how it's functions. The post writer had said that Adobe updated the PDF form controls since he created the script.
Sent from my iPad
Copy link to clipboard
Copied
Sure, share away...
Copy link to clipboard
Copied
if (global.count == null) //checks to see if count exists
{
global.count = 1000000 //sets initial count, you can change to suite your needs
global.setPersistent("count",true) //makes the counter a global object so it is persistent data
}
else
global.count++ //increments the the counter by one when the document is opened
var f = this.getField("RECEIPT NUMBER") //creates a variable for the form field where the counter will be displayed
f.value = global.count //sets the form field to equal the counter
I placed the script in the document page properties. I also created a button and placed it in the actions section so I could manually advance the receipt number. As mentioned before, on closing the PDF without saving and then reopening, the new number followed on from the previous number.
The workflow I devised was to print each new invoice so there would be a copy for distribution and or achieve purposes.
I would also like to simplify the output process by printing to image file and hard copy in one function. Perhaps using a "watch folder” setup could take care of the print function.
Thanks for your patience.
Copy link to clipboard
Copied
Yes, this is the global variable approach I've described. It should work, but only if a certain preference is first set by the user in Acrobat/Reader.
They have to un-check the "Enable global object security policy" box under Edit - Preferences - JavaScript for it to work.
Copy link to clipboard
Copied
Yup, that works, you're the man!
Many thanks!
Sent from my iPad
Copy link to clipboard
Copied
Good morning, I've been following this post and it has been extremely helpful in auto numbering my PDF form! I am a novice at scripting, so your script and other's notes have been invaluable. Thank you to all! The script is working wonderfully except that I cannot get it to start at the number that I want. I had it once, then the owners decided to up the number. I would like the numbering to start at 1701000. Following is the script I am using. Any advice is greatly appreciated.
if (this.documentFileName == "2017 Electronic Job Ticket FORM-js.pdf")
if (global.count == null) //checks to see if count exists
{
global.count = 1701000 //sets initial count, you can change to suite your needs
global.setPersistent("count",true) //makes the counter a global object so it is persistent data
}
else
global.count++ //increments the the counter by one when the document is opened
var f = this.getField("Order #") //creates a variable for the form field where the counter will be displayed
f.value = global.count //sets the form field to equal the counter
Copy link to clipboard
Copied
currently the script still remembers the number I somehow initially set.
Copy link to clipboard
Copied
Run this command from the JS Console:
delete global.count;
Copy link to clipboard
Copied
Thank you! Worked perfectly.
Copy link to clipboard
Copied
I have one other issue if anyone has a suggestion. My consecutively numbered form is on a central iMac running OS 10.10.5 that two of us in the department connect to via Mac File Sharing. I created the form from my iMac, but have always saved and worked on it via File Sharing. The numbers change consecutively perfectly for me. I open the original form I created, Save As, with a name reflecting the new number and job name. The next time I open the original form, the job number increased, save as, etc. When either of the other two gals open the form, it does not reflect the time I have opened the form and doesn't increase consecutively. It does for each of them individually, but we all need to access the same form and have the number increase each time. Does that make sense? ANy suggestions appreciated. Following is my script:
if (this.documentFileName == "2017 Electronic Job Ticket FORM-js.pdf")
if (global.count == null) //checks to see if count exists
{
global.count = 1701000 //sets initial count, you can change to suite your needs
global.setPersistent("count",true) //makes the counter a global object so it is persistent data
}
else
global.count++ //increments the the counter by one when the document is opened
var f = this.getField("Order #") //creates a variable for the form field where the counter will be displayed
f.value = global.count //sets the form field to equal the counter
Copy link to clipboard
Copied
In that case you can't use the global variable. You will need to simply increment the field's value by one each time the file is opened.
The code for that would be:
this.getField("Order #").value = Number(this.getField("Order #").valueAsString)+1;
Copy link to clipboard
Copied
Hi try,
I am spawning my 2 page PDF form from the below code
var a =this.getTemplate("p1" ); //using my page 1 template
var b =this.getTemplate("p2" ); //using my page 2 template
a.spawn(1,true, false);
b.spawn(1,true, false);
this.pageNum++;
but I want to increase my one of the field value by 1 on every spawning and this can be done from the above code you gave but I think it will not work because on spawning the field name on new page will change to something else then how we will do it?
Thanks in advance
Copy link to clipboard
Copied
Hi, Thanks For Code Please Ineed reset Count to 1 how can i do it
Regards
Copy link to clipboard
Copied
This thread has been really helpful! Thanks to everyone involved.
However, I'm a little stuck. I have the field in the PDF generating the random number upon document opening, however it keeps changing each time it is opened after that.
I'm using the this.getField("Invoice No").value = util.printf("%06d", Math.floor((Math.random() * 1000000) + 1)); script.
Is there a way of making this script only run if the field is empty?
Ideally the user will open the "master.pdf" which has an empty field (which generates the number upon open), and then fill out other forms, then save as a new name. I can't have it generating new numbers each time it opens after saving.
Any help out there?
Copy link to clipboard
Copied
Sure. Use this code:
if (this.getField("Invoice No").valueAsString=="") {
this.getField("Invoice No").value = util.printf("%06d", Math.floor((Math.random() * 1000000) + 1));
}
Copy link to clipboard
Copied
Thank you so much!
Do you know if there is a better way to generate the number? I've read that it may be better to generate it based on date with milliseconds, as to avoid the possibility of repeated numbers. If so, would you know how to do it?
Copy link to clipboard
Copied
Sure, that's easily done.
Replace the second line of code with this one to achieve it:
this.getField("Invoice No").value = new Date().getTime();
This will generate a number such as: 1488321852191
Copy link to clipboard
Copied
The nice thing about it is that it's not only a unique number, it's also a time-stamp, ie you can use it to know when the file was opened.
However, you have no guarantees that the computer clock is accurate on the user's end...