Mega Code Archive
Set TRichEdit Print Margins Programmatically
Title: Set TRichEdit Print Margins Programmatically
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') ;