How To…PageBreak when X Space Left

Question

“How can I force a page break when less than X amount of space left?”

Solution

For simple layouts, use the DetailBand.PrintCount property to limit the number of times a static height detail band prints on each page.

For layouts with dynamic height detail, this example shows how to use the DetailBand BeforePrint event to calculate the amount of space left on the page.

Download: PageBreakXSpaceLeft.zip

Sample Delphi code:

procedure TForm1.ppDetailBand1BeforePrint(Sender: TObject);
var
  liSpaceAvailable: Integer;
begin

  {calc page space available - units here are microns (i.e. mm thousandths)}
  liSpaceAvailable := ppReport1.Engine.PageBottom - ppReport1.Engine.PrintPosRect.Top;

  {convert microns to Report.Units - using inches here}
  liSpaceAvailable := Trunc(ppFromMMThousandths(liSpaceAvailable, ppReport1.Units, pprtVertical, ppReport1.Printer));

  {force new page when less than 5 inches of page space remaining}
  if (liSpaceAvailable < 5) then
    begin
     ppReport1.Detail.OutOfSpace := True;
     ShowMessage('Space Available < 5, forcing new page..');
    end
  else
    ShowMessage('Space Available: ' + IntToStr(liSpaceAvailable));

end;