For pageType = myPDF.Pages.Count to myPDF.Pages.Count
If pageType <> “59” Then
myPDF.Pages.Delete 59
End If
Next
myPDF.Save True
myPDF.Close
Set myPDF = Nothing
I have to admit I am new to scripting of any kind. Does anyone have any suggestion for a script that deletes certain pages. For example from a PDF of 500 pages, delete pages( 58,281,389)
Well that script is not the most efficient, it contains a useless loop.
To delete several pages, you could just store the page numbers in an array and then loop through that array to delete the specified pages. NOTE: you must loop through the pages in descending order for this to work.
So something like this would do the trick:
var pagesToDelete = [1,4,7];
// Sort the pages in descending order
pagesToDelete.sort(function(a, b){return b-a});
var myPDF= Watch.GetPDFEditObject();
myPDF.Open(Watch.GetJobFileName(),false);
for(var i=0;i<pagesToDelete.length;i++) {
myPDF.Pages().Delete(pagesToDelete[i]-1)
}
myPDF.Save(true)
myPDF.Close()
I will keep playin, learning and improving. Even though I know some basic function in JS sometimes my imagination disappoints me… and cannot utilize them in some use cases like this.
PS: After seeing the first line I knew it was Javascript
Just for fun (and if you want to study a more complex JS sample), here’s a revised version of the script that allows you to specify multiple page ranges, using the same syntax you would use to print pages from Microsoft Word, for instance.
So for instance, say you specify something like this: 1,12,3-6,18-. That means "Delete page 2, page 12, pages 3, 4, 5, 6 and from page 18 to the end of the document.
var pageRange = "1,12,3-6,18-";
var myPDF= Watch.GetPDFEditObject();
myPDF.Open(Watch.GetJobFileName(),false);
// Parse all the ranges and store page numbers in array
var pagesToDelete = [];
var ranges = pageRange.split(",");
for(var i=0;i<ranges.length;i++){
if(ranges[i].indexOf("-")>-1) {
var subRanges = ranges[i].split("-");
var startPage = subRanges[0];
var endPage = subRanges[1] || myPDF.Pages().Count();
for(var j=startPage;j<=endPage;j++){
pagesToDelete.push(j);
}
} else {
pagesToDelete.push(ranges[i]);
}
}
// Sort the pages in descending order
pagesToDelete.sort(function(a, b){return b-a});
//Delete the pages
for(var i=0;i<pagesToDelete.length;i++) {
myPDF.Pages().Delete(pagesToDelete[i]-1);
}
myPDF.Save(true);
myPDF.Close();
You still need it. Under some circumstances, Windows’ JavaScript engine does not do a very good job at collecting garbage on its own, so you still have to give it that extra push.