Home General
New Blog Posts: Merging Reports - Part 1 and Part 2

Horizontaly position a lppLabel

edited March 2009 in General

Hi

I have 4 ppLabel in my report on the detailband

ppLabel1
ppLabel2
ppLabel3
ppLabel4

The caption of the labels is set at runtime. I want the ppLabel with
the longest caption horizontaly in the center of the page (same as
ppLabel.ParentWidth := true, ppLabel.TextAlignment := taCentered), and
the other three ppLabels get the same left position as the longest one.

eg blablabla
blablablablabla <--- this one horizontaly centered on the page
bla
blabla

How to achieve this?

Arno
--

Comments

  • edited March 2009
    Hi Arno,

    There is no built-in feature of ReportBuilder to do this. You will need to
    manually calculate the position of each label before the first label is
    printed.

    Perhaps in the Band.BeforePrint, determine the longest label using the
    TCanvas.TextWidth routine. Then with this information, determine where the
    center location should be...

    LeftPos := (PageWidth / 2) - (TextWidth / 2);

    ...and assign this left position to each of the other labels before they
    print. Be sure to use the correct units when calculating. The ppUtils.pas
    file includes numerous helpful conversion routines.

    --
    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited March 2009
    Nico Cizik (Digital Metaphors) wrote:


    Hi Nico,

    thanks for your help. It took me a little while, but now it seems to
    work

    procedure TForm1.ppDetailBand1BeforePrint(Sender: TObject);
    var
    PrinterCanvas : TCanvas;
    MaxLength : Integer;
    LeftPos : Integer;
    begin
    PrinterCanvas := ppReport1.Printer.Canvas;
    PrinterCanvas.Font := ppLabel1.Font;

    MaxLength := PrinterCanvas.TextWidth(ppLabel1.Caption);

    PrinterCanvas.Font := ppLabel2.Font;
    if PrinterCanvas.TextWidth(ppLabel2.Caption) > MaxLength then
    MaxLength := PrinterCanvas.TextWidth(ppLabel2.Caption);

    PrinterCanvas.Font := ppLabel3.Font;
    if PrinterCanvas.TextWidth(ppLabel3.Caption) > MaxLength then
    MaxLength := PrinterCanvas.TextWidth(ppLabel3.Caption);

    PrinterCanvas.Font := ppLabel4.Font;
    if PrinterCanvas.TextWidth(ppLabel4.Caption) > MaxLength then
    MaxLength := PrinterCanvas.TextWidth(ppLabel4.Caption);

    Leftpos := trunc( (ppReport1.PrinterSetup.PaperWidth / 2)
    - (MaxLength / 2)
    )
    - trunc(ppReport1.PrinterSetup.MarginLeft);

    ppLabel1.Left := LeftPos;
    ppLabel2.Left := LeftPos;
    ppLabel3.Left := LeftPos;
    ppLabel4.Left := LeftPos;
    end;

    --
This discussion has been closed.