Draw letterheads in WPTools editor

The event OnPaintWatermark can be used with a TWPRichText to draw a watermark.

This event is executed every time the page is rendered, either on screen, during printing and also when a PDF export is performed using the component wPDF.

The event OnPaintWatermarks received the canvas to draw to, the current resolution to draw, the rectangle of the page. It also receives the paint mode which makes it possible, to detect if currently an export to PDF takes place.

It is very easy to print a metafile as letter head:

procedure TForm1.WPRichText1PaintWatermark(Sender: TObject;
  RTFEngine: TWPRTFEnginePaint; toCanvas: TCanvas; PageRect: TRect; PaintPageNr,
  RTFPageNr: Integer; WaterMarkRef: TObject; XRes, YRes: Integer;
  CurrentZoom: Single; PaintMode: TWPPaintModes);
begin
  toCanvas.StretchDraw(PageRect, Image1.Picture.Graphic);
end;

But in some cases the customers want a letterhead they can configure.

Why not use a TWPRichText to let the user edit their letterhead?

They can add images and tables, and even movable text boxes. (requires WPTools “Premium“)

The code is almost as simple as the above – simply the PrintPageOnCanvas method for a second TWPRichText is called:

procedure TForm2.WPRichText1PaintWatermark(Sender: TObject;
  RTFEngine: TWPRTFEnginePaint; toCanvas: TCanvas; 
  PageRect: TRect; PaintPageNr,
  RTFPageNr: Integer; WaterMarkRef: TObject; XRes, YRes: Integer;
  CurrentZoom: Single; PaintMode: TWPPaintModes);
begin
  BackgroundRTF.PaintPageOnCanvas(
     0,
     PageRect.Left,
     PageRect.Top,
     PageRect.Width,
     PageRect.Height,
     toCanvas,
     [wppGrayAll, wppGrayHeaderFooter, ppWhiteIsTransparent ],
     0,
     0,
     -1,
     -1,
     [wpNoViewPortAPI, wpNoViewPortOrgAPI]
      );
end;

The flags wppGrayAll and wppGrayHeaderFooter are optional, they will text page be drawn using gray instead of black.

The flags wpNoViewPortAPI, wpNoViewPortOrgAPI have to be used to make sure the BackgroundRTF is rendering in the scrolling position context of the main editor.

The text in BackgroundRTF may contain page number objects. To make these use the page number of the main editor add this two assignments:

BackgroundRTF.Memo.OverridePageCount := WPRichText1.PageCount;
BackgroundRTF.Memo.OverridePageOffset := PaintPageNr;