Using variables in a property - The TrimBox example

Is the TrimBox centered or not

The file above has a MediaBox and a TrimBox and our goal is to find whether the TrimBox lies in the center of the MediaBox. Well, there is a Property (which could be used in a Check) in pdfToolbox named 'TrimBox is centered inside MediaBox'. This will check the location of the TrimBox inside the MediaBox, but without any tolerance. That means, if the TrimBox is located in the exact center, hence not off by even a point, the Check is a success. Also the distances between MediaBox and TrimBox in horizontal and vertical direction must be identical.

Define your own condition for centering using JavaScript

To understand the center, the distance between the TrimBox and the MediaBox on the left and right side of the page (similarly for the top and bottom) has to be equal. Fortunately, there is a condition in pdfToolbox to check the in-between distance on the left hand side named 'Horizontal offset of TrimBox from MediaBox'.

We now have to compare this value to the distance between the two boxes on the right side, for which we will use a JavaScript variable. Hence,

Condition == Distance on the left

JavaScript variable == Distance on the right

And for our requirement, the 'Distance on the left' has to be equal to the 'Distance on the right.'

We start by making a new Check, 'TrimBox not centered inside MediaBox' with a custom check including 2 properties:

1. 'Horizontal offset of TrimBox from MediaBox' (to check the distance on the left) and a Javascript variable inside this property to check the distance on the right and comparing them (here, Unequal to)

2. 'Vertical offset of Trimbox from Mediabox' (to check the distance on the top) and a Javascript variable inside this property to check the distance on the bottom and comparing them (here, Unequal to).

What's inside the Script?

Very simple. The script here is calculating the distance between the MediaBox and the TrimBox on the right. (Another script for the vertical distance is not shown here in this example, but it has the same basis)

PLEASE NOTE: This Profile will work brilliantly on a one page file or a file which has the same distance between the two boxes on the right side of the first page and the left side of all the pages.

You can very well notice that the script is picking up the right hand side value only from the first page. Have a look at this: app.doc.pages[0] (in JavaScript, the page "0" is the first page)

In case the pages in your file have different MediaBox dimensions on different pages, the script has to be a bit more complicated.

For multi page files or files with different dimensions of boxes on different pages

The problem in the script above was that the JavaScript variable was calculating the distance of the 2 boxes on the right side from the first page of the document and comparing it to the left side of all the pages.

What we want to achieve here is to calculate the distance between the TrimBox and the MediaBox on the right side (hence the JavaScript variable) separately for each page. The solution can be achieved using a Process Plan which will run through all the pages and compare left and right (also top and bottom) for each page individually (Left of page 1 with left of page 2 and so on).

The process plan with loops has 4 parts:

1. A variable to check that the page that we are looking at is page 1.

2. A check for horizontal offset (to calculate the distance between the boxes on the right side for each page individually) for the current page: screenshot below.

3.  A check for vertical offset (to calculate the distance between the boxes on the top side for each page individually) for the current page.

4. A variable to increase the page number by 1, continuing with step 2 (if the page number is lower than or equal to the last page) or to end the loop when it is on the last page.

Alternatively starting pdfToolbox 11

The process plan with loops includes 4 sequences:

  1. A Variable that checks that the page we are looking at is page 1.
  2. A Check on the current page for horizontal offset (to calculate the distance between the frames on the right for each page individually); see figure below.
  3. A Check on the current page for vertical offset (to calculate the distance between the frames above for each page individually).
  4. A Variable to increase the page number from step 2 by 1 (if the page number is lower than or equal to the last page) or to end the loop with the last page.

The screenshot above shows the Check for the point 2 of the above Process Plan (The Check for point 3 will be similar, hence not shown here).

The above Check contains 2 Scripts (mentioned by numbers and shown below)