Month: September 2009

Office2PDFA – Scripting Support

Office2PDFA now also supports scripting. It supports two CLR languages: VB.NET & C#.  CLR = Common Language Routines, ie. C# and VB.NET are based on (implements the) Common Language Routines – there are other CLR programming languages J#, IronPhyton. VBScript is also supported but only for 32bit version.

It is possible to execute script before, and after the conversion. The script execution can be enabled/disabled. The scripts for before and after can be written in different programming languages.

A script consists of a list of functions and declarations. The runtime will generate a class from the functions and declarations and will execute the “Run” method. The Run method has one parameter of type IScriptContext.

IScriptContext properties:

  • SkipConversion (boolean): if the value is set to true, the application will not convert the document to PDF. The script is responsible to convert the document and write the result to a temporary folder. The path of the temportary folder should be stored in the DestinationFile property. The destination file type (extension) should be specified also from the script,  using the DestinationFileExt property.
  • DestinationFile (string): the fully qualified path of a file
  • DestinationFileExt (string): the extension of the destination file
  • FolderPath: the path of the folder of the input file
  • RootFolderPath: the root folder path, it can be different than the FolderPath property in the case if the subfolder monitoring is enabled
  • Error (boolean): the script should set this property some errors are occured.
  • ErrorDescription (string): the script should set this property and provide a description of the error.
  • FilePath: the fully qualified path of the source file
  • Folder (IFolder): a reference to an Office2PDF monitored folder.

Folder properties – use this object to get additional information about the monitored folder:

  • Name (the name of the monitored folder)
  • InputFolder (path to the input folder)
  • OutputFolder (path of the output foldre)
  • ErrorFolder (error folder)
  • ArchiveFolder (archive folder)

Sample VB.NET Script to convert a MS-Word DOCX to a DOC document – The following pre-script can be used to convert DOCX documents to DOC:

Download – Sample script to convert DOCX to DOC >>>

It is also possible to specify additional assembly references which are used by the scripts. For this script we used the following references:

  • System.Windows.Forms.dll
  • System.Data.dll
  • System.Drawing.dll
  • System.Xml.dll
  • Microsoft.VisualBasic.dll

The workflow of the Office2PDFA remains, the destination file is handled like a normal PDF conversion result. All options are available. PDF metadata, and other PDF properties cannot be applied.

Office2PDFA_Scripting_sample_docx_to_doc_#1 Office2PDFA_Scripting_sample_docx_to_doc_#2

Additional info about IScriptContext which is used by the Run method

It has 2 additional methods:

  • GetParam(name, defValue)
  • SetParam(name, value)

You can store in the IScriptContext parameter your script state data if you want to transmit some data from the pre-script to the post-script, because the same IScriptContext object is used for both scripts to ensure the correct workflow.

The post action script’s parameter contains all information about:

  • the source file/folder, subfolder
  • the desitnation file/folder, subfolder
  • the error (if any)

It is possible to use only the „post“ action script, or only the „pre“ script or both. Scripts can be written in C#, VB.NET or VBScript.  All features of the .NET framework and all features of these programming languages are available. The script security context (Evidence) is the security context of the Office2PDFA application. That means that the script will have the same security context like the application.


Office2PDFA also supports VBScript:

The method signature should look like:

sub Run(byref context)
end sub

all parameters are also available with VBScript