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

How can I add code to the document open event?

fanchunrong
Registered: Mar 4 2008
Posts: 37

I want to execute some code when the document is opened. But I couldn't find how to add the code to the open event of the document. Anyone can help?

Thanks!

My Product Information:
Acrobat Pro 8.0, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You need to add a document level script using the menu option "Advance => Document Processing => Document JavaScripts...". When the "JavaSripts Functions" pop-up opens, assign a name for your script, click the "Add" button and enter your script. You can place the code within the function definition, then use a call of the function, remove the function lines and enter your code, or add your code and create a function. Be aware that depending upon where you define a variable, you will be controlling the scope of that variable.

George Kaiser

marklorenz
Registered: May 6 2009
Posts: 14
Hello. I have created a setFocus() script and tested it via a doc-level event (will save). However, I cannot find a way to associate this script with the document being opened - can you help? I only see Save/Print/Close doc-level events (I'm using Acrobat 8 Pro). Thanks for your help! Mark
marklorenz
Registered: May 6 2009
Posts: 14
Figured it out, using example #3 on this page:
http://www.planetpdf.com/developer/article.asp?ContentID=6828

I didn't know that you had to include code to invoke the function after the function declaration in the doc-level scripts. Once I did that, it worked.

Hope this helps someone else... l8r
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
Planet PDF has been a source or quality Acrobat/Reader information before the Acrobat Users Community. And many of the posters here have posted and still post there. Even though some of the articles maybe old, there is a lot of good basic information there. Some of the posters that have posted there are Ken Foss, Ted Padova, Carl Young, and George Johnson to name a few.

Creating or inserting code by using the "Advanced => Document processing => Document JavaScripts..." not only allows for the definition of a document level function, entering JavaScirpt code without a function allows those JavaScript statements to be processed upon the opening the PDF. Just as an application folder level script is executed upon starting the Acrobat/Reader application, for example adding a menu item requires the execution of specific lines of code to add the menu item and creating a function as part of the code makes using the 'addMenuItem" coding a lot easier.Just defining a function in an event level, a document level or an application folder level does not execute the function's code, Acrobat/Reader only checks the syntax and tokenizes the code. A function's code is only executed when there is a call to the function.

But that does not mean one should not use them.

Since a function can return a value, one can verify that a complex action has completed successfully.
Use of a function can break large complex code into smaller more readable sections of code.
Use of a function can allow reuse of a code for a oft repeated action.
Use of a function can isolate problem code for easier debugging or error trapping.
One can define a simpler name for a JavaScript method, or expand the JavaScript function to be more flexible.
For example modifying the JavaScirpt 'Math.round()' method to allow for providing the number of decimal places to round to;
function Round(fValue, fDecimals){// round fValue to fRound decimal placesreturn  Math.round(fValue * Math.pow(10, fDecimals) ) / Math.pow(10, fDecimals);} // end round function

And one can also create a single use function without a function name:
// create and call an undefined function(// define functionfunction () {app.alert('Hello World!', 3, 0);return;}// call undefined function() );

George Kaiser