Finding user screen resolution with JavaScript

PDF’s cross-platform nature makes it very portable, but it also means that there is a multitude of different software environments, preference settings and hardware configurations to be found in the potential audience of any widely distributed document. For this reason, it can be really useful to run some JavaScript that checks these things and modifies the behavior of the PDF accordingly.

This is a piece of code that I find particularly useful for consumer or customer documentation — basically, it checks the user’s screen resolution. This information can then be used in a number of ways, but one would be setting a document’s initial zoom level based on the user’s screen resolution (e.g. If the screen resolution is 800×600 or below, set the document zoom to 65%, but if the screen resolution is above 800×600, set the zoom level to 100%.)

Run this code at document open using document level JavaScript (more on document level JavaScript here.)

// get object representing the primary monitor
var monitors = app.monitors.primary();

// returns an array that represents the monitor's boundaries
var res = monitors[0].rect;

// check if resolution is higher than 800x600
if(res[2] > 800 && res[3] > 600)
// set document zoom to 100%
this.zoom = 100;
// set document zoom to 65%
this.zoom = 65;

You May Also Like

About the Author: Chris Dahl

Leave a Reply