<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Karl Heinz Kremer&#039;s Ramblings &#187; VB</title>
	<atom:link href="http://www.khk.net/wordpress/tag/vb/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.khk.net/wordpress</link>
	<description>Stuff, stuff and more stuff</description>
	<lastBuildDate>Sun, 25 Sep 2011 18:38:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Acrobat, JavaScript and VB walk into a bar&#8230;</title>
		<link>http://www.khk.net/wordpress/2009/03/11/acrobat-javascript-and-vb-walk-into-a-bar/</link>
		<comments>http://www.khk.net/wordpress/2009/03/11/acrobat-javascript-and-vb-walk-into-a-bar/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 00:37:36 +0000</pubDate>
		<dc:creator>khk</dc:creator>
				<category><![CDATA[Acrobat]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PDF]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Acrobat SDK]]></category>
		<category><![CDATA[Adobe Acrobat]]></category>
		<category><![CDATA[VB]]></category>

		<guid isPermaLink="false">http://khk.net/wordpress/?p=256</guid>
		<description><![CDATA[OK, let&#8217;s just forget about that old joke and concentrate on how to combine all three into something that is quite useful. As I&#8217;ve described in one of my previous posting, it is quite easy to automate Acrobat from VB or VBA. So how does JavaScript fit into this picture? As you may know, Acrobat [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: left; margin-right: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.khk.net%2Fwordpress%2F2009%2F03%2F11%2Facrobat-javascript-and-vb-walk-into-a-bar%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.khk.net%2Fwordpress%2F2009%2F03%2F11%2Facrobat-javascript-and-vb-walk-into-a-bar%2F&amp;source=khkremer&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>OK, let&#8217;s just forget about that old joke and concentrate on how to combine all three into something that is quite useful.</p>
<p>As I&#8217;ve described in one of my <a href="http://khk.net/wordpress/2009/03/04/adobe-acrobat-and-vba-an-introduction/">previous posting</a>, it is quite easy to automate Acrobat from VB or VBA. So how does JavaScript fit into this picture? As you may know, Acrobat comes with a very powerful JavaScript engine that provides access to a lot of functionality &#8211; more functions actually than what you have access to from your VB program. So, if you want to access some of these features, but you are stuck with VB, how can you do that?</p>
<p>Adobe provides a VB/JavaScript bridge with Acrobat &#8211; the JSObject, and the <a href="http://livedocs.adobe.com/acrobat_sdk/9/Acrobat9_HTMLHelp/IAC_DevApp_OLE_Support.100.13.html">Acrobat SDK describes how to use that feature</a>.</p>
<p>There is quite a bit of good information in the documentation. When you access the online documentation, expand the tree to &#8220;Acrobat Interapplication Communication > Developing Applications Using Interapplication Communication > Using OLE > Using the JSObject interface&#8221;.</p>
<p><H3>JavaScript</H3></p>
<p>In this example, I want to illustrate how you can create a folder level JavaScript function, instantiate the JSObject, and then call the custom function and display the result in VB. My plan was to use the <a href="http://khk.net/wordpress/2009/03/11/counting-bookmarks/">JavaScript code from my last posting</a>, but I found one small problem in the way I wrote the code (it works fine as a standalone JavaScript program, but we cannot use it in the VB context), so here is it&#8217;s replacement:</p>
<p><pre><pre>
function CountBookmarks(bkm, nLevel)
{
&nbsp;&nbsp;&nbsp;&nbsp;var count = 0;
&nbsp;&nbsp;&nbsp;&nbsp;if (bkm.children != null)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count = bkm.children.length;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (var i = 0; i &lt; bkm.children.length; i++)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; count += CountBookmarks(bkm.children[i], nLevel + 1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;return count;
}

function CountAllBookmarks()
{
&nbsp;&nbsp;&nbsp;&nbsp;console.clear(); console.show();
&nbsp;&nbsp;&nbsp;&nbsp;var n = CountBookmarks(this.bookmarkRoot, 0);
&nbsp;&nbsp;&nbsp;&nbsp;console.println(&quot;Number of bookmarks found: &quot; + n);

&nbsp;&nbsp;&nbsp;&nbsp;return n;
}

// add the menu item
app.addMenuItem({
&nbsp;&nbsp;&nbsp;&nbsp; cName: &quot;countBookmarks&quot;,
&nbsp;&nbsp;&nbsp;&nbsp; cUser: &quot;Count Bookmarks&quot;,
&nbsp;&nbsp;&nbsp;&nbsp; cParent: &quot;Document&quot;,
&nbsp;&nbsp;&nbsp;&nbsp; cExec: &quot;CountAllBookmarks();&quot;,
&nbsp;&nbsp;&nbsp;&nbsp; cEnable: &quot;event.rc = (event.target != null);&quot;
});

</pre></pre> </p>
<p>Save this JavaScript program as a folder level JavaScript file and make sure that it works.</p>
<p>So, why can&#8217;t we just implement the whole algorithm with the JSObject? The problem is with how VB handles objects that are actually JavaScript objects &#8211; in this case the root bookmark object. I cannot figure out how to access it&#8217;s &#8220;children&#8221; property through the JSObject. That&#8217;s the reason why I&#8217;m &#8220;cheating&#8221; by calling our custom JavaScript function &#8211; being able to do that is pretty cool IMHO.</p>
<p><H3>The VB Part</H3></p>
<p>We start out just like with any other VB program, by declaring some objects, initializing them and then it gets interesting&#8230;</p>
<p>Here is some sample code that shows how to initialize the JSObject, and how to call our own JavaScript function.  </p>
<p>Create a button on an Excel spreadsheet again, and put the following code into the button handler callback (just like before). </p>
<p><pre><pre>
Dim gApp As Acrobat.CAcroApp
Dim gPDDoc As Acrobat.CAcroPDDoc
Dim jso As Object

Sub Button1_Click()
&nbsp;&nbsp;&nbsp;&nbsp;Set gApp = CreateObject(&quot;AcroExch.App&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;Set gPDDoc = CreateObject(&quot;AcroExch.PDDoc&quot;)
&nbsp;&nbsp;&nbsp;&nbsp;If gPDDoc.Open(&quot;c:\temp\test.pdf&quot;) Then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set jso = gPDDoc.GetJSObject
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MsgBox (jso.CountAllBookmarks())
&nbsp;&nbsp;&nbsp;&nbsp;End If
End Sub
</pre></pre></p>
<p>Now just make sure that you have a file c:\temp\test.pdf that has some bookmarks in it. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.khk.net/wordpress/2009/03/11/acrobat-javascript-and-vb-walk-into-a-bar/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

