Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to create sequential number field?

Guest
Nov 17, 2013 Nov 17, 2013

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...

59.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Nov 18, 2013 Nov 18, 2013

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.

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 18, 2013 Nov 18, 2013

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 18, 2013 Nov 18, 2013

Thank you so much. It worked.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 02, 2014 Apr 02, 2014

This solution works great if you save the form each time after the new number count is generated.

Is there a way to have a form do a +1 each time it is opened without having to save the form?

I want to be able to give a customer a bill of lading and each time they open the blank form it generates a new auto BOL number that is +1 from the last time they opened it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 02, 2014 Apr 02, 2014

How do you want the serial number field to act if the form has been assigned a serial number?

There are a lot of issues that need to be worked out for this to work well.

If the customer has it on his/her own machine how do you control the numbers across all your customers?

Will the form always be open on a machine that is always connected to the internet?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 02, 2014 Apr 02, 2014

It will be a unique identifier number for that document whether printed, saved or emailed.

It is a pdf form with submit buttons, so the form information can be submitted via email as well as saved to the users computer if they wish.

If the number happens to reset with a new user on a different computer that is fine. I have decided that each user will have another field to enter their initials or a number unique only to them that will prevent confusion on duplicate numbers. For example: AB4583 (hard coded #)-______ (unique number propagates here) - _____ (User enters their initials or # here)

The primary goals is for each individual user to be able to reopen the same blank form and not see the same number twice.

I read about global objects but the document was a little more advanced than my skill level and I was not sure how to proceed with that set up. It was also a fairly old article.

Thank you

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 06, 2016 Jul 06, 2016

The script works great when opening the file. I am looking for a way to do a +1 every time the document is printed. Is there a way to do that?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 06, 2016 Jul 06, 2016

Move the code to the document's Will Print event, which you can find under Tools - JavaScript - Set Document Actions.

Edit: If you want the number to change after the file is printed use the Did Print event in the same location...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 25, 2025 Jul 25, 2025

Hi, 

Firstly, thank you for all the brilliant information and your help. I am trying to achieve something similar for my document. I have a master file that I need to be numberefd sequentially everytime it is printed. I have achieved this using the code you have shared here and it works perfect. However, this works best when the file is saved after each print. If the file is not saved, the sequential print number is always default value + increment (i.e., 0+1). Is it possible to generate next sequential number everytime the file is printed without the need to save file each time? The master file sits on a shared network folder and accessible to a bunch of people who will print it. I can password protect the master file if required. Please let me know if it is possible to achieve it? Thank you in advance! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 27, 2025 Jul 27, 2025

The problem with a sequential number is maintaining the previous value. If the value is stored on the document, then only that specific document will produce true unique serial numbers. Copies of that document will not be unique. If the number is stored on the system, then only users of that system will have unique sequential numbers. If the number is on the document on a server, then  users must always save the document back, and can only use it when no one else has the document open.   

 

The solution for a universal unique sequential number is to create a number server.  The number on the server is stored in a database that can only be accessed by one user at a time.  The PDF accesses the server with a submit operation. 

 

A simpler version of this is to store the number in a PDF file on the shared network folder. A script on the user document will open the PDF, increment the number and then save and close the number PDF.  This way the saving is done automatically and doesn't depend on the users. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 28, 2025 Jul 28, 2025

Hi Thom, thank you very much for detailed explanation, much appreciated. Could I please ask your help in achieving this auto saving on a shared network folder that doesn't depend on the users? How do I acheive this in practice? What script should I be referring to and how do I add it to the document? Thank you very much! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2025 Jul 28, 2025

There are multiple parts to making this process work. Each users system will need to be setup for it.

 

Since the number is incremented on Print, the script is placed in the "WillPrint" script on the PDF that will display the sequential number. . 

But the first thing to do is to create the PDF where the number is stored. Call it the "Number PDF". In the simplest case this is a one page, blank, PDF containing a single form field.  Call it "SeqNum".  If you want to capture other information, such as the last date/time used, user name, or anything else, then you'll need more fields. It also might help if there was some explanatory text on the page.   And finally, add this line of code to a document level script.

this.disclosed = true;

It's also helpful if the Number PDF contains a document level function for performing the necessary increment opertation. 

For example:

function GetSeqNum(){

     var nSeq = Number(this.getField("SeqNum").value) + 1;

     this.getField("SeqNum").value = nSeq;

    return nSeq;

}

This is just an example. The function could be expanded to include some validation and to store other information. 

 

Next, add a WillPrint script to the target PDF form. This script opens the Number PDF from a hard coded location, as a hidden file. Acquires the number using the "GetSeqNum()" function, then saves and closes the file. 

This is were it gets tricky. Saving a PDF requires a privileged context.  There are a few ways to add privilege, but the simplest is to add a special folder level Acrobat script to each user's system. You can read about it here:

https://www.pdfscripting.com/public/How-to-Save-a-PDF-2.cfm

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 05, 2025 Aug 05, 2025

Hi Thom, 

Thank you again for your guidance. I have a couple of more follow up questions on your previous guidance. I tried creating the 'Number PDF' and added the following sciprts to the 'document will print' option -

this.disclosed = true;

function GetSeqNum(){

     var nSeq = Number(this.getField("SeqNum").value) + 1;

     this.getField("SeqNum").value = nSeq;

    return nSeq;

}

1. I want to check if this script should be added to the 'document will print' option or not? 

2. In the target PDF what script should I add to be able to get the seq number incremnented, printed and saved from 'Number PDF'?

Both 'Number PDF' and 'Target PDF' are in the same networked folder. 

Apologoes for these questions, I have no background/experience of java scripts or coding for that matter, I appreciate your time on this immensly! Thank you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 05, 2025 Aug 05, 2025
LATEST

This is a complicated process, so it's important to understand how the different pieces fit together. And you'll also need a good understanding of the Acrobat JavaScript model.  If you are not a programmer already and are not interested in learning how to program, then I'd suggest engaging a developer.  But if you are, here's a better explanation of the process. 

 

There are two PDFs, Lets call them the ToPrintPdf and the NumberPDF.   

  • The NumberPDF:   The PDF that provides the sequential number.  It is stored on a shared network drive where everyone in the organization can access it.    To make the process work this PDF contains, at a minimum, 3 elements.
    1.   A form field for maintaining the number. 
    2.  A document level function for retriving and incrementing the number.
    3.  A document level script for disclosing the PDF so it can be accessed from a non-privileged context. 
  • The ToPrintPDF:  The PDF the user wants to print with a sequential number. It doesn't matter where this PDF is stored.  Initially, it could be on a shared network drive so everyone in the organization and access it. But then a user could download it to their machine and use it from as much as they want, it doesn' t matter. This PDF must contain, at a minimum, 2 elements.
    1. A form field where the sequential number will be displayed.
    2. A Will Print script that  opens the NumberPDF and calls the function that returns the sequential number and then places it in the form field.

 

PM me through this forum if you are interested in some Paid for consulting/developement. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 27, 2017 May 27, 2017

Hello,

I'm trying to collect data about surgery results at 1 week, 1 month, 6 months.... is it possible to recover an autunumbered form so I can continue filling more fields?

for example. patient 1 - form assign 1 in id field, then I collect data of first week, but the form is already prepared for 1 month, 6 months, etc... Can I recover later the form number 1 so I con continue filling the rest of the form?

Thanks in advance

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 27, 2017 May 27, 2017

That choice is entirely with the form designer, you need to discuss your needs with them.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 27, 2017 May 27, 2017

I would add some code to test if the "defaultValue" is empty and then add the serial number and if it is not empty then do not update the number. Your source document will have to be saved with the "defaultValue" of the field cleared.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 18, 2022 Apr 18, 2022

Hi!  Hopefully you are still helping others with PDF forms. 

 

I am also trying to do this- sequential numbering on a PDF form when it is opened a new number is genertated based on the previous number used.  I am not a programmer, and I am not really familiar with javascript.

 

I used the script that you provided as an answer to the post here- obviously I did not do something right.

 

This is what I used-

var f=this.getField("WORK ORDER");
f.value=002038(f.value)+1;
f.defaultValue=f.value;

 

I deleted -( not sure if I was supposed to)

function SEQ NUMBERS()
{
}

 

I will also attach a copy of my form.

 

Thank you in advance for your assistance.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2022 Apr 18, 2022

This line doesn't make sense:

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

What did you try to achieve there?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 19, 2022 Apr 19, 2022

As I said- I am not a programmer and not really familiar with javascript.  I thought that's where the starting number might go.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 19, 2022 Apr 19, 2022

I'm not exactly sure how it works.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2022 Apr 19, 2022

It's not. And it's never a good idea to code by guessing... It's better to ask first, or read the documentation. Use the code as I provided it, only adjusting the field name in the first line, if needed.

The initial number should be placed as the default value of that field. However, the current code will strip any zeros from the start of it. If you want to include those the code will need to be adjusted.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 19, 2022 Apr 19, 2022

Thank you.  I truly appreciate your time for people like me.

 

I don't necessarily need the zeros, so that's ok.  Where do I put the first number?  On my form- in the 2nd cell over from the WORK ORDER cell?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2022 Apr 19, 2022

You should go to the field's Properties, then place the default value under the Options tab, like this:

 

try67_0-1650378812244.png

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 19, 2022 Apr 19, 2022

Ok.   So if I save that form, and name it 2038- when I open the orginal form shouldn't it advance to 2039?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines