resource()

The resource() function returns information about an image resource. This function is useful in a Control Script, for example to check the number of pages or the page height and width before setting it as a background (see Control Script: Setting a Print section's background).

This function can also be used to check if a file exists. It returns null if the file does not exist. There is no need to explicitly check for null; a check to see if a resource exists can simply be written as:

Copy
var info = resource(path);
if (!info) {
// File does not exist
}

resource(location, pageNumber)

location

The location can be either:

  • A path relative to the template root, like images/Image.png

  • An absolute URL, like

    • http://image-server.com/Image.png

    • file:///C:/images/Image.png

    • file://C:/images/Image.png

    • file://192.168.1.123/images/Image.png

  • An absolute local file system path, like C:\Images\image.png

pageNumber (optional)

The desired page. Counting starts at 1. If no page number is given, information about the first page will be retrieved.

The returned object is of the type ImageInfo. It has the following fields:

Field Type Description
height float The height of the current page (in points).
page Number Current page number (counting from 1) within the resource.
pages Number The total number of pages in the resource.
permissions

Permissions

PDF only. Allows to verify if a PDF is password-protected or has restrictions for printing.
width float The width of the current page (in points)

Examples

This script retrieves the second page of a PDF that is present in the template's resources.

Copy
var pdf = resource("images/stamp.pdf", 2);
var height = pdf.height;
var width = pdf.width;
var numberOfPages = pdf.pages;

In the following script, the function is used to check if a file exists.

Copy
if(resource("C:/paw.pdf")){    
    //exists
} else {    
    //oops
}