OL Learn

Delete multiple pages from PDF with VB Script


I was playing around with workflow and tried this script in order to delete a page and it works.

Dim myPDF : Set myPDF = Watch.GetPDFEditObject
Dim pageType
Dim i

myPDF.Open Watch.GetJobFilename, False

Watch.Log myPDF.Pages.Count & " " & pageType, 2

For pageType = myPDF.Pages.Count to myPDF.Pages.Count
If pageType <> “59” Then
myPDF.Pages.Delete 59
End If

myPDF.Save True
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();

for(var i=0;i<pagesToDelete.length;i++) {

This is JavaScript, by the way, not VBScript.

Thank you very much, sir !!

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();

// 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++){
  } else {

// 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++) {


Have fun!

Ooo! WoW, this for the next Olympic Games!:grinning:

Definitely I`ll have fun :)))

Good homework for weekend!!

Many thanks again!


Hi @Phil, does Workflow no longer need

Back when setting up my own scrips I couldn’t get .Close() in JS to work without it. Can I remove it from the scripts now?

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.