How To…Calculate the Current Print Position

Question

“How do I calculate the current position of a component after it has been generated to a report?”

Solution

Use the OnDrawCommandCreate event of the component to get the current position on the page, then use the conversion utility functions in ppUtils.pas to convert the value into report units.

Download: CalcPrintPosition.zip

Sample Delphi code:

uses
  ppTypes,
  ppUtils;

procedure TForm1.ppLabel2DrawCommandCreate(Sender, aDrawCommand: TObject);
var
  lDrawTextCommand: TppDrawText;
  lPrintPosInInches: Double;
  lBottomPosInMicrons: Integer;
begin

  // Each time an object generates on a page, it creates a draw command
  // This is the Label2.OnDrawCommandCreate, which fires each time the
  // label generates on a page. The DrawCommand contains a desscription what will
  // be drawn to the page, the location, font, etc.

  // type cast to TppDrawText
  lDrawTextCommand := TppDrawText(aDrawCommand);

  // calc bottom position in microns
  lBottomPosInMicrons := lDrawTextCommand.Top + lDrawTextCommand.Height;

  // convert to inches
  lPrintPosInInches := ppFromMMThousandths(lBottomPosInMicrons, utInches, pprtVertical, nil);

  // display on the report
  lDrawTextCommand.Text := FormatFloat('0.000', lPrintPosInInches);

end;