Reverse Crop With Javascript

After cropping a page with javascript, how do I reverse it so the crop box goes back to the original settings?

David Dagley

2 Answers

Voted Best Answer

Unfortunately the original settings are lost, we don't know what the old crop box was. What you can do is save by how much you adjust the crop box and then take those values and apply a new crop box with these adjusted settings. The following code snippet is something close to what I use when I try to extract text from a specific location: I first crop to that location, then extract the text, and then reverse the first crop operation again.

var top = 2.3 * 72;
var bot = 8.2 * 72;
var left = 1.0 * 72;
var right = 6.5 * 72;

var aRect = this.getPageBox("Crop", 0);
var a = left;
var b = aRect[1]-top;
var c = aRect[2]-right;
var d = aRect[3]+bot;
this.setPageBoxes("Crop", 0, 0, [a, b, c, d]);

var theWord = this.getPageNthWord(0, 0, 0);
console.println("theWord = " + theWord);

// reset the crop box
aRect = this.getPageBox("Crop", 0);
a = aRect[0]-left;
b = aRect[1]+top;
c = aRect[2]+right;
d = aRect[3]-bot;
this.setPageBoxes("Crop", 0, 0, [a, b, c, d]);

The top/bot/left/right variables hold the amount by which I crop from the top/bottom/left/right margin.

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...

By Karl Heinz Kremer   

You should get the whole page back by setting the crop box to the media box. From there you could restore the original cropbox.

Hope this can help.

Max Wyss.

Max Wyss   

Please specify a reason: