Print other RTF text as letterhead

<< Click to Display Table of Contents >>

Navigation:  Programming > Printing and PDF Export > Watermarks >

Print other RTF text as letterhead

You can print the contents of one editor to the background of a different editor:

 

procedure TWPLetterHeadEdit.WPRichText1PaintWatermark(Sender: TObject;

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

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

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

begin

// We are painting on a RTF-Engine surface so use the

// PaintPageMode wpNoViewPortAPI or wpUseWorldScaling since everything has been

// set up already

 WPLetterhead.PaintPageOnCanvas(

    0,         // PageNo

    0, 0, 0, 0, // Use current page size

    toCanvas,   // The destination canvas

    PaintMode+[wppNoPageBackground], // or []

    XRes, YRes, // the printing resolution

    -1, -1,     // No clipping

    [wpUseWorldScaling]);

end;

 

The code above however does not work perfectly when printing since the the printer always uses a physical margin.

The left and top physical margin needs to be subtracted in this case.

 

Use this code to calculate the physical margin:

 

 xoff := -GetDeviceCaps(toCanvas.Handle, PHYSICALOFFSETX);

 yoff := -GetDeviceCaps(toCanvas.Handle, PHYSICALOFFSETY);

 

To use such margin values PaintPageOnCanvas needs to be called differently:

 

 WPLetterhead.PaintPageOnCanvas(0, // Pagenr.

      xoff, yoff, // Offset

      0, 0,

      toCanvas,

      [wppNoPageBackground]+ (PaintMode * [wppInPaintForwPDF,wppOutputToPrinter]),

      GetDeviceCaps(toCanvas.Handle, LOGPIXELSX),

      GetDeviceCaps(toCanvas.Handle, LOGPIXELSY), -1, -1,

      [ wpUseProvidedWidthHeight   ]);

 

Please note also that the current page size is not necessarily the size of the paper in the printer.

In our RTFonWatermark demo we use some special code to read the current page size and draw lines for verification:

 

In Form.OnShow we read the page size, set the margins and make sure the text is formatted:

 

procedure TWPPrintRTFAsWatermark.FormShow(Sender: TObject);

begin

  // Adjust the page size to current printer size

  WPRichText1.ReadPrinterProperties;

  WatermarkText.ReadPrinterProperties;

 

  // Adjust the page margins - set 2 cm on all sides

  WPRichText1.Header.SetPageWHCentimeter(-1,-1, 2,2,2,2);

  WatermarkText.Header.SetPageWHCentimeter(-1,-1, 2,2,2,2)

end;

 

The event handler draws some lines. The outer, blue line is not visible when printing since it is in the non printable area.

 

procedure TWPPrintRTFAsWatermark.WPRichText1PaintWatermark(Sender: TObject;

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

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

 CurrentZoom: Single; PaintMode: TWPPaintModes);

var xoff, yoff : Integer;

   r : TRect;

   margin_cm : Single;

begin

  // When printing we need to subtract the physical margins

  if wppOutputToPrinter in PaintMode then

  begin

      xoff := -GetDeviceCaps(toCanvas.Handle, PHYSICALOFFSETX);

      yoff := -GetDeviceCaps(toCanvas.Handle, PHYSICALOFFSETY);

      // We need the real physical page size

      PageRect.Right := GetDeviceCaps(toCanvas.Handle, PHYSICALWIDTH);

      PageRect.Bottom := GetDeviceCaps(toCanvas.Handle, PHYSICALHEIGHT);

  end else

  begin

      xoff := 0;

      yoff := 0;

  end;

 

  // For test purposes we draw a line around the page

  margin_cm := 1.5;

 

  r := Rect( xoff, yoff, xoff+PageRect.Right, yoff+PageRect.Bottom);

  toCanvas.Pen.Width := XRes div 20;

  toCanvas.Pen.Color := clBlue;

  toCanvas.Brush.Style :=bsClear;

  toCanvas.Rectangle(r.Left, r.Top, r.Right, r.Bottom);

 

  InflateRect(r, -Round(margin_cm/2.54*XRes), -Round(margin_cm/2.54*YRes) );

  toCanvas.Pen.Width := 0;

  toCanvas.Pen.Color := clRed;

  //toCanvas.Brush.Style := bsSolid;

  //toCanvas.Brush.Color := clYellow;

  toCanvas.Rectangle(r.Left, r.Top, r.Right, r.Bottom);

 

  toCanvas.Font.Name := 'Arial';

  toCanvas.Font.Height := -XRes div 10;

  SetTextAlign(toCanvas.Handle, TA_TOP);

  toCanvas.TextOut(r.Left, r.Top, Format('%.2f cm',[margin_cm]));

 

  toCanvas.TextOut(r.Left, r.Bottom, Format('%.2f cm',[r.Bottom/YRes*2.54]));

 

  // Here we print on screen

  if wppInPaintDesktop in PaintMode then

  begin

    // Print on screen in Editor

    WatermarkText.PaintPageOnCanvas(0, // Pagenr.

      0, 0, 0, 0, toCanvas, [wppNoPageBackground], XRes, YRes, -1, -1, [wpNoViewPortAPI]);

  end

  // And here to the printer or on the wPDF Canvas.

  else

  begin

    WatermarkText.PaintPageOnCanvas(0, // Pagenr.

      xoff, yoff, // Offset

      0, 0,

      toCanvas,

      [wppNoPageBackground]+ (PaintMode * [wppInPaintForwPDF,wppOutputToPrinter]),

      GetDeviceCaps(toCanvas.Handle, LOGPIXELSX),

      GetDeviceCaps(toCanvas.Handle, LOGPIXELSY), -1, -1,

      [  wpUseProvidedWidthHeight   ]);

  end;

end;