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

Using insertChild to add a hierarchial bookmark into a PDF (JSObject)

cl5792
Registered: Jul 17 2008
Posts: 53
Answered

I have a VB6 module that is inserting a bookmark into a PDF via JSObject. This is working great except that all of the bookmarks are off of the root. I would like to add new bookmarks under a parent (as determined by a string passed). I can't seem to get the insert into the correct position in the tree.

This is the code for finding the parent and it works well but I cannot get any further to insert in the correct position:

Set oJSO = oPDDoc.GetJSObject
Set oBookMarkRoot = oJSO.bookmarkRoot

oBookMarks = oBookMarkRoot.children
iBookMarkCount = UBound(oBookMarks)
For i = 0 To iBookMarkCount
If oBookMarks(i).Name = sParentName Then
'I would like to get the parent's children to determine the position
'to add the new bookmark but I cannot seem to get the child bookmarks

'The following code does not work and throws an error
oPBookmarks = oBookMarks(i).children

iParent = i

Exit For
End If
Next

Any help with getting the insert into the correct position, under the parent, would be greatly appreciated. ALso, the bookmarks also seem to be inserting at the top of the tree each time, instead of at the end of the tree. I may have this fixed but if anyone has any input on this it is appreciated.

Thanks,

My Product Information:
Acrobat Pro 8.1.2, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
There is a problem with using the JSO in a VB application, mainly that JavaScript and VB are two very different languages and thier data types are not always compatible. Specifically VB has a problem with multilevel JS objects like the bookmark object. This cause all kinds of problems when you have a good bit of JavaScript to execute.

The best strategy for using the JSO is to write all of the JavaScript code into a Folder Level Function, then call the function from your VB program. This way there is no incompatibility issue.

You can read about Folder level scripts here:
http://www.acrobatusers.com/tech_corners/javascript_corner/tips/2006/folder_level_scripts/

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

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

cl5792
Registered: Jul 17 2008
Posts: 53
Thanks Thomp,

I am looking at creating the JavaScripts to do this. One question... Is there a way to execute a JavaScript from within a VB app, passing a variable for the bookmark name and page?

Thanks,
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Yes, you create a folder level script containing a function with any inputs you want. Then call the function directly from the JSO. Passing in the parameters. The restriction is that you have to pass in scalar argument. No object, just strings and integers. For complex data I pass in string definition that can be easily converted/parsed on the JavaScript side. For example here's a function that takes an pseudo-Array as input.

// In the Folder level Script
fucntion Myfunct(cArr)
{
// convert to a real array
var aMyArr = eval(cArr);
}

!! In the VB code

jso.Myfunct("['apple','grape','orange']")

I haven't checked over the syntax, so there might be a bug here. But this is the idea.


Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

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

cl5792
Registered: Jul 17 2008
Posts: 53
Yes, this looks like the track I need to go down. Now, how do I instance the JavaScript folder file (.js) to execute it from vb? Sorry, but I have not gone this route before. Since the file is not part of a PDF doc object I would need to open it as it's own object, correct?

Can you give some help on this? Also do I use FSO or do I need another reference in my project?

thanks so much!
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Also, check out the ExecuteThisJavascript method of the fields object.

George
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
From the JSO you can execute any Folder or Document Level JavaScript function. Simply by creating the function in Acrobat, it is now visible to the JSO. Be sure to read the artical I referenced earlier about Folder Level Scripts.

George brings up an alternate methodology. Instead of using the JSO, you can execute a folder or document level function through the "ExecuteThisJavaScript" method in the "AForm" Field Object. You'll find info on using this method in the IAC reference for Acrobat 8 and later.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

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

cl5792
Registered: Jul 17 2008
Posts: 53
I have a .js file in the Acrobat JavaScripts folder (C:\Program Files\Adobe\Acrobat 8.0\Acrobat\Javascripts). It has this function:


function BookMark1(sName, iPage)
{
// Add a bookmark
var sPage = "";

sPage = "pageNum=" & iPage;
bookmarkRoot.createChild(sName, sPage);
}


I have this code in VB6:


Dim oPDDoc As New Acrobat.AcroPDDoc
Dim oJSO As Object
Dim vReturn As Variant
Dim iNumPages as integer

Set oPDDoc = New Acrobat.AcroPDDoc

iNumPages = oPDDoc.GetNumPages
Set oJSO = oPDDoc.GetJSObject
Set oBookMarkRoot = oJSO.bookmarkRoot

vReturn = oJSO.Bookmark1("Page 3", 3)

The last line does not run and I get an error "Object does not support this property or method"

Any ideas?

thanks :)
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Well the first thing to do is to is make sure your folder level fucntion is working correctly within the Acrobat Envrionment.

The folder level scripts are loaded when Acrobat first starts up.

1. Close Acrobat and use the task manager to make sure it's not running in the background.
2. Restart Acrobat
3. When it's up and running open up the Console Window.
4. Make sure no errors are being reported.
5. Then try running your function from the console window.
6. Again make sure no errors are reported and it does exactly what it is supposed to do.

If everything is ok, then try it from the VB App.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

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

cl5792
Registered: Jul 17 2008
Posts: 53
I was able to run the function successfully from the console. Am I calling it correctly from VB?
cl5792
Registered: Jul 17 2008
Posts: 53
Thanks ever so much Thom! I had Acrobat running in the background and when I restarted I was able to run the function from VB successfully.

I now have hierarchial bookmarks in my 180+ PDF.

Thanks again
Cammy