How To…Manually Add a Watermark to a Page

Question

“How can I add a watermark to a page in code without using the PageStyle band?”

Solution

Use the Report.OnEndPage event to create the drawcommands needed and manually add them to the page.

Note: If you would like the watermark to appear behind the rest of the report components, create a Page Style and use its BeforePrint event instead.

Download:
WaterMark.zip
WaterMarkBehind.zip

Sample Delphi code:

uses
  ppDrwCmd,
  ppUtils,
  ppTypes;

procedure TForm1.ppReport1EndPage(Sender: TObject);
begin
  CreateWaterMark.Page := ppReport1.Engine.Page;

end;


function TForm1.CreateWaterMark: TppDrawCommand;
var
  lWaterMark: TppDrawText;
  llPageHeight: Longint;
  llPageWidth: Longint;
  liTextWidth: Integer;
  liTextHeight: Integer;
  lPrinter: TppPrinter;
begin

  lWaterMark := TppDrawText.Create(nil);
  lWaterMark.WrappedText.Add('ReportBuilder ' + ppEdition + #153 + ' - Demo Copy');
  lWaterMark.WrappedText.Add('Version ' + ppVersion);
  lWaterMark.WrappedText.Add('Digital Metaphors Corporation');
  lWaterMark.WrappedText.Add('E-Mail: sales@digital-metaphors.com');
  lWaterMark.Transparent := True;
  lWaterMark.IsMemo      := False;
  lWaterMark.WordWrap    := True;
  lWaterMark.Font.Name   := 'Arial';
  lWaterMark.Font.Size   := 18;
  lWaterMark.Font.Color  := clBlack;
  lWaterMark.Autosize    := True;

  liTextWidth  := 375;
  liTextHeight := 150;

  lPrinter := ppReport1.Printer;

  llPageHeight      := lPrinter.PrinterSetup.PageDef.mmPrintableHeight;
  llPageWidth       := lPrinter.PrinterSetup.PageDef.mmPrintableWidth;
  lWaterMark.Height := ppToMMThousandths(liTextHeight, utScreenPixels, pprtHorizontal, nil);
  lWaterMark.Width  := ppToMMThousandths(liTextWidth,  utScreenPixels, pprtHorizontal, nil);

  lWaterMark.Top  := (llPageHeight - lWaterMark.Height)  div 2;
  lWaterMark.Left := (llPageWidth -  lWaterMark.Width )  div 2;

  Result := lWaterMark;

end;