OL Learn

Delete Pages in PDF

Hi,

I am trying to delete pages that don’t have the “Invoice” word at a specific location on the page, but I am getting the following error:

var myPDF = Watch.GetPDFEditObject();
myPDF.Open(Watch.GetJobFileName(),false);
var myPDFPages = myPDF.Pages();

for(var j = 0, len_j = myPDFPages.Count(); j < len_j; j++) {
var currentPage = myPDFPages.Item(j);
var pageType = currentPage.ExtractText2(1.125,0.14583,4.10416,0.57291).trim();
Watch.Log(j + ’ ’ + pageType, 2);
if(pageType != ‘Invoice’){
myPDFPages.Delete(j);
}

}

CollectGarbage();
myPDF.Save(true);
myPDF.Close();

Error:

[0002] W3602 : Error 0 on line 10, column 9: AlambicEdit.AlambicEditPDF.1: Error deleting page 0: One or more pages are in use and could not be deleted.

Please help!

Regards,
Steve

I believe this is a known issue with JavaScript and the AlambicEdit API.

Try this in VBScript:

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

myPDF.Open Watch.GetJobFilename, False
For i = 0 To myPDF.Pages.Count
   pageType = Trim(myPDF.Pages(i).ExtractText2(1.125,0.14583,4.10416,0.57291))
   Watch.Log i & " " & pageType, 2
   If pageType <> "Invoice" Then
        myPDF.Pages.Delete i
   End If
Next

myPDF.Save True
myPDF.Close
Set myPDF = Nothing

Hope this helps.

J.

Thank you Jim. I got the same error though in VBScript.

But I managed to understand why. As I delete pages from the PDF, I needed a way to update the total page count in my for loop otherwise I start querying indexes which no longer exist.

Hence I reworked my script by using a do-while loop, giving me the opportunity to update my page count before the next loop index.

In the past when coding PDF page inserts and/or deletions, I moved BACKWARDS through the PDF because of this same thing.