These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Error when trying to get pageNum after executing bookmark

sfischmann
Registered: Jun 12 2008
Posts: 5
Answered

Hello,

I'm trying to set page numbers using bookmarks that are already in a PDF document. I am doing this in a javascript action in a batch sequence, with "run commands on" set to "ask when sequence is run."

Here is a snippet (bkm is a bookmark, and startPage is a variable that holds where the numbering should begin):

if ( bkm != null ) {
bkm.execute();
var startPage = this.pageNum;
}

After executing the bookmark, I can see in the debugger that the pageNum property of the current document is not set, so "this.pagenum" gives an InvalidGetError.

If I open the documents in Acrobat, and change the batch sequence "run commands on" to "files open in acrobat" the script works. Why doesn't it work when I choose the files from a file dialog, or preselect a list?

Thanks for any help you may have...

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
For a Batch Script, the working document object is in "event.target". The "this" pointer is unreliable because it refers to the current object, which isn't always the doc.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

sfischmann
Registered: Jun 12 2008
Posts: 5
Thanks for your response!

I changed all instances of this to event.target, and have the same results: the script works for documents currently open in Acrobat, but not when you change the setting to ask it to prompt you for files, because bkm.execute() does not set the pageNum property for the document object.

When using the debugger, it looks like bkm is set properly:
[object Bookmark="Introduction"]

...and bkm's doc property's URL is correctly set to the file that Acrobat is currently processing.

Is there any special way I must invoke bookmark.execute()? That seems to be the problem.

Here's a more complete snippet:

if ( bkm != null ) {
bkm.execute();
var startPage = event.target.pageNum;
event.target.setPageLabels(startPage, [ "D", "", 1]);
event.target.setPageLabels(2, [ "r", "", 1]);
}

The exact error is:

InvalidGetError: Get not possible, invalid or unknown.
Global.pageNum:36:Batch undefined:Exec
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Ahh, after looking at this a bit closer, yes, in batched files the document page number is not valid. This is because the document is opened as hidden. Only viewed documents have a current page number. So you can't do it this way.

If you are working on a small number of documents then you should use a regular Automation Script and do the files one at a time. Or just open all of them in Acrobat.

If you've got a lot of files. I'd suggest writing a small VB program to open the files in Acrobat and then run the JS code. Basically, writing your own batch process.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

sfischmann
Registered: Jun 12 2008
Posts: 5
Thank you very much for your help. In retrospect, it all makes sense. I will look into Automation scripting.