1. Home
  2. Computing & Technology
  3. Delphi Programming

Set TRichEdit Print Margins Programmatically
Rich Edit Printing Margins (PageRect)

By Zarko Gajic, About.com

Setup TRichEdit Printing Margins

The TRichEdit Delphi control is a wrapper for a Windows rich text edit control. Rich text edit controls let the user enter (edit, format, print, and save) text that includes font and paragraph formatting.

Rich Edit provides a simple Print method allowing you to programmatically print the (formatted) contents of the rich edit to the active printer - with a single line of code.

Unfortunately, there's no exposed functionality to set printer margins as you are accustomed to in various word processing applications.

Set Rich Edit Printing Margins

A custom Delphi procedure SetRichEditMargins demonstrate how to print, set the PageRect property for a TRichEdit with arbitrary margins. The PageRect property specifies the dimensions, in pixels, of the logical page size used when printing the contents of a rich text edit control.

The first 4 parameters specify the left, right, top and bottom margin in inches (1 IN = 2.54 cm). The last parameter is the rich edit control.

(* Set RichEdit margins in Inches (1 in = 2.54 cm)
   for the active printer *)

procedure SetRichEditMargins(
  const mLeft, mRight,
        mTop, mBottom: extended;
  const re : TRichEdit) ;
var
  ppiX, ppiY : integer;
  spaceLeft, spaceTop : integer;
  r : TRect;
begin
  // pixels per inch
  ppiX := GetDeviceCaps(Printer.Handle, LOGPIXELSX) ;
  ppiY := GetDeviceCaps(Printer.Handle, LOGPIXELSY) ;

  // non-printable margins
  spaceLeft := GetDeviceCaps(Printer.Handle, PHYSICALOFFSETX) ;
  spaceTop := GetDeviceCaps(Printer.Handle, PHYSICALOFFSETY) ;

  //calc margins
  R.Left := Round(ppiX * mLeft) - spaceLeft;
  R.Right := Printer.PageWidth - Round(ppiX * mRight) - spaceLeft;
  R.Top := Round(ppiY * mTop) - spaceTop;
  R.Bottom := Printer.PageHeight - Round(ppiY * mBottom) - spaceTop;

  // set margins
  re.PageRect := r;
end;
Note: do not mix "printer margins" with the "Margins" property in the VCL.

Here's a simple usage example:

SetRichEditMargins(1, 1, 0.5, 0.5, richEdit1) ;
richEdit1.Print('Printing with margins') ;

Delphi tips navigator:
» Where to Locate the Missing TServerSocket and TClientSocket Delphi components
« Hide a Delphi 2007 Application Button from the TaskBar (with MainFormOnTaskBar)

More Delphi Programming Quick Tips
Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2008 Tips
  7. Set TRichEdit Print Margins Programmatically

©2009 About.com, a part of The New York Times Company.

All rights reserved.