Copy link to clipboard
Copied
While trying to come up with a way to quickly reduce the size of large PDFs that are viewed on line, I thought I would highlight a key word on the pages I need and then delete pages without annotations. I decided to replace step 2 in the Find and Highlight Words action with my own script. The script I came up with, (my first ever) works fine on the 35 page document that I used to develop it:
var pageArray=[]
for (var i = 0; i < this.numPages; i++)
{
Aannots = this.getAnnots(i);
if (Aannots == null)
{
pageArray.push(i);
};
pageArray.sort();
pageArray.reverse();
}
for (var j = 0; j < pageArray.length; j++)
{
this.deletePages(pageArray
}
but when run on a very large, 2000+ page doc, it deletes some pages, then hangs or finishes with:
"TypeError: Invalid argument type.
Doc.deletePages:62:Batch undefined:Exec
===> Parameter nStart.""
in the console.
If I keep rerunning this code in the console, it eventually deletes all the unannotated pages.
Since the arguments come from the array, I print it out and am surprised that it is not in the reversed numerical order I expected.
Could that be the problem? or one of them?
Thanks for any help you can give . . . at 65, I don't know if I'll live long enough to figure this out myself!
When deleting items from any kind of array it's always a good idea to loop over it from the end back to the beginning, because as you're deleting items in it you're effectively changing the size of the array and this can screw up the next steps in your loop.
Alternatively, you need to reverse the order of the array (which your code attempts to do, but it's not working because of the way the sort function works). Try this code instead:
...var pageArray=[]
for (var i = 0; i < this.numPages; i++) {
A
Copy link to clipboard
Copied
When deleting items from any kind of array it's always a good idea to loop over it from the end back to the beginning, because as you're deleting items in it you're effectively changing the size of the array and this can screw up the next steps in your loop.
Alternatively, you need to reverse the order of the array (which your code attempts to do, but it's not working because of the way the sort function works). Try this code instead:
var pageArray=[]
for (var i = 0; i < this.numPages; i++) {
Aannots = this.getAnnots(i);
if (Aannots == null) {
pageArray.push(i);
};
}
pageArray.reverse();
for (var j = 0; j < pageArray.length; j++) {
this.deletePages(pageArray
,pageArray ) }
Copy link to clipboard
Copied
Did you check the result of your sort. I do not think you are getting a numeric ordered sort.
You may also to adjust for duplicate page numbers.
MDN JavaScript Reference sort method.If one uses the optional compare parameter, the sored array can be reversed with sort method and one does not need to use thee reverse method. From the sort method description:
"If compareFunction
is not supplied, elements are sorted by converting them to strings and comparing strings in Unicode code point order. For example, "Banana" comes before "cherry". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in Unicode order."