Copy link to clipboard
Copied
I need to be able to use form fields to indicate "Page X of Y". I know there's an option to do this in the document footer, but it won't work for this process because the documents are being aggregated in another system and it needs to be able to combine docs and have the page numbering change to match. If you use Acrobat's footers, combining two three-page documents gives you a six page document with page numbers 1/3, 2/3, 3/3, 1/3, 2/3, 3/3 instead of 1/6, 2/6, etc.
The question: How do I set it up so each page has a field, "PageNo", that indicates the current page number, and another field next to it, "NoPages," that contains the current number of pages in the document? These fields have to be able to update dynamically on document open as the documents are combined or split in a third party system.
The main problem I've run into: Any field with the same name as another field is functionally a copy of the original field and mirrors that field's contents. So you end up with five pages marked "Page 1 of 5." I can't give the fields unique names on each page, either, because the document combining happens outside of Acrobat and could be any combination of forms being compiled into various reports. I also don't have access to the system controlling this process, it's a black box.
I'm hoping there's some document-level script out there that can be set up to run when the merged document is opened to update the page numbers and total number of pages, but I don't know anywhere close to enough about JavaScript to come up with it on my own. I've also not run across any snippets of code floating around that handle this precise issue. If anyone can offer me any insight it would be greatly appreciated, I'm at the end of my rope with this one.
Copy link to clipboard
Copied
You could just remove and update the footer as needed. I would create an Action for this but it's only a couple of steps.
Also, I googled "javascript page numbering in pdf" and came up with a number of options, including:
https://asserttrue.blogspot.com/2011/04/script-for-putting-page-numbers-on-pdf.html
Note: I have not tested this code.
Copy link to clipboard
Copied
Using form fields to add pages number is old school an really time consuming:
😉
Copy link to clipboard
Copied
It is, yes. But for the reasons outlined in my original post, it's the only way to do the job in this instance.
Copy link to clipboard
Copied
I still don't understand why you can't simply update the footer as needed whenever files are combined.
Copy link to clipboard
Copied
I read too fast… 😉
Place this JavaScript as a calculation script in text fields:
var pnum = this.pageNum+1;
var numpag = this.numPages
event.value = pnum + " / " + numpag;
As any calculation script, it requires something to happen to be triggered. So you need to do something with form fields to update pages numbers (eg: Reset).
Copy link to clipboard
Copied
I did the above calculation JR and when I deleted a page it did not change the page numbers i.e. I had pages 1 of 3 and I deleted page 3 and the first page still says Page 1 of 3.
Copy link to clipboard
Copied
"As any calculation script, it requires something to happen to be triggered. So you need to do something with form fields to update pages numbers (eg: Reset)."
Copy link to clipboard
Copied
I am still confused with your response. I need to insert a form field and when I add or delet pages of pdf I need page numbers to update automatically
Copy link to clipboard
Copied
The Calculation event is triggered when any form field in the document is "onBlur".
So you have to make a dumb button that must be clicked after the pages are spawned/deleted just to trigger the calculation event.
Copy link to clipboard
Copied
Hi,
This thread is really helpful,
however I have a problem with this script. When I duplicate text field across pages they all say the same e.g. "1 of 7" on all pages.
When I go down page 2 and update calculation it says "2 of 7" on all pages .
Can you help me with what goes wrong?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
So I cannot duplicate it across my document, but need to insert a new text field on every page to get it
Copy link to clipboard
Copied
Hi,
Yes, each field must have a unique name, any field with the same name will be given the same value.
Copy link to clipboard
Copied
You can add the fields with a script.
Copy link to clipboard
Copied
I am having the same issue of the numbers duplicating. When I create the new textbox it has the correct page number (eg. 1/5), but when I create an new box and put in the same code all the textboxes change to match that one. I double checked and the text boxes have different names.
Copy link to clipboard
Copied
This is the code I am using
this.pageNum+1;
this.numPages;
event.value=pnum + "/" +numpag;
Copy link to clipboard
Copied
This code doesn't make sense... What are the first two lines for? Where do you define "pnum", and "numpag"?
Copy link to clipboard
Copied
I was using the code from above posted by JR Boulay, but I ended up having issues. I went with this code in the end and it seems to work pretty well
event.target.value=event.target.page+1;
this.calculateNow();
Copy link to clipboard
Copied
It was working... but now it has gone haywire.
When I spawn the new page the page # comes in as 0. As soon as I type on the page it changes to -1,5 or -1,6 -1,7 etc.. when the pages should really be numbered 6,7,8.
This is the code I currently have. I have no idea what has gone wrong? Maybe the calculations are no resetting?
event.target.value=event.target.page+1;
this.calculateNow();
Any help would be greatly appreciated
Copy link to clipboard
Copied
Why don't you use the original code posted? Your code had important parts missing.
Copy link to clipboard
Copied
When I use the original code it takes on the number of whatever page I interact with last. So it keeps changing as I work through the document and all my pages end up being the same number
Copy link to clipboard
Copied
Are you sure you are giving a different name to each field on each page? This sounds like what I'd expect if you use the same name. And your code is no better - all the pages would have the same number, which happens to be the number you're viewing. If you print, they will all be alike.
Copy link to clipboard
Copied
That's because that code is not entirely correct. It uses pageNum, which is the current page being viewed, instead of the actual page number the field is on. Use this instead:
event.value = (event.target.page+1) + "/" + this.numPages;
Of course, each field must have a unique name, and if you spawn pages you have to not only rename the spawned fields on those pages, but also call calculateNow afterwards, in order to update the field values.
Copy link to clipboard
Copied
Do I just write that line, or do I insert that into the code I already have? When I only write that line nothing seems to change. It shows up as 0 and then when I type on the page it shows up as -1,5.
I do have unique names for each page number field ex HazardousMaterialPg, FireAlarmpg, and I only need to spawn one copy of each template.
Is there not a way to just print whatever number page the field is currently on?