Display a pre-printed form

<< Click to Display Table of Contents >>

Navigation:  Programming > Printing and PDF Export > Watermarks >

Display a pre-printed form

Display a pre-printed form (such as a money transfer form) as part of the page.

 

WaterM2Demo

 

This example requires to use the event OnMeasurePage as well. In this event we reserve space at the bottom of the first page:

 

const PR_Form_Height = 10.5; // Form Height in CM

     PR_Form_Width  = 15.0;

 

procedure TForm1.WPRichText1MeasureTextPage(Sender: TObject;

 PageInfo: TWPMeasurePageParam);

begin

// We want to make sure the first page has a bottom margin which is

// large enough for our form

if PageInfo.pagenr = 1 then

begin

   PageInfo.marginbottom := WPCentimeterToTwips(PR_Form_Height);

   PageInfo.changed := TRUE;

end;

end;

 

The PaintWatermark code only draws on the first page:

 

procedure TForm1.WPRichText1PaintWatermark(Sender: TObject;

 RTFEngine: TWPRTFEnginePaint; toCanvas: TCanvas; PageRect: TRect;

 PaintPageNr, RTFPageNr: Integer; WaterMarkRef: TObject; XRes,

 YRes: Integer; CurrentZoom: Single; PaintMode: TWPPaintModes);

// ~~~~~~~~~~~~~~~~~~~~~ Convert CM values into pixel ~~~~~~~~~~~~~

function XP(cm: Double): Integer;

begin

   Result := MulDiv(WPCentimeterToTwips(cm), Xres, 1440);

end;

function YP(cm: Double): Integer;

begin

   Result := MulDiv(WPCentimeterToTwips(cm), Yres, 1440);

end;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

var r, r2: TRect;

 off, w: Integer;

begin

if PaintPageNr = 0 then

begin

   r := PageRect;

   r.Top := r.Bottom - YP(PR_Form_Height);

   toCanvas.Pen.Color := clBlack;

   toCanvas.Pen.Style := psDash;

   toCanvas.MoveTo(r.Left, r.Top);

   toCanvas.LineTo(r.Right, r.Top);

 

  // Draw the form at the right bottom border

   r.Left := r.Right - XP(PR_Form_Width);

 

  // Draw line

   toCanvas.MoveTo(r.Left, r.Top);

   toCanvas.LineTo(r.Left, r.Bottom);

 

  // Draw the sizzors

   toCanvas.Font.Name := 'WingDings';

   toCanvas.Font.Height := -YP(0.5);

   toCanvas.TextOut( PageRect.Left + XP(0.7), r.Top-

      toCanvas.TextHeight(#$22) div 2 , #$22 );

 

    // This is background of the form, we do not draw this when

    // we are printing!

  if wppInPaintDesktop in PaintMode then

  begin

     r2 := r;

     inc(r2.Left,XP(0.1));

     inc(r2.Top,YP(0.1));

     toCanvas.Brush.Color := clYellow;

     toCanvas.FillRect(r2);

  end;

 

    // This are the form text

   toCanvas.Brush.Color := clWhite;

   toCanvas.Font.Name := 'Courier New';

   toCanvas.Font.Height := -YP(0.5);

   toCanvas.Font.Style := [fsBold];

   off := YP(0.1);

 

    // NAME

   r2.Left := r.Left + XP(1.0);

   r2.Top := r.Top + YP(2);

   r2.Right := r2.Left + XP(10);

   r2.Bottom := r2.Top + YP(0.7);

   toCanvas.FillRect(r2);

   toCanvas.TextOut(r2.Left + off, r2.Top + off, NameE.Text);

 

    // ADR

   r2.Left := r.Left + XP(1.0);

   r2.Top := r.Top + YP(5.5);

   r2.Right := r2.Left + XP(10);

   r2.Bottom := r2.Top + YP(0.7);

   toCanvas.FillRect(r2);

   toCanvas.TextOut(r2.Left + off, r2.Top + off, AdrE.Text);

 

    // COST, right aligned

   r2.Left := r.Left + XP(8.5);

   r2.Top := r.Top + YP(4.5);

   r2.Right := r2.Left + XP(5);

   r2.Bottom := r2.Top + YP(0.7);

   toCanvas.FillRect(r2);

   w := toCanvas.TextWidth(CostE.Text);

   toCanvas.TextOut(r2.Right - off - w, r2.Top + off, CostE.Text);

 

  // For Debugging

  // Draw a Line at 10,10 CM

  if DrawDebugCross.Checked then

  begin

     toCanvas.TextOut( PageRect.Left+XP(10), PageRect.Top, '10');

     toCanvas.TextOut( PageRect.Left, PageRect.Top+YP(10), '10');

     toCanvas.MoveTo( PageRect.Left, PageRect.Top + YP(10));

     toCanvas.LineTo( PageRect.Right, PageRect.Top + YP(10));

     toCanvas.MoveTo( PageRect.Left+XP(10), PageRect.Top);

     toCanvas.LineTo( PageRect.Left+XP(10), r.Top);

  end;

end;

end;