Document Developer - instructions > Scripting > Scripting examples > Checking if there is room for objects on a page

Checking if there is room for objects on a page
In this example, an invoice document has a list with a repeated number of item lines depending on input data.
The list consists of a text area that is repeated as many times as specified in the count field in the input data block_count block. The following script is attached before the document root and evaluates the count field with the stEvalXPath() script function. It also creates an array that is used to repeat the text area.
Example 23
//Block repeats
$cnt = stEvalXPath( "/data/message/block_count/count");
$i = 0;
while( num($i) < num($cnt) )
{
$block_repeats[$i] = $i;
$i++;
}
 
A payment slip is printed at the bottom of the last page of the document. The page with the payment slip is printed on a different type of paper and must always be the last page in the document. This means the last page can contain item lines together with the payment slip. Or, if the payment slip does not fit on the last page with item lines, the last page contains only the payment slip.
A text area with a marketing message (Marketing message 1) is also rendered on the last page above the payment slip if there is room. If not, it is rendered on the second last page below the items list. If the last page has no item lines, another text area (Marketing message 2) is rendered above the payment slip.
The Story has the following structure:
Click to enlarge
Figure 37
The script below is attached to Marketing message 1. The script checks the position of the text area (A value depending on how much the item list has been repeated). If this position is higher up on the page than at the point where the payment slip could fit, the difference of these heights is calculated. If the height left is more than one inch, the Height property is set on the text area. This means that the area between the items list and the payment slip is filled with this text area.
If the position of the text area is lower than the position where the payment slip fits, and the remaining height is more than one inch, the area is filled with this text area. This also means that the payment slip is printed on the following page with an empty space available above it. A flag is set that the empty space is available which allows rendering of Marketing message 2 in this "white space".
Example 24
$pos_y = stGetParagraphPageYpt();
$min_height = in2pt(1); //inches
$high_pos = in2pt(7.4); //inches
$low_pos = in2pt(11.125); //inches
$last_page_blank = 0;
if ( num($pos_y) < num($high_pos) )
{
$height_left = num($high_pos) - num($pos_y);
}
else if ( num($pos_y) < num($low_pos) )
{
$height_left = num($low_pos) - num($pos_y);
$last_page_blank = 1;
}
else
{
$height_left = 0;
}
if ( num( $height_left ) >  in2pt(1) )
{
stsetPropertypt( "Height", $height_left );
}
else
{
skip();
}
 
The Marketing message 2 text area has a repeater on it with an XPath set to $last_page_blank[. = 1]. This means that it is only rendered when $last_page_blank = 1.
The following script is attached on the Story frame where the item lines table flow together with the marketing messages (if applicable). It is an After script:
Note:
Example 25
//Calculate where on the page the frame ends in Y direction
$frame_end = stGetPropertypt("Y") + stGetPropertypt("Height");
 
The following script is attached on the subsequent Story frame where the payment slip is rendered. It is a Before script:
Example 26
//Test if position is below frame end
$posy = stGetPropertypt("Y");
if( num($posy) < num($frame_end) )
{
	   skip();
}
 
This means that if the Y position of the payment slip Story frame is above the Y position of the items list story frame end point, the payment slip Story frame is skipped on the page instance.
Output
The following examples show the output when the input data has two, seven and nine transactions respectively:
 
Example 27
Click to enlarge
 
 
Example 28
Click to enlarge
 
 
Example 29
Click to enlarge
 
OpenText StreamServe 5.6 Updated: 2013-03-01