Contexts
JavaScript in Revu executes from three distinct contexts, each with its own purpose and scope:
-
Event-level scripts: Code that runs in response to user actions
-
Document-level scripts: Code that loads when a PDF opens
-
Folder-level scripts: Code stored externally that's available to all PDFs
Understanding where to place your code and what each context can access is fundamental to PDF scripting in Revu.
Event-level scripts are the most common type of JavaScript in PDF documents. They execute when users interact with form fields, buttons, and other interactive elements.
Accessing event scripts
In Revu, add event scripts through the Properties panel:
-
Select a form field or button.
-
Open the Properties panel.
-
On the Actions tab, choose the trigger event (Mouse Up, On Focus, Validate, etc.).
-
Select Run a JavaScript as the action.
-
Enter your JavaScript code.
The 'this' object
In event-level scripts, this refers to the Doc object of the current PDF:
// In a button's Mouse Up event
console.println(this.numPages) // Number of pages in the document
var field = this.getField("TextField1") // Access a form field
this.print() // Open the print dialog
The 'event' object
Event scripts have access to a special event object containing information about the current interaction:
// In a text field's Keystroke event
console.println(event.value) // Current field value
console.println(event.change) // The keystroke being entered
event.rc = false // Cancel the event
Document-level scripts execute automatically when a PDF opens. They're ideal for initialization code and defining functions that will be used throughout the document.
Accessing document scripts
Create document-level scripts in Revu:
-
Go to Forms > JavaScript.
-
Select Document JavaScript from the dropdown menu.
-
Select Add to create a new script.
-
Name your script.
-
Enter your JavaScript code.
Purpose
Document-level scripts typically do the following:
-
Define reusable functions for the entire document
-
Initialize form field values
-
Create document-wide variables
-
Set up the document environment
The 'this' object
In document-level scripts, this also refers to the Doc object:
// Document-level script
console.println("Document: " + this.documentFileName)
// Define a function available to all events in this document
function formatCurrency(value) {
return "$" + Number(value).toFixed(2)
}
// Initialize a field
this.getField("DateField").value = util.printd("mm/dd/yyyy", new Date())
Scope
Variables and functions defined in document-level scripts are available to all event scripts within that document:
// Document-level script
var taxRate = 0.08
function calculateTax(amount) {
return amount * taxRate
}
// Any event script in the same document can use these
var tax = calculateTax(100)
console.println("Tax rate is: " + taxRate)
Folder-level scripts are JavaScript files stored outside of PDFs, making their functions available to all PDFs opened in Revu.
Location
Folder-level scripts are stored in:
C:\ProgramData\Bluebeam Software\Bluebeam Revu\21\Revu\JavaScriptAny .js file in this directory loads automatically when Revu starts.
Purpose
Folder-level scripts provide the following:
-
Utility functions used across multiple PDFs
-
Company-wide standards and validations
-
Shared libraries of common functions
-
Trusted functions for privileged operations
The 'this' object
In folder-level scripts, this is undefined, not a document. To work with documents, you must pass them as parameters:
// Folder-level script (utilities.js)
// 'this' is undefined, not a document
// Global function available to all PDFs
function globalFormatDate(date) {
return util.printd("yyyy-mm-dd", date)
}
// Function that accepts a [Doc](../chapter_5/Doc.md) object as parameter
function countFieldsInDocument(doc) {
// Must receive the document explicitly
return doc.numFields
}
Using folder-level functions from a PDF:
// In a PDF's event script
var formatted = globalFormatDate(new Date()) // Call global function
var fieldCount = countFieldsInDocument(this) // Pass 'this' (the Doc) explicitly
Context interaction
The three contexts work together to create complete solutions:
// Folder-level: Define utility
function calculateInterest(principal, rate, time) {
return principal * Math.pow(1 + rate, time)
}
// Document-level: Add document-specific logic
var defaultRate = 0.05
function getLocalPayment() {
var amount = this.getField("Amount").value
return calculateInterest(amount, defaultRate, 5)
}
// Event-level: Use both in response to user action
var payment = getLocalPayment()
this.getField("Result").value = payment
Loading order
-
Folder-level scripts load when Revu starts.
-
Document-level scripts load when a PDF opens.
-
Event-level scripts run when triggered.
This means folder functions are always available, document functions are available after the PDF loads, and event scripts can use both.
Quick reference
Where to place code:
-
Folder-level: Shared utilities across multiple PDFs
-
Document-level: PDF-specific functions and initialization
-
Event-level: User interaction responses
How 'this' works:
-
Folder-level: this is undefined
-
Document-level: this is the Doc object
-
Event-level: this is the Doc object
Moving forward
Understanding these three contexts provides the framework for JavaScript in Revu. The next section explores security.
Resources
Revu 21
Revu JavaScript
JavaScript