True PDF Software
for you!

  • Deutsch
  • English

VB-Script Events and Macros

The PDF writer now features VBScript macros and event handlers. This means that you can now hook into the process of PDF creation by writing your own Visual Basic event handlers. You can also use Visual Basic Script in your macros for output file name, author, title, subject, and keyword properties.

VBScript events are an extremely strong tool for customizing the PDF writer for use in your own software. Writing the right event handlers gives you complete control over the PDF or image output and the dialogs shown to the user in the process. It enables you to bypass the normal dialogs and insert your own dialogs written in the programming language of your choice.

Program Life Cycle

In order to use the VBScript feature you must understand the program life cycle. The life cycle is a description of when an event is triggered and the information available to the event at that time in the process.

Event handlers often read and write information to a global Context variable. The global Context variable contains information about the current state of the document creation. Events can control the process by manipulating values in the Context variable.

The table below shows the order in which Event Handlers are triggered and Context Variables are set.

EventHandler Reference

Error Handling

If an error is raised during the execution of a macro script then the document creation will abort and the error is reported. Errors can be caused by the executed code or raised explicitly by the programmer.

The example code below shows how you can raise an error.

  1. Sub OnAfterGUI()
  2.  
  3.     Err.raise(9999, "Test macro script", "Fake error to stop execution.")
  4.  
  5. End Sub

By default the program will show the error message in a message box and the user will have to press the Ok button to continue. The message box can be suppressed with the SuppressErrors setting and errors can be logged in the status file using the StatusFile setting.

You can add your own code to handle errors in the OnError event handler. The code below shows how you can retrieve the error information.

  1. Sub OnError()
  2.  
  3.     MsgBox("Error trapped by OnError eventhandler." & vbcrlf & vbcrlf & _
  4.  
  5.      "Error=" & Context("ErrorNumber") & vbcrlf & _
  6.  
  7.      "Description=" & Context("ErrorDescription") & vbcrlf & _
  8.  
  9.      "Source=" & Context("ErrorSource"))
  10.  
  11. End Sub

Important Note: All names used to access information in the context object are case sensitive. When reading and writing information to the Context("Config")("configname") all configuration names must be written in lower case.