Add watermark: Autosize Font?

Hello,

I want to add a watermark to a page when a report is printer with the archive reader. To do that i am using the "OnReadPage" event to get access to the currentlty printed page:
   lDrawText := TppDrawText.Create(nil);
   lDrawText.Angle := 45;
   lDrawText.Left := lPage.PageDef.mmMarginLeft; 
   lDrawText.Top := lPage.PageDef.mmMarginTop; 
   lDrawText.Width := lPage.PageDef.mmPrintableWidth; 
   lDrawtext.Height := lPage.PageDef.mmPrintableWidth; 

   lDrawText.Font.Color := clLtGray;
   lDrawText.Font.Size := 100;     // how to autosize the font size to fit to the page?
   lDrawText.Alignment := taCenter;
   lDrawText.TextAlignment := taCentered;
   lDrawText.Transparent := true;
   lDrawText.IsMemo := false;
   lDrawText.Autosize := true;
   lDrawText.Text := 'WATERMARK';
   lDrawText.Page := lPage;
I found out the "lDrawText.Font.Size" by trial and error (can be used for the page size of my currently used test report). However it would be much better if the font size could be calculated to fit on any page size!

Can you give me a hint how to calculate the font size so that it fits exactly on the page (of any size) for the label printed in an angel of 45 degrees?

Thanks in advance,
Ralf

Comments

  • the line
    lDrawtext.Height := lPage.PageDef.mmPrintableWidth;
    should read
    lDrawtext.Height := lPage.PageDef.mmPrintableHeight;
    (Copy and paste error )
  • Additional question:

    I want this watermark to apear behind all other content of the report. With the code above the watermark appears on top of all other content. I read in other posts that the watermark should be added to a page style but all in examples i found the page style is already created at design time. Can i add such a page style in the OnReadPage (!!) event too?

    Thanks again,
    Ralf
  • edited September 2019
    Hi Ralf,

    ReportBuilder does not have auto font scaling so you will need to measure the text manually. I suggest creating a TBitmap and using the Canvas.TextWidth and Canvas.TextHeight to do so.

    -- pseudo code --

    lBitmap := TBitmap.Create;

    while not(lbSpaceFilled) do
    begin
    lBitmap.Canvas.Font := MyFont;

    liTextWidth := lBitmap.Canvas.TextWidth(lsWaterMarkText);

    if liTextWidth > liSpaceAvailable then
    begin
    lbSpaceFilled := True;
    MyFont.Size := MyFont.Size - 1;
    end
    else
    MyFont.Size := MyFont.Size + 1;
    end;

    lBitmap.Free;
    You can insert the drawcommand at the beginning of the list using the InsertChild command of the TppPage. (Instead of assigning the lDrawCommand.Page property.

    lPage.InsertChild(0, lDrawText); //Watermark behind
    //lDrawText.Page := lPage; //Watermark in front


    Best Regards,

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

    The solution ist working good, thanks.

    We currently add the watermark behind all other components. However that leads to a problem when a report contains (for example) label components that are set to be not transparent. These labels are hiding the watermark because of the non transparent background. which does not look good.

    A solution would be to add the watermark in front but make this watermark "semi-transparent". Is it possible to "alpha blend" the watermark (it is a TppDrawText) so that the background or and other component behind it shines through? I have not found a way to do that so far. So maybe it is not possible at all?

    Thanks in advance,
    Ralf

Sign In or Register to comment.