Skip to main content
Nov 18, 2013 5:24 AM UTC
Answered

How to create sequential number field?

I have created a pdf file and I'd like to have a field with sequential numbers.

For instance, if I open my pdf now and it has number 1 in the field, 'd like to have number 2 in the field the next time that I open the file, and so on...

try67
try67Correct answer
Community Expert
Nov 18, 2013 9:08 AM UTC

Go to Tools - JavaScript - Document JavaScripts and create a new script with the following code (remove any default code that's inside the window when you first open it):

var f = this.getField("Serial Number");

f.value = Number(f.value)+1;

f.defaultValue = f.value;

Of course, you'll might need to adjust the field name in the first line of code.

70 replies

Participating Frequently
Dec 12, 2022 4:04 AM UTC

I have just added your java script to my form, but I would like to know if it can be modified to add a fixed numeric at the begin of the sequential number that will be created each time the form is open. For example, I would like to add 23 - to the beginning to signify the year 2023. Is there a way I can do this so that the 23 - is fixed in the code somewhere, and then I can go in and change it for 2024?

 

Thanks,

Nick

Translate
try67
Community Expert
Community Expert
Dec 12, 2022 8:30 AM UTC

Yes, like this:

 

var prefix = "23-";
if (this.documentFileName=="Original file name.pdf") {
	var num = this.getField("Text2");
	num.value = prefix  + Number(num.valueAsString.replace(prefix, ""))+1;
	num.defaultValue = num.valueAsString;
}
Translate
Participating Frequently
Dec 12, 2022 1:00 PM UTC

I copied this Java Script and changed the one I already had, and it is not changing anything. Now the number won't increase and the 23 - is not showing up.

 

Translate
Participant
Oct 8, 2021 7:38 PM UTC

I have the sequential number field using this coding: 

var num = this.getField("Text2");
num.value = Number(num.value)+1;
num.defaultValue = num.value;

 

BUT, I actually don't want the number to change after the doc is filled out. It's a work order so once the doc is opened, new number is generated, the rest of the form is completed and it's saved. I need this document to keep this same work order number idefinitely. 

 

Is it possible to have a "Master" doc that generates sequential numbers but then once it's saved as a new name, the number stays the same? So employees can open a single Master doc when they get ready to provide an estimate and each time the work order generates a new +1 number. Then, once they save and rename the document, the number sequence no longer changes. 

Translate
try67
Community Expert
Community Expert
Oct 8, 2021 8:03 PM UTC

The best way to do that is to set the "master" file (and the folder where it's located) as read-only.

If you want to make it dependent on the file name, though, that's possible too.

Adjust the code like this:

 

if (this.documentFileName=="Original file name.pdf") {
	var num = this.getField("Text2");
	num.value = Number(num.value)+1;
	num.defaultValue = num.value;
}
Translate
russb73725712
Known Participant
Jul 17, 2020 2:03 AM UTC

I can get the sequential numbering to work if I keep using the last form that was saved.

The master form does not update. 
Is there a way for the master form to update each time you open it?

I'm opening master form/adding my data/then save as with new file name/close tab/open original master form and number is the same.

Need a little help.

Translate
try67
Community Expert
Community Expert
Jul 17, 2020 9:11 AM UTC

Save it over the "master form", then, not under a new name...

Translate
russb73725712
Known Participant
Jul 17, 2020 1:23 PM UTC

I was able to get it to work that way, but that seems counter intuitive. I don't want to have to rename the file after everytime.

There's gotta be another way?

I've seen these global object scripts, that apparently works when using just your own computer, but haven't had any success, although, I'm probably doing it wrong. 

I tried to add a required one time click button to add +1 to current number, but not sure how to create a "one time" click funcion.

The only thing that I was able to make work is use the date and time to create a unique number, but then it's not really sequential. 

I'm hoping to make the sequential number work somehow. 
I'll keep trying. 

 

 

 

 

Translate
francj
Participant
Aug 15, 2019 11:53 AM UTC

Very usefull for me - thanks!

How can I modify script to do NOTHING, when field "Serial Number" is empty?

Translate
try67
Community Expert
Community Expert
Aug 15, 2019 5:01 PM UTC

Change it to this:

var f = this.getField("Serial Number");

if (f.valueAsString!="") {

    f.value = Number(f.value)+1;

    f.defaultValue = f.value;

}

Translate
francj
Participant
Aug 21, 2019 9:47 AM UTC

Thanks for Your reply, I used very similar code (from my friend):

var f = this.getField("Serial-Number");

if (f.value != "" && f.value != null) {

    f.value = Number(f.value)+1;

    f.defaultValue = f.value;

}

Works fine.

By the way, how can I tag code as “code“ :-), like You in Your last post?

Translate
Krizzzle
Participant
Nov 29, 2017 4:57 PM UTC

I am trying to do something similar, but more difficult. I need to print a 4 page PDF, it has a number field with a 3 digit number in the corner of the first page. I need the number to increase every time it prints, however, I print many copies at once, sometimes 50 to 100. Is there a way to set it to print that many copies and increase that number after each copy? Ive been trying to figure this out for about a year now, if anyone could help, please let me know.

Translate
Thom Parker
Community Expert
Community Expert
Nov 29, 2017 5:28 PM UTC

If you tell the printer to print X copies, then it handles the printing, outside of Acrobat. The only way to modify a value on the PDF is to control the printing yourself. A script would do this easily from a Folder level script, Command Script, or the Console window.

Contact me if you'd like such a script written.

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Krizzzle
Participant
Nov 29, 2017 8:06 PM UTC

Well that would solve all my problems if I knew how to print outside of acrobat. Im using a Mac and Ive never printed a document outside of its program. My only other issue is being able to set print settings. Its not just a plain paper document, its an exam. Its printed on 11 x 17 cardstock then folded by the printer, so I would need to incorporate those print settings as well. Not sure if its doable, but I will keep trying to find a solution to this problem. I appreciate the response.

Translate
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Nov 18, 2013 9:08 AM UTC

Go to Tools - JavaScript - Document JavaScripts and create a new script with the following code (remove any default code that's inside the window when you first open it):

var f = this.getField("Serial Number");

f.value = Number(f.value)+1;

f.defaultValue = f.value;

Of course, you'll might need to adjust the field name in the first line of code.

Translate
Nov 18, 2013 9:50 AM UTC

Thank you so much. It worked.

Translate