Skip to main content
aliw1
Participating Frequently
December 19, 2017
Answered

Unable to get loop to work for crop

  • December 19, 2017
  • 2 replies
  • 1025 views

Hi,

I am trying to change the crop box by a 10th of its height and length on every page of a PDF.

So if the 1st page is 100 by 100 it would be come 110 by 110. If the second page is 200 by 200 it would become 220 by 220 leaving new white space to the top and right which I intend to add a watermark.

This seems to work fine, but for some reason its only working on the active page, and not looping thorough each page.

I am no Java expert so I am probably missing something very simple, here is my code:

for (var i = 0; i <= this.numPages; i++)

{

    var rectCrop = this.getPageBox("Crop",i);

    var width = rectCrop[2];

    var height = rectCrop[1];

    var new_width = width+width / 10;

    var new_height = height+height / 10;

    

    var rectNewCrop = [ 0, new_height, new_width, 0 ];

    this.setPageBoxes("Crop", i, i, rectNewCrop);

}

It is hurting my brain trying to figure this out so any advice would be greatly appreciated.

Cheers,

Alistair

This topic has been closed for replies.
Correct answer try67

This was the output for a 10 page PDF:

0,792,612,0

0,871.2000122070312,673.2000122070312,0

0,792,612,0

0,871.2000122070312,673.2000122070312,0

0,792,612,0

0,871.2000122070312,673.2000122070312,0

0,792,612,0

0,871.2000122070312,673.2000122070312,0

0,612,792,0

0,673.2000122070312,871.2000122070312,0

0,612,792,0

0,673.2000122070312,871.2000122070312,0

0,612,792,0

0,673.2000122070312,871.2000122070312,0

0,612,792,0

0,673.2000122070312,871.2000122070312,0

0,612,792,0

0,673.2000122070312,871.2000122070312,0

0,612,792,0

0,673.2000122070312,871.2000122070312,0


The Crop box is not enough. You need to also set the Media, and possibly the Trim, boxes as well.

2 replies

try67
Community Expert
Community Expert
December 19, 2017

Change the first line to:

for (var i = 0; i < this.numPages; i++)

aliw1
aliw1Author
Participating Frequently
December 19, 2017

Still the same result, only one page is being cropped.

Interestingly, running this code on page one and, then on page two, changes page one back to its original dimensions, while page two is given the desired dimensions.

Bernd Alheit
Community Expert
Community Expert
December 19, 2017

Try following with output of messages:

console.show();

for (var i = 0; i < this.numPages; i++)

{

    var rectCrop = this.getPageBox("Crop",i);

    console.println(rectCrop);

    var width = rectCrop[2];

    var height = rectCrop[1];

    var new_width = width+width / 10;

    var new_height = height+height / 10;

    var rectNewCrop = [ 0, new_height, new_width, 0 ];

    this.setPageBoxes("Crop", i, i, rectNewCrop);

    rectCrop = this.getPageBox("Crop",i);

    console.println(rectCrop);

}

Bernd Alheit
Community Expert
Community Expert
December 19, 2017

Are the page sizes large enough?

aliw1
aliw1Author
Participating Frequently
December 19, 2017

The dimensions in the question above are an example, the document I have been testing this on is 8.5 x 11 in for the first 29 pages and the final page is 11 x 8.5 in.

I need to add white space and keep the page scaling.

I will be running this on many documents where the individual pages will often be different dimensions.