Copy link to clipboard
Copied
Hi,
I have a PDF form, one of many that I am using this script on. For some reason it does not work correctly on this one document.
There are fields at the bottom of each page which are populated with the text "<<remove_page>>" when I want the page to be deleted. Here is the javascript I'm using attached to a button at the top of the first page:
for (var fieldNumber = 0; fieldNumber < numFields; fieldNumber ++)
{
var strfield = getField(getNthFieldName(fieldNumber)).value ;
var strpage = getField(getNthFieldName(fieldNumber)).page ;
if (strfield == "<<remove_page>>") {
this.deletePages(strpage);
}
}
The behavior is this. It deletes all the pages but one. It's the same one each time I have renamed the field, recreated it and moved the page around. It leaves the page each time. When I run the script from the button a second time it deletes the page.
I tried debugging it and found that on the first run of the script it is not finding the field when it iterates through them. The second time it does!!
Any help would be great. Thanks,
JC
Copy link to clipboard
Copied
You need to sort the fields by page number and then iterate from the last one to the first. Also, after deleting a page you need to skip all the other fields that (used to be) on it, as they no longer exist. Alternatively, save the page numbers of the pages to delete to an array and then delete them in a separate loop, again, starting from the last one and going backwards to the first.
Copy link to clipboard
Copied
Thanks, I had the array idea but going from last to first was the missing piece! I'll post my code here when I'm done.
Copy link to clipboard
Copied
I now have the following code which is skipping one of the fields in the document still. This loops through the fields backwards:
for (var i = numFields-1; i > 0; --i)
{
var strname = getField(getNthFieldName(i)).name ;
var strfield = getField(getNthFieldName(i)).value ;
var strpage = getField(getNthFieldName(i)).page ;
if (strfield == "<<remove_page>>") {
app.alert(strname)
}
}
How do I loop through all the fields without missing one? Thanks,
JC
Copy link to clipboard
Copied
Change the end criteria for your loop to " i >= 0" - that should do the trick.
Copy link to clipboard
Copied
Thanks try67 & Karl,
Final code:
for (var i = numFields-1; i >= 0; --i)
{
var strname = getField(getNthFieldName(i)).name ;
var strfield = getField(getNthFieldName(i)).value ;
var strpage = getField(getNthFieldName(i)).page ;
if (strfield == "<<remove_page>>") {
this.deletePages(strpage);
}
}
Copy link to clipboard
Copied
The problem of deleting things while looking at them is a nasty one, like that of sawing off a branch on which you are sitting. Your last code has the potential to fail on some PDFs at least. For example, suppose the last page has 5 fields. There are 10 fields. Field(9) is on the last page. In your loop you field(9) first, and delete that last page. There are now only 5 fields. Now you look for field(8). What will happen?
This is why the two stage approach is recommended. Build an array of page numbers. Remove duplicates. Sort the array. Go through from last to first.
Or another approach is to run a loop like yours, and delete the page found. Then exit the loop and repeat this whole process from the top. Exit the whole process if the loop finds no pages. (I'd also put in a sanity check and do no more than min(numpages,numfields) loops).
Copy link to clipboard
Copied
Another thing that must be checked is that you're not trying to delete the last page in the file, as that's forbidden too, of course.
Copy link to clipboard
Copied
I see... To take all scenarios into account would the following idea work:
1. Build an array of all the page numbers, eliminate duplicates.
2. iterate through the array of pages and on each page loop through the fields on that page building an array of the pages that contain a field with the value "<<delete_page>>". Exit whole process if no pages found.
3. Sort the second array and iterate through it backwards deleting the pages in order of last to first, Exit process if no pages found.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now