VB-Script Events and Macros / PDF Printer
The PDF writer now features VBScript macros and event handlers (from Professional Edition or higher). 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.
Own VBScript Macros are stored in the "Macros" directory, which is located in the installation directory of the printer, under a simple file name. All VBScript macros in this directory are executed by the PDF printer! Flexibly affect the PDF printer, with simple VBScript programming opens up immense flexibility for you to configure the PDF printer in any way, to use and programatically influence!
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.
|Event Handler or|
|PrinterName||Name of the used PDF printer.
reads the PDF printer name into the printername variable.
|AppPath||Holds the full path of the program folder where the PDF printer
reads the program path into the apppath variable.
|PostscriptFileName||This context variable holds the name of the raw postscript output from the printer driver.
Using this file path you can get access to the postscript code as it looks before any modifications are made to it.
reads the file path into the filename variable.
This is the name of the document as it is listed in the print job queue.
The title in the configuration is often based on this value.
However, the title in the configuration is often change whereas the
|DocumentCreationDate||A creation date string read from the raw postscript created by the printer driver.|
|Config||This is the loaded configuration. The configuration is probably the most interesting context variable.
It carries information about the current configuration.
Using this context variable you can read and write to the current configuration and change the
PDF or image creation process. Configuration values must be accessed using lower case names.
If you by accident use mixed or upper case names then you will just get an empty result.
sets the watermark text of the created document.
This event is raised when the configuration is loaded from a file. The loaded configuration can be manipulated through Context("Config").
Rem -- Simple example that shows the selected output
Rem -- path in a message box
Rem -- Example that creates a sample bookmark at page 1
Dim filename, fso, content, file
Rem -- Get the name of the Postscript file
filename = Context("PostscriptFileName")
Rem -- Load the content
fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile(filename, 1) ' 1 = ForReading
content = file.ReadAll()
Rem -- Insert bookmark
content = Replace(content, _
vbcrlf & "%%Page: 1 1" & vbcrlf, _
vbcrlf & "[ /Title (Sample bookmark) /OUT pdfmark" & _
vbcrlf & "%%Page: 1 1" & vbcrlf)
Rem -- Save the file again
Set file = fso.OpenTextFile(filename, 2) ' 2 = ForWriting
|TextFileName||The PDF writer can create a text file with all the text found in the print job.
A text extract can be parsed by an event handler and used to modify values
in the configuration. This is useful if you want to use values from the
printer output as properties in the resulting PDF file.
To enable the text extraction you must set the ExtractText configuration value to yes.
reads the full path of the extracted text file into the
Triggered when the extracted text is ready for processing.
REM -- Example that shows the file name of the extracted text file.
This event is only fired if you have set ExtractText=yes in the configuration.
|OnMacrosApplied||Macros have been applied to the configuration.|
Called after the GUI has been shown. It is also called if the GUI is not shown based on the ShowSettings setting.
This is the event where you can make the last adjustments before the PDF or image is created.
You can access and modify information such as document title and author using the
REM -- Example that adds the author to the title
context("Config")("title") = _
context("Config")("title") & _
" (" & context("Config")("author") & ")"
|OutputFileName||Name of the output file.|
|FileCount||Number of files in the output. Multiple files are normally created when the output type is an image.|
|OnSuccess||OnSuccess is triggered if the document creation is completed without errors.|
|OnError||OnError is triggered if there were errors during the document creation.|
Regardless of the status this event is triggered when the process is completed.
If you need to know if the operation was a success then you can create an
event handler for the
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.
Err.raise(9999, "Test macro script", "Fake error to stop execution.")
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.
MsgBox("Error trapped by OnError eventhandler." & vbcrlf & vbcrlf & _
"Error=" & Context("ErrorNumber") & vbcrlf & _
"Description=" & Context("ErrorDescription") & vbcrlf & _
"Source=" & Context("ErrorSource"))
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.